我刚开始掌握Flutter的窍门,但我不知道如何设置按钮的启用状态。
从文档中,它说将onPressed设置为null来禁用按钮,并给它一个值来启用它。如果按钮在生命周期中继续处于相同的状态,这是没问题的。
我得到的印象是,我需要创建一个自定义的有状态小部件,它将允许我以某种方式更新按钮的启用状态(或onPressed回调)。
我的问题是我该怎么做?这似乎是一个非常简单的要求,但我在文档中找不到任何关于如何做到这一点的东西。
谢谢。
我刚开始掌握Flutter的窍门,但我不知道如何设置按钮的启用状态。
从文档中,它说将onPressed设置为null来禁用按钮,并给它一个值来启用它。如果按钮在生命周期中继续处于相同的状态,这是没问题的。
我得到的印象是,我需要创建一个自定义的有状态小部件,它将允许我以某种方式更新按钮的启用状态(或onPressed回调)。
我的问题是我该怎么做?这似乎是一个非常简单的要求,但我在文档中找不到任何关于如何做到这一点的东西。
谢谢。
当前回答
为了禁用任何按钮,如FlatButton, RaisedButton, MaterialButton, IconButton等,你需要做的就是将onPressed和onLongPress属性设置为null。下面是一些按钮的简单示例:
FlatButton (Enabled)
FlatButton(
onPressed: (){},
onLongPress: null, // Set one as NOT null is enough to enable the button
textColor: Colors.black,
disabledColor: Colors.orange,
disabledTextColor: Colors.white,
child: Text('Flat Button'),
),
FlatButton(禁用)
FlatButton(
onPressed: null,
onLongPress: null,
textColor: Colors.black,
disabledColor: Colors.orange,
disabledTextColor: Colors.white,
child: Text('Flat Button'),
),
RaisedButton(启用)
RaisedButton(
onPressed: (){},
onLongPress: null, // Set one as NOT null is enough to enable the button
// For when the button is enabled
color: Colors.lightBlueAccent,
textColor: Colors.black,
splashColor: Colors.blue,
elevation: 8.0,
// For when the button is disabled
disabledTextColor: Colors.white,
disabledColor: Colors.orange,
disabledElevation: 0.0,
child: Text('Raised Button'),
),
RaisedButton(禁用)
RaisedButton(
onPressed: null,
onLongPress: null,
// For when the button is enabled
color: Colors.lightBlueAccent,
textColor: Colors.black,
splashColor: Colors.blue,
elevation: 8.0,
// For when the button is disabled
disabledTextColor: Colors.white,
disabledColor: Colors.orange,
disabledElevation: 0.0,
child: Text('Raised Button'),
),
IconButton (Enabled)
IconButton(
onPressed: () {},
icon: Icon(Icons.card_giftcard_rounded),
color: Colors.lightBlueAccent,
disabledColor: Colors.orange,
),
IconButton(禁用)
IconButton(
onPressed: null,
icon: Icon(Icons.card_giftcard_rounded),
color: Colors.lightBlueAccent,
disabledColor: Colors.orange,
),
注意:一些按钮,如IconButton只有onPressed属性。
其他回答
简单的答案是onPressed: null给出一个禁用按钮。
在我看来,这是最简单的方法:
RaisedButton(
child: Text("PRESS BUTTON"),
onPressed: booleanCondition
? () => myTapCallback()
: null
)
你也可以使用吸收指针,你可以用下面的方式使用它:
AbsorbPointer(
absorbing: true, // by default is true
child: RaisedButton(
onPressed: (){
print('pending to implement onPressed function');
},
child: Text("Button Click!!!"),
),
),
如果您想了解有关此小部件的更多信息,可以查看以下链接Flutter Docs
有两种方法:
1- https://stackoverflow.com/a/49354576/5499531
2-你可以使用MaterialStatesController:
final _statesController = MaterialStatesController();
然后将状态更改为:
_statesController.update(
MaterialState.disabled,
true, // or false depending on your logic
);
在你的按钮上
ElevatedButton(
onPressed: _onPressed,
statesController: _statesController,
child: Text("Awesome"),
),
此外,你可以改变按钮的风格,当禁用: 在主题设置中:
....
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: colors.primary500, // set your own color
textStyle: button, // set your own style
onPrimary: colors.onPrimary100, // set your own color
enableFeedback: true,
disabledBackgroundColor: colors.primary300, // set your own color
disabledForegroundColor: colors.primary300, // set your own color
disabledMouseCursor: SystemMouseCursors.forbidden, // when is disable the change the cursor type
),
),
...
我喜欢为此使用flutter_mobx,并对状态进行处理。
接下来我使用一个观察者:
Container(child: Observer(builder: (_) {
var method;
if (!controller.isDisabledButton) method = controller.methodController;
return RaiseButton(child: Text('Test') onPressed: method);
}));
控制器侧:
@observable
bool isDisabledButton = true;
然后在控件中,您可以随心所欲地操作这个变量。
参考文献。: Flutter mobx