简体   繁体   English

如何进行保存更改按钮?

[英]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.

相关问题 如何在 ListTile 中保存按钮的 state 并使按钮异步? - How to save a state of button in a ListTile and make the button async? 单击保存按钮时如何使此列可编辑并保存编辑数据? - how to make this column editable and save the edit data when click on save button? Flutter:按下提交按钮后,如何使我的字段成为必填项并保存我的下拉列表和文本数据? - Flutter: How to make my fields required and save my dropdown and text data after the submit button is pressed? App被杀死时如何保存更改? - How to save changes when App gets killed? 如何根据对 textformfield 的更改来更改按钮的文本 - How to change the text of a button based on changes to a textformfield 如何在本地的Flutter插件代码中进行更改 - How to make changes in flutter plugins code locally 如何从 flutter 中的另一个屏幕进行更改 - how to make changes from another screen in flutter 如何更改 DocumentSnapshot/QueryDocumentSnapshot (FlutterFire) - How to make changes to a DocumentSnapshot/QueryDocumentSnapshot (FlutterFire) 如何在颤动中制作滑动按钮? 不是开关按钮 - How to make a slide button in flutter? Not a switch button 将文本文件作为文件打开后如何保存对文本文件所做的更改? - How to save changes made to text file after opening it as a File?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM