如何在抖动警报框中更新按钮文本

时间:2019-07-14 14:00:00

标签: flutter

我扑朔迷离地有一个警告框,提示用户输入名称和日期。名称是一个简单的TextField,通过单击一个按钮以打开日期选择器来输入日期。最初该按钮以当前日期为文本,我希望此日期能在所选日期之前更新,但无法实现。不过,当我返回到文本输入时,它确实会更新。

感谢您的帮助

我当前的代码如下。

PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;

memset(&piProcInfo, 0, sizeof(PROCESS_INFORMATION));
memset(&siStartInfo, 0, sizeof(STARTUPINFO));

siStartInfo.cb = sizeof(STARTUPINFO); 

CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &siStartInfo, &piProcInfo);

对不起,评论。如果能够以更好的方式回答问题,那将是很好的。无论如何,我希望我可以创建一个更通用的“状态对话框”,所以我创建了类似的东西。

Future<bool> getNameDate(BuildContext context)
  async
  {
    return showDialog<bool>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Save Event"),
          content: new Column(
            children: <Widget>[
              new Expanded(
                child: new TextField(
                  autofocus: true,
                  decoration: new InputDecoration(
                      labelText: "Event Name"
                  ),
                  onChanged: (value) {
                    name = value;
                  },
                  onSubmitted: (value) {
                  },
                ),
              ),
              new Expanded(
                  child: new Row(
                    children: <Widget>[
                      new Text("Date of Event "),
                      new RaisedButton(
                          child: Text(DateFormat("dd/MM/yyyy").format(when)),
                          onPressed: () async {
                            DateTime picked = await showDatePicker(
                              context: context,
                              initialDate: when,
                              firstDate: DateTime(2015, 8),
                              lastDate: DateTime(2101),
                            );
                            setState(() {
                              when = picked ;
                            });
                          }),
                    ],
                  )
              )
            ],
          ),
          actions: <Widget>[
            new FlatButton(
                child: const Text('OK'),
                onPressed: () {
                  Navigator.of(context).pop(true);
                }),
            new FlatButton(
                child: const Text("CANCEL"),
                onPressed: () {
                  Navigator.of(context).pop(false);
                }),
          ],
        );
      },
    );
  }

然后在我现有的函数中将其更改为..

class StateDialog extends StatefulWidget
{
  final AlertDialog dialog;
  StateDialog({Key key, @required this.dialog}) : super (key: key);

  @override
  StateDialogState createState() => StateDialogState(dialog: dialog);

}

class StateDialogState extends State<StateDialog>
{
  final AlertDialog dialog;
  StateDialogState({@required this.dialog});
  @override
  Widget build(_) {
    return dialog;
  }
}

但是,这仍然不会更新日期按钮中的文本。我很高兴知道为什么。

1 个答案:

答案 0 :(得分:0)

这是因为setState()方法实际上不是在更新alertDialog,而是在树上的小部件。 alertDialog没有自己的状态。您可以签出this article,如果需要,这里是一个代码示例,可以根据需要进行一些更改:

尝试一下:我对其进行了测试,并选择了一个新日期后,按钮的文本将更新为您刚选择的正确日期!

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

void main() {
  runApp(new MaterialApp(
    home: new MyHomePage(),
  ));
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _selectedIndex = 0;

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("StackoverFlow"),
      ),
      body: Container(),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await _dialogCall(context);
        },
      ),
    );
  }

  Future<void> _dialogCall(BuildContext context) {
    return showDialog(
        context: context,
        builder: (BuildContext context) {
          return MyDialog();
        });
  }
}

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

class _MyDialogState extends State<MyDialog> {
  String name = "";
  bool button = false;
  DateTime when = DateTime.now();

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text("Save Event"),
      content: new Column(
        children: <Widget>[
          new Expanded(
            child: new TextField(
              autofocus: true,
              decoration: new InputDecoration(labelText: "Event Name"),
              onChanged: (value) {
                name = value;
              },
              onSubmitted: (value) {},
            ),
          ),
          new Expanded(
              child: new Row(
            children: <Widget>[
              new Text("Date of Event "),
              new RaisedButton(
                  child: Text(DateFormat("dd/MM/yyyy").format(when)),
                  onPressed: () async {
                    DateTime picked = await showDatePicker(
                      context: context,
                      initialDate: when,
                      firstDate: DateTime(2015, 8),
                      lastDate: DateTime(2101),
                    );
                    setState(() {
                      when = picked;
                    });
                  }),
            ],
          ))
        ],
      ),
      actions: <Widget>[
        new FlatButton(
            child: const Text('OK'),
            onPressed: () {
              Navigator.of(context).pop(true);
            }),
        new FlatButton(
            child: const Text("CANCEL"),
            onPressed: () {
              Navigator.of(context).pop(false);
            }),
      ],
    );
  }

  Future<bool> getNameDate(BuildContext context) async {
    return showDialog<bool>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Save Event"),
          content: new Column(
            children: <Widget>[
              new Expanded(
                child: new TextField(
                  autofocus: true,
                  decoration: new InputDecoration(labelText: "Event Name"),
                  onChanged: (value) {
                    name = value;
                  },
                  onSubmitted: (value) {},
                ),
              ),
              new Expanded(
                  child: new Row(
                children: <Widget>[
                  new Text("Date of Event "),
                  new RaisedButton(
                      child: Text(DateFormat("dd/MM/yyyy").format(when)),
                      onPressed: () async {
                        DateTime picked = await showDatePicker(
                          context: context,
                          initialDate: when,
                          firstDate: DateTime(2015, 8),
                          lastDate: DateTime(2101),
                        );
                        setState(() {
                          when = picked;
                        });
                      }),
                ],
              ))
            ],
          ),
          actions: <Widget>[
            new FlatButton(
                child: const Text('OK'),
                onPressed: () {
                  Navigator.of(context).pop(true);
                }),
            new FlatButton(
                child: const Text("CANCEL"),
                onPressed: () {
                  Navigator.of(context).pop(false);
                }),
          ],
        );
      },
    );
  }
}