在开发中,我们有时候需要RaisedButton组件禁用,满足条件后可以点击。
const RaisedButton({ Key key, @required VoidCallback onPressed, VoidCallback onLongPress, ValueChanged<bool> onHighlightChanged, MouseCursor mouseCursor, ButtonTextTheme textTheme, Color textColor, Color disabledTextColor, Color color, Color disabledColor, Color focusColor, Color hoverColor, Color highlightColor, Color splashColor, Brightness colorBrightness, double elevation, double focusElevation, double hoverElevation, double highlightElevation, double disabledElevation, EdgeInsetsGeometry padding, VisualDensity visualDensity, ShapeBorder shape, Clip clipBehavior = Clip.none, FocusNode focusNode, bool autofocus = false, MaterialTapTargetSize materialTapTargetSize, Duration animationDuration, Widget child, })
下面我们来看看常用属性
我们看下disabledColor属性源码中的描述:
/// The fill color of the button when the button is disabled. /// /// The default value of this color is the theme's disabled color, /// [ThemeData.disabledColor]. /// /// See also: /// /// * [color] - the fill color of the button when the button is [enabled]. final Color disabledColor; /// Whether the button is enabled or disabled. /// /// Buttons are disabled by default. To enable a button, set its [onPressed] /// or [onLongPress] properties to a non-null value. bool get enabled => onPressed != null || onLongPress != null;
按钮是否enable就看onPressed或者onLongPress是不是为空,不为空就可点击,为空则不能点击。
那么,如果想禁用按钮使按钮变色的话,只需在点击事件处理即可,例如:
RaisedButton( disabledColor: Res.COLOR.text_999999, color: Res.COLOR.title_bg, padding: EdgeInsets.only(top: 16, bottom: 16), child: const Text('提交',style: TextStyle(color: Colors.white)), onPressed: _submit(), ) void Function() _submit() { return selectedContactList.isNotEmpty ? () => onBtnPressed() : null; } void onBtnPressed() { var params = <String, dynamic>{ 'select_data': '123' }; FlutterBoost.singleton.closeCurrent(result: params); }