migrate some widget from statefulWidget to statelessWidget to enhance performance

null_safety
小赖 4 years ago
parent 5c299a5dd5
commit 7d688b08ac

@ -1,3 +1,3 @@
{ {
"dart.flutterSdkPath": "/Users/akufe/apps/flutter" "dart.flutterSdkPath": "/Users/akufe/fvm/versions/1.22.6"
} }

@ -2,11 +2,11 @@ library ansu_ui;
//buttons //buttons
export 'buttons/as_button.dart'; export 'buttons/as_button.dart';
export 'buttons/as_longbutton.dart'; export 'buttons/as_long_button.dart';
export 'buttons/as_back_button.dart'; export 'buttons/as_back_button.dart';
export 'buttons/as_numeric_button.dart'; export 'buttons/as_numeric_button.dart';
export 'buttons/as_bottom_button.dart'; export 'buttons/as_bottom_button.dart';
export 'buttons/as_gradientbutton.dart'; export 'buttons/as_gradient_button.dart';
export 'buttons/as_radio_button.dart'; export 'buttons/as_radio_button.dart';
export 'buttons/as_material_button.dart'; export 'buttons/as_material_button.dart';

@ -3,7 +3,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ansu_ui/styles/as_colors.dart'; import 'package:ansu_ui/styles/as_colors.dart';
/// ///
class ASButton extends StatefulWidget { class ASButton extends StatelessWidget {
/// ///
final Color bgcolor; final Color bgcolor;
@ -143,40 +143,33 @@ class ASButton extends StatefulWidget {
padding = EdgeInsets.symmetric(vertical: 8.w), padding = EdgeInsets.symmetric(vertical: 8.w),
super(key: key); super(key: key);
@override
_ASButtonState createState() => _ASButtonState();
}
class _ASButtonState extends State<ASButton> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialButton( return MaterialButton(
disabledColor: widget.disableColor, disabledColor: disableColor,
disabledTextColor: widget.disableTextColor, disabledTextColor: disableTextColor,
textColor: widget.textColor ?? kLightPrimaryColor, textColor: textColor ?? kLightPrimaryColor,
minWidth: widget.width, minWidth: width,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
onPressed: widget.onPressed, onPressed: onPressed,
child: widget.title is String child: title is String
? Text(widget.title, ? Text(title, style: textStyle ?? TextStyle(fontSize: 13.sp))
style: widget.textStyle ?? TextStyle(fontSize: 13.sp)) : title,
: widget.title, padding: padding ?? EdgeInsets.symmetric(vertical: 6.w, horizontal: 12.w),
padding: widget.padding ?? shape: radius == null
EdgeInsets.symmetric(vertical: 6.w, horizontal: 12.w),
shape: widget.radius == null
? StadiumBorder( ? StadiumBorder(
side: widget.outline side: outline
? BorderSide(color: widget.outlineColor, width: 0.5.w) ? BorderSide(color: outlineColor, width: 0.5.w)
: BorderSide.none, : BorderSide.none,
) )
: RoundedRectangleBorder( : RoundedRectangleBorder(
side: widget.outline side: outline
? BorderSide(color: widget.outlineColor, width: 0.5.w) ? BorderSide(color: outlineColor, width: 0.5.w)
: BorderSide.none, : BorderSide.none,
borderRadius: BorderRadius.circular(widget.radius ?? 15.5.w)), borderRadius: BorderRadius.circular(radius ?? 15.5.w)),
color: widget.bgcolor ?? kForegroundColor, color: bgcolor ?? kForegroundColor,
splashColor: widget.splashColor, splashColor: splashColor,
highlightColor: widget.splashColor?.withOpacity(0.3), highlightColor: splashColor?.withOpacity(0.3),
elevation: 0, elevation: 0,
focusElevation: 0, focusElevation: 0,
highlightElevation: 0, highlightElevation: 0,

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ansu_ui/styles/as_colors.dart'; import 'package:ansu_ui/styles/as_colors.dart';
class ASGradientButton extends StatefulWidget { class ASGradientButton extends StatelessWidget {
/// ///
///stringwidget ///stringwidget
final dynamic title; final dynamic title;
@ -90,19 +90,14 @@ class ASGradientButton extends StatefulWidget {
colors = [Color(0xFFFFA700), Color(0xFFFFBD00)], colors = [Color(0xFFFFA700), Color(0xFFFFBD00)],
super(key: key); super(key: key);
@override bool get isNullFunc => onPressed == null;
_ASGradientButtonState createState() => _ASGradientButtonState(); List<Color> get _colors {
} if (colors == null)
class _ASGradientButtonState extends State<ASGradientButton> {
bool get isNullFunc => widget.onPressed == null;
List<Color> get colors {
if (widget.colors == null)
return null; return null;
else { else {
return List.generate( return List.generate(
widget.colors.length, colors.length,
(index) => widget.colors[index].withOpacity(isNullFunc ? 0.5 : 1), (index) => colors[index].withOpacity(isNullFunc ? 0.5 : 1),
); );
} }
} }
@ -112,32 +107,32 @@ class _ASGradientButtonState extends State<ASGradientButton> {
return AnimatedContainer( return AnimatedContainer(
curve: Curves.easeInOutCirc, curve: Curves.easeInOutCirc,
duration: Duration(milliseconds: 300), duration: Duration(milliseconds: 300),
width: widget.width ?? 110.w, width: width ?? 110.w,
decoration: BoxDecoration( decoration: BoxDecoration(
gradient: LinearGradient( gradient: LinearGradient(
begin: widget.begin ?? Alignment.topCenter, begin: begin ?? Alignment.topCenter,
end: widget.end ?? Alignment.bottomCenter, end: end ?? Alignment.bottomCenter,
colors: colors ?? colors: _colors ??
[ [
kDarkPrimaryColor.withOpacity(isNullFunc ? 0.5 : 1), kDarkPrimaryColor.withOpacity(isNullFunc ? 0.5 : 1),
kLightPrimaryColor.withOpacity(isNullFunc ? 0.5 : 1), kLightPrimaryColor.withOpacity(isNullFunc ? 0.5 : 1),
], ],
), ),
borderRadius: BorderRadius.circular(widget.radius ?? 20.w), borderRadius: BorderRadius.circular(radius ?? 20.w),
), ),
child: MaterialButton( child: MaterialButton(
disabledColor: widget.disableColor, disabledColor: disableColor,
textColor: widget.textColor ?? Color(0xD9FFFFFF), textColor: textColor ?? Color(0xD9FFFFFF),
disabledTextColor: widget.disableTextColor, disabledTextColor: disableTextColor,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
onPressed: widget.onPressed, onPressed: onPressed,
child: widget.title is String child: title is String
? Text(widget.title, ? Text(title,
style: widget.textStyle ?? TextStyle(fontSize: 18.sp)) style: textStyle ?? TextStyle(fontSize: 18.sp))
: widget.title, : title,
padding: widget.padding ?? EdgeInsets.symmetric(vertical: 10.w), padding: padding ?? EdgeInsets.symmetric(vertical: 10.w),
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(widget.radius ?? 20.w)), borderRadius: BorderRadius.circular(radius ?? 20.w)),
elevation: 0, elevation: 0,
), ),
); );

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ansu_ui/styles/as_colors.dart'; import 'package:ansu_ui/styles/as_colors.dart';
class ASLongButton extends StatefulWidget { class ASLongButton extends StatelessWidget {
/// ///
///stringwidget ///stringwidget
final dynamic title; final dynamic title;
@ -87,35 +87,30 @@ class ASLongButton extends StatefulWidget {
outline = true, outline = true,
outlineColor = kDarkPrimaryColor, outlineColor = kDarkPrimaryColor,
super(key: key); super(key: key);
@override
_ASLongButtonState createState() => _ASLongButtonState();
}
class _ASLongButtonState extends State<ASLongButton> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialButton( return MaterialButton(
minWidth: widget.width ?? 280.w, minWidth: width ?? 280.w,
disabledColor: widget.disableColor, disabledColor: disableColor,
textColor: widget.textColor ?? kLightTextColor, textColor: textColor ?? kLightTextColor,
disabledTextColor: widget.disableTextColor, disabledTextColor: disableTextColor,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
onPressed: widget.onPressed, onPressed: onPressed,
child: widget.title is String child: title is String
? Text(widget.title, ? Text(title,
style: widget.textStyle ?? TextStyle(fontSize: 20.sp)) style: textStyle ?? TextStyle(fontSize: 20.sp))
: widget.title, : title,
padding: widget.padding ?? EdgeInsets.symmetric(vertical: 8.w), padding: padding ?? EdgeInsets.symmetric(vertical: 8.w),
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
side: widget.outline side: outline
? BorderSide(color: widget.outlineColor, width: 1.w) ? BorderSide(color: outlineColor, width: 1.w)
: BorderSide.none, : BorderSide.none,
borderRadius: BorderRadius.circular(widget.radius ?? 22.5.w)), borderRadius: BorderRadius.circular(radius ?? 22.5.w)),
color: widget.bgColor ?? kForegroundColor, color: bgColor ?? kForegroundColor,
elevation: 0, elevation: 0,
highlightElevation: 0, highlightElevation: 0,
splashColor: ColorTool.getSplashColor(widget.bgColor), splashColor: ColorTool.getSplashColor(bgColor),
); );
} }
} }

@ -10,16 +10,11 @@ import 'package:velocity_x/velocity_x.dart';
///`items` ///`items`
/// ///
///with auto cancel ///with auto cancel
class ASBottomDialog extends StatefulWidget { class ASBottomDialog extends StatelessWidget {
final List<Widget> items; final List<Widget> items;
ASBottomDialog({Key key, @required this.items}) : super(key: key); ASBottomDialog({Key key, @required this.items}) : super(key: key);
@override _buildCancel(BuildContext context) {
_ASBottomDialogState createState() => _ASBottomDialogState();
}
class _ASBottomDialogState extends State<ASBottomDialog> {
_buildCancel() {
return ASMaterialButton( return ASMaterialButton(
color: kForegroundColor, color: kForegroundColor,
onPressed: () { onPressed: () {
@ -37,9 +32,9 @@ class _ASBottomDialogState extends State<ASBottomDialog> {
crossAxisAlignment: CrossAxisAlignment.stretch, crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
...widget.items.sepWidget(separate: ASDivider()), ...items.sepWidget(separate: ASDivider()),
10.hb, 10.hb,
_buildCancel(), _buildCancel(context),
], ],
), ),
); );

@ -5,7 +5,7 @@ import 'package:ansu_ui/styles/as_colors.dart';
import 'package:ansu_ui/extension/num_extension.dart'; import 'package:ansu_ui/extension/num_extension.dart';
import 'package:ansu_ui/extension/list_extension.dart'; import 'package:ansu_ui/extension/list_extension.dart';
class ASDialog extends StatefulWidget { class ASDialog extends StatelessWidget {
final bool close; final bool close;
/// ///
@ -30,21 +30,16 @@ class ASDialog extends StatefulWidget {
this.childPadding, this.childPadding,
}) : super(key: key); }) : super(key: key);
@override double get _widgetSpacer => spacer ?? 20.w;
_ASDialogState createState() => _ASDialogState();
}
class _ASDialogState extends State<ASDialog> {
double get _widgetSpacer => widget.spacer ?? 20.w;
EdgeInsets get _widgetPadding => EdgeInsets get _widgetPadding =>
widget.padding ?? padding ??
EdgeInsets.only( EdgeInsets.only(
top: 13.w, top: 13.w,
bottom: 20.w, bottom: 20.w,
); );
EdgeInsets get _childPadding => EdgeInsets get _childPadding =>
widget.childPadding ?? childPadding ??
EdgeInsets.only( EdgeInsets.only(
top: 40.w, top: 40.w,
bottom: 50.w, bottom: 50.w,
@ -75,16 +70,16 @@ class _ASDialogState extends State<ASDialog> {
), ),
child: Padding( child: Padding(
padding: _childPadding, padding: _childPadding,
child: widget.child, child: child,
), ),
), ),
), ),
...widget.items ...items
.sepWidget(separate: SizedBox(height: _widgetSpacer)), .sepWidget(separate: SizedBox(height: _widgetSpacer)),
], ],
), ),
), ),
widget.close close
? Positioned( ? Positioned(
right: 0, right: 0,
top: 0, top: 0,

@ -1,6 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ansu_ui/buttons/as_longbutton.dart'; import 'package:ansu_ui/buttons/as_long_button.dart';
class ASDialogButton extends StatelessWidget { class ASDialogButton extends StatelessWidget {
final String title; final String title;

@ -35,7 +35,7 @@ Future<T> showASDrawer<T>(Widget drawer) async {
/// ///
/// ///
///使`showASDrawer`使`Scaffold``drawer``endDrawer` ///使`showASDrawer`使`Scaffold``drawer``endDrawer`
class ASDrawer extends StatefulWidget { class ASDrawer extends StatelessWidget {
///List Children ///List Children
/// ///
///ListView ///ListView
@ -85,11 +85,6 @@ class ASDrawer extends StatefulWidget {
'child or children cant be null'), 'child or children cant be null'),
super(key: key); super(key: key);
@override
_ASDrawerState createState() => _ASDrawerState();
}
class _ASDrawerState extends State<ASDrawer> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Align( return Align(
@ -100,15 +95,15 @@ class _ASDrawerState extends State<ASDrawer> {
height: screenHeight - statusBarHeight, height: screenHeight - statusBarHeight,
width: screenWidth - 44.w, width: screenWidth - 44.w,
child: Material( child: Material(
child: widget.child ?? child: child ??
Stack( Stack(
children: [ children: [
ListView( ListView(
padding: widget.padding, padding: padding,
children: widget.children, children: children,
), ),
Positioned( Positioned(
child: widget.bottom ?? SizedBox(), child: bottom ?? SizedBox(),
bottom: 45.w, bottom: 45.w,
left: 26.w, left: 26.w,
right: 26.w, right: 26.w,

@ -3,7 +3,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ansu_ui/styles/as_colors.dart'; import 'package:ansu_ui/styles/as_colors.dart';
import 'package:ansu_ui/extension/num_extension.dart'; import 'package:ansu_ui/extension/num_extension.dart';
class ASEditTile extends StatefulWidget { class ASEditTile extends StatelessWidget {
final Widget title; final Widget title;
final FocusNode node; final FocusNode node;
final String hintText; final String hintText;
@ -16,18 +16,13 @@ class ASEditTile extends StatefulWidget {
this.controller, this.controller,
}) : super(key: key); }) : super(key: key);
@override
_ASEditTileState createState() => _ASEditTileState();
}
class _ASEditTileState extends State<ASEditTile> {
FocusNode _node; FocusNode _node;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
if (widget.node != null) { if (node != null) {
widget.node?.requestFocus(); node?.requestFocus();
} else { } else {
_node?.requestFocus(); _node?.requestFocus();
} }
@ -43,13 +38,13 @@ class _ASEditTileState extends State<ASEditTile> {
color: Colors.black.withOpacity(0.65), color: Colors.black.withOpacity(0.65),
fontSize: 14.sp, fontSize: 14.sp,
), ),
child: widget.title ?? Text(''), child: title ?? Text(''),
), ),
), ),
Expanded( Expanded(
child: TextField( child: TextField(
focusNode: widget.node ?? _node, focusNode: node ?? _node,
controller: widget.controller, controller: controller,
textAlign: TextAlign.end, textAlign: TextAlign.end,
style: TextStyle( style: TextStyle(
fontSize: 14.sp, fontSize: 14.sp,
@ -60,7 +55,7 @@ class _ASEditTileState extends State<ASEditTile> {
border: InputBorder.none, border: InputBorder.none,
isDense: true, isDense: true,
contentPadding: EdgeInsets.zero, contentPadding: EdgeInsets.zero,
hintText: widget.hintText, hintText: hintText,
hintStyle: TextStyle( hintStyle: TextStyle(
color: kTextSubColor, color: kTextSubColor,
fontSize: 14.sp, fontSize: 14.sp,

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ansu_ui/styles/as_colors.dart'; import 'package:ansu_ui/styles/as_colors.dart';
class ASListTile extends StatefulWidget { class ASListTile extends StatelessWidget {
/// ///
final String title; final String title;
@ -33,27 +33,23 @@ class ASListTile extends StatefulWidget {
}) : height = 32.w, }) : height = 32.w,
crossAxisAlignment = CrossAxisAlignment.center, crossAxisAlignment = CrossAxisAlignment.center,
super(key: key); super(key: key);
@override
_ASListTileState createState() => _ASListTileState();
}
class _ASListTileState extends State<ASListTile> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
margin: widget.height == null margin: height == null
? EdgeInsets.symmetric(vertical: 8.w, horizontal: 10.w) ? EdgeInsets.symmetric(vertical: 8.w, horizontal: 10.w)
: EdgeInsets.symmetric(horizontal: 10.w), : EdgeInsets.symmetric(horizontal: 10.w),
height: widget.height, height: height,
alignment: Alignment.centerLeft, alignment: Alignment.centerLeft,
child: Row( child: Row(
crossAxisAlignment: crossAxisAlignment:
widget.crossAxisAlignment ?? CrossAxisAlignment.start, crossAxisAlignment ?? CrossAxisAlignment.start,
children: [ children: [
Container( Container(
width: 85.w, width: 85.w,
child: Text( child: Text(
widget.title, title,
maxLines: 1, maxLines: 1,
overflow: TextOverflow.visible, overflow: TextOverflow.visible,
softWrap: false, softWrap: false,
@ -61,17 +57,17 @@ class _ASListTileState extends State<ASListTile> {
), ),
), ),
Expanded( Expanded(
child: widget.text == null child: text == null
? Text('') ? Text('')
: widget.text is String : text is String
? Text( ? Text(
widget.text, text,
maxLines: 2, maxLines: 2,
style: TextStyle(color: kTextSubColor, fontSize: 14.sp), style: TextStyle(color: kTextSubColor, fontSize: 14.sp),
) )
: widget.text, : text,
), ),
widget.trail ?? SizedBox() trail ?? SizedBox()
], ],
), ),
); );

@ -4,7 +4,7 @@ import 'package:ansu_ui/styles/as_colors.dart';
import 'package:ansu_ui/extension/num_extension.dart'; import 'package:ansu_ui/extension/num_extension.dart';
import 'package:ansu_ui/extension/text_style_extension.dart'; import 'package:ansu_ui/extension/text_style_extension.dart';
import 'package:ansu_ui/pickers/as_date_range_picker_part.dart'; import 'package:ansu_ui/pickers/as_date_range_picker_part.dart';
import 'package:ansu_ui/buttons/as_longbutton.dart'; import 'package:ansu_ui/buttons/as_long_button.dart';
import 'package:velocity_x/velocity_x.dart'; import 'package:velocity_x/velocity_x.dart';
class AS2DatePicker extends StatefulWidget { class AS2DatePicker extends StatefulWidget {

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart'; import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:ansu_ui/styles/as_colors.dart'; import 'package:ansu_ui/styles/as_colors.dart';
class ASRefresh extends StatefulWidget { class ASRefresh extends StatelessWidget {
final Widget child; final Widget child;
final Future Function() onLoad; final Future Function() onLoad;
final Future Function() onRefresh; final Future Function() onRefresh;
@ -23,22 +23,17 @@ class ASRefresh extends StatefulWidget {
this.scrollController, this.scrollController,
}) : super(key: key); }) : super(key: key);
@override
_ASRefreshState createState() => _ASRefreshState();
}
class _ASRefreshState extends State<ASRefresh> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return EasyRefresh( return EasyRefresh(
scrollController: widget.scrollController, scrollController: scrollController,
child: widget.child, child: child,
onRefresh: widget.onRefresh, onRefresh: onRefresh,
onLoad: widget.onLoad, onLoad: onLoad,
controller: widget.controller, controller: controller,
firstRefresh: widget.firstRefresh, firstRefresh: firstRefresh,
emptyWidget: widget.emptyWidget, emptyWidget: emptyWidget,
firstRefreshWidget: widget.firstRefreshWidget, firstRefreshWidget: firstRefreshWidget,
header: MaterialHeader( header: MaterialHeader(
valueColor: AlwaysStoppedAnimation(kPrimaryColor), valueColor: AlwaysStoppedAnimation(kPrimaryColor),
), ),

@ -3,7 +3,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ansu_ui/styles/as_colors.dart'; import 'package:ansu_ui/styles/as_colors.dart';
import 'package:ansu_ui/extension/num_extension.dart'; import 'package:ansu_ui/extension/num_extension.dart';
class ASTag extends StatefulWidget { class ASTag extends StatelessWidget {
/// ///
final double width; final double width;
@ -108,37 +108,33 @@ class ASTag extends StatefulWidget {
width = 38.w, width = 38.w,
height = 19.w, height = 19.w,
super(key: key); super(key: key);
@override
_ASTagState createState() => _ASTagState();
}
class _ASTagState extends State<ASTag> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
padding: widget.padding ?? padding: padding ??
EdgeInsets.symmetric( EdgeInsets.symmetric(
horizontal: 6.w, horizontal: 6.w,
vertical: 2.w, vertical: 2.w,
), ),
width: widget.width, width: width,
height: widget.height ?? 19.w, height: height ?? 19.w,
decoration: BoxDecoration( decoration: BoxDecoration(
color: widget.bgColor ?? kDarkPrimaryColor, color: bgColor ?? kDarkPrimaryColor,
border: widget.outline border: outline
? Border.all( ? Border.all(
width: 1.w, width: 1.w,
color: widget.outlineColor ?? kSecondaryColor, color: outlineColor ?? kSecondaryColor,
) )
: Border.fromBorderSide(BorderSide.none), : Border.fromBorderSide(BorderSide.none),
borderRadius: (widget.radius ?? 9.w).radius, borderRadius: (radius ?? 9.w).radius,
), ),
alignment: Alignment.center, alignment: Alignment.center,
child: Text( child: Text(
widget.text, text,
style: widget.textStyle ?? style: textStyle ??
TextStyle( TextStyle(
color: widget.textColor, color: textColor,
fontSize: 10.sp, fontSize: 10.sp,
), ),
), ),

Loading…
Cancel
Save