如何在Flutter中刷新AlertDialog?

Nit*_*euq 13 flutter flutter-layout flutter-alertdialog

目前,我有AlertDialog一个IconButton。用户可以单击IconButton,每次单击都有两种颜色。问题是我需要关闭AlertDialog并重新打开以查看颜色图标的状态更改。我想在用户单击时立即更改IconButton的颜色。

这是代码:

bool pressphone = false;
//....
new IconButton(
   icon: new Icon(Icons.phone),
   color: pressphone ? Colors.grey : Colors.green,
   onPressed: () => setState(() => pressphone = !pressphone),
),
Run Code Online (Sandbox Code Playgroud)

Geo*_*rge 50

在 AlertDialogStatefulBuildercontent部分中使用 a 。甚至StatefulBuilder 文档实际上也有一个带有对话框的示例。

它的作用是为您提供一个新的contextsetState函数以在需要时重建。

示例代码:

showDialog(
  context: context,
  builder: (BuildContext context) {

    int selectedRadio = 0; // Declare your variable outside the builder
    
    return AlertDialog( 
      content: StatefulBuilder(  // You need this, notice the parameters below:
        builder: (BuildContext context, StateSetter setState) {
          return Column(  // Then, the content of your dialog.
            mainAxisSize: MainAxisSize.min,
            children: List<Widget>.generate(4, (int index) {
              return Radio<int>(
                value: index,
                groupValue: selectedRadio,
                onChanged: (int value) {
                  // Whenever you need, call setState on your variable
                  setState(() => selectedRadio = value);
                },
              );
            }),
          );
        },
      ),
    );
  },
);
Run Code Online (Sandbox Code Playgroud)

正如我所提到的,这就是showDialog 文档中所说的:

[...] 构建器返回的小部件与最初调用 showDialog 的位置不共享上下文。如果对话框需要动态更新,请使用 StatefulBuilder 或自定义 StatefulWidget


And*_*ris 29

首先你需要使用StatefulBuilder. 然后我设置_setState变量,甚至可以在外面使用StatefulBuilder,以设置新状态。

StateSetter _setState;
String _demoText = "test";

showDialog(
  context: context,
  builder: (BuildContext context) {

    return AlertDialog( 
      content: StatefulBuilder(  // You need this, notice the parameters below:
        builder: (BuildContext context, StateSetter setState) {
          _setState = setState;
          return Text(_demoText);
        },
      ),
    );
  },
);
Run Code Online (Sandbox Code Playgroud)

_setState 的使用方式与 setState 方法相同。例如像这样:

_setState(() {
    _demoText = "new test text";
});
Run Code Online (Sandbox Code Playgroud)


azi*_*iza 16

这是因为您需要自己放置并AlertDialog在其StatefulWidget颜色上移动所有状态操纵逻辑。

更新:

在此处输入图片说明

void main() => runApp(MaterialApp(home: Home()));

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
            child: RaisedButton(
      child: Text('Open Dialog'),
      onPressed: () {
        showDialog(
            context: context,
            builder: (_) {
              return MyDialog();
            });
      },
    )));
  }
}

class MyDialog extends StatefulWidget {
  @override
  _MyDialogState createState() => new _MyDialogState();
}

class _MyDialogState extends State<MyDialog> {
  Color _c = Colors.redAccent;
  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      content: Container(
        color: _c,
        height: 20.0,
        width: 20.0,
      ),
      actions: <Widget>[
        FlatButton(
            child: Text('Switch'),
            onPressed: () => setState(() {
                  _c == Colors.redAccent
                      ? _c = Colors.blueAccent
                      : _c = Colors.redAccent;
                }))
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


mal*_*m91 11

showDialog(
  context: context,
  builder: (context) {
    String contentText = "Content of Dialog";
    return StatefulBuilder(
      builder: (context, setState) {
        return AlertDialog(
          title: Text("Title of Dialog"),
          content: Text(contentText),
          actions: <Widget>[
            FlatButton(
              onPressed: () => Navigator.pop(context),
              child: Text("Cancel"),
            ),
            FlatButton(
              onPressed: () {
                setState(() {
                  contentText = "Changed Content of Dialog";
                });
              },
              child: Text("Change"),
            ),
          ],
        );
      },
    );
  },
);
Run Code Online (Sandbox Code Playgroud)

  • @MutluSimsek当您尝试设置在 StatefulBuilder/StatefulWidget 外部声明的变量时,它不起作用 (4认同)
  • 这是正确的答案,我从没想过你可以 'subClass\subOverload' [setState] 活救星 (2认同)
  • 也不为我工作。有谁可以解释为什么它对某些人不起作用? (2认同)
  • 我使用的是 Dart 2.8.1 和 Flutter 1.19.0-0.0.pre,它在 WEB 中按预期工作,谢谢:) (2认同)
  • 谢谢。它对我有用。但是,如果我从外部单独的方法在有状态构建器中添加任何子项,它就不起作用。然后,如果我将孩子直接添加到有状态构建器中,它就会起作用。更改有状态构建器内的变量是有效的。如果它位于有状态构建器之外,那么它就不起作用。 (2认同)