Flutter RaisedButton怎样禁用

本文介绍了如何在Flutter中使用RaisedButton组件根据条件禁用按钮,并改变其颜色。通过设置`onPressed`和`onLongPress`属性来控制按钮的启用状态,以及利用`disabledColor`属性改变禁用时的背景色。通过示例代码展示了如何在按钮点击事件中动态判断按钮是否可点击,并实现相应的颜色变化。
摘要由CSDN通过智能技术生成

在开发中,我们有时候需要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);
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值