[英]How to make a save changes button?
I have an AlertDialog that I use as a settings window, when the user opens it, the Apply button is not active, I would like that when the settings change, the button becomes active and saves the changes.我有一个 AlertDialog 用作设置 window,当用户打开它时,应用按钮不活动,我希望当设置更改时,按钮变为活动并保存更改。 How can I do this?
我怎样才能做到这一点?
showAlertDialogSettings(BuildContext context, state) { showAlertDialogSettings(BuildContext 上下文,状态){
Widget okButton = TextButton(
child: Text("Apply"),
onPressed: null,
);
Widget cancelButton = TextButton(
child: Text("Close"),
onPressed:() => Navigator.pop(context),
);
AlertDialog alert = AlertDialog(
title: Center(child: Text("Settings")),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Sound:'),
SwitchWidget(),
],),
SizedBox(
height: 48,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Difficulty:'),
Padding(
padding: const EdgeInsets.only(right: 5),
child: DropDownButtonSettingsWidget()
),
],),
),
],
),
actions: [
okButton,
cancelButton,
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return BackdropFilter (
filter: ImageFilter.blur(sigmaX: 6, sigmaY: 6),
child: alert );
},
);
}
You will need to maintain state of both the sound and the difficulty values (there are many ways to tackle this), though the simplest would be to split out the "body" of the AlertDialog
to be a StatefulWidget
to contain its state .您需要维护声音和难度值的 state(有很多方法可以解决这个问题),尽管最简单的方法是将 AlertDialog 的“主体”拆分为
StatefulWidget
以包含其 state AlertDialog
From there, you can check whether values have changed and update the view state to enable the apply button.从那里,您可以检查值是否已更改并更新视图 state 以启用应用按钮。
It's highly recommended to not mix business logic with UI logic, so this widget shouldn't actually do any of the saving.强烈建议不要将业务逻辑与 UI 逻辑混合,因此该小部件实际上不应进行任何保存。 Inputs can be encapsulated within a class, and then this class can be passed back via
Navigator.of(context).pop(T)
(where T
is your value class, see docs ) upon closing the dialog from the apply button callback.输入可以封装在 class 中,然后这个 class 可以通过
Navigator.of(context).pop(T)
传回(其中T
是您的值class关闭应用按钮后的回调)。
// Input passed back via `pop(T)` can be retrieved via:
final input = await showDialog(MyAlertDialog());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.