From 011b25b5ba313b564113c60c017fdaefcf907d2d Mon Sep 17 00:00:00 2001 From: laiiihz Date: Wed, 3 Mar 2021 17:37:23 +0800 Subject: [PATCH] migrate checkBox and button from stateful to stateless --- lib/box/as_check_box.dart | 25 +++++++++-------------- lib/buttons/as_bottom_button.dart | 33 +++++++++++++------------------ 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/lib/box/as_check_box.dart b/lib/box/as_check_box.dart index f166d4e..964bc74 100644 --- a/lib/box/as_check_box.dart +++ b/lib/box/as_check_box.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; -class ASCheckBox extends StatefulWidget { +class ASCheckBox extends StatelessWidget { final bool value; ///控制选中时的样式 @@ -17,11 +17,6 @@ class ASCheckBox extends StatefulWidget { : checkStyle = true, super(key: key); - @override - _ASCheckBoxState createState() => _ASCheckBoxState(); -} - -class _ASCheckBoxState extends State { _renderDefault() { return Container( height: 17.w, @@ -37,11 +32,10 @@ class _ASCheckBoxState extends State { child: AnimatedContainer( duration: Duration(milliseconds: 300), curve: Curves.fastOutSlowIn, - height: widget.value ? 13.w : 5.w, - width: widget.value ? 13.w : 5.w, + height: value ? 13.w : 5.w, + width: value ? 13.w : 5.w, decoration: BoxDecoration( - color: (widget.color ?? Color(0xFFF69A2D)) - .withOpacity(widget.value ? 1 : 0), + color: (color ?? Color(0xFFF69A2D)).withOpacity(value ? 1 : 0), borderRadius: BorderRadius.circular(13.w), ), ), @@ -58,22 +52,21 @@ class _ASCheckBoxState extends State { decoration: BoxDecoration( border: Border.all( color: Color(0xFFD5D5D5), - width: widget.value ? 0 : 1.w, + width: value ? 0 : 1.w, ), borderRadius: BorderRadius.circular(27.w), - color: (widget.color ?? Color(0xFFFFBD32)) - .withOpacity(widget.value ? 1 : 0), + color: (color ?? Color(0xFFFFBD32)).withOpacity(value ? 1 : 0), ), child: AnimatedOpacity( duration: Duration(milliseconds: 300), curve: Curves.fastOutSlowIn, - opacity: widget.value ? 1 : 0, + opacity: value ? 1 : 0, child: FittedBox( child: Padding( padding: EdgeInsets.all(5.w), child: Icon( Icons.check, - color: widget.color != null ? Colors.white : Colors.black, + color: color != null ? Colors.white : Colors.black, ), ), ), @@ -83,6 +76,6 @@ class _ASCheckBoxState extends State { @override Widget build(BuildContext context) { - return widget.checkStyle ? _renderCheck() : _renderDefault(); + return checkStyle ? _renderCheck() : _renderDefault(); } } diff --git a/lib/buttons/as_bottom_button.dart b/lib/buttons/as_bottom_button.dart index cdcdab6..15e4c8a 100644 --- a/lib/buttons/as_bottom_button.dart +++ b/lib/buttons/as_bottom_button.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:ansu_ui/styles/as_colors.dart'; -class ASBottomButton extends StatefulWidget { +class ASBottomButton extends StatelessWidget { ///动态组件,可以是字符或组件 final dynamic title; @@ -102,28 +102,23 @@ class ASBottomButton extends StatefulWidget { width = double.infinity, super(key: key); - @override - _ASBottomButtonState createState() => _ASBottomButtonState(); -} - -class _ASBottomButtonState extends State { @override Widget build(BuildContext context) { return Container( padding: EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom), - width: widget.width ?? double.infinity, + width: width ?? double.infinity, decoration: BoxDecoration( gradient: LinearGradient( - begin: widget.begin ?? Alignment.bottomRight, - end: widget.end ?? Alignment.topLeft, - colors: widget.colors ?? [widget.bgcolor, widget.bgcolor]), + begin: begin ?? Alignment.bottomRight, + end: end ?? Alignment.topLeft, + colors: colors ?? [bgcolor, bgcolor]), ), child: MaterialButton( - onPressed: widget.onPressed, - textColor: widget.textColor ?? kLightTextColor, - disabledColor: widget.disableColor, - disabledTextColor: widget.disableTextColor, - padding: widget.padding ?? + onPressed: onPressed, + textColor: textColor ?? kLightTextColor, + disabledColor: disableColor, + disabledTextColor: disableTextColor, + padding: padding ?? EdgeInsets.symmetric( vertical: 13.w, ), @@ -132,11 +127,11 @@ class _ASBottomButtonState extends State { highlightElevation: 0, focusElevation: 0, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, - child: widget.title is String - ? Text(widget.title, - style: widget.textStyle ?? + child: title is String + ? Text(title, + style: textStyle ?? TextStyle(fontSize: 20.sp, fontWeight: FontWeight.bold)) - : widget.title, + : title, ), ); }