如何将数据从警报对话框传递到颤振中的同一页面

moh*_*bil 2 dart flutter

我想从警报对话框传递数据。警报对话框包含文本字段,因此无论用户在文本字段上输入什么内容,文本都应该传递到主页(屏幕)。下面是警报对话框的代码

    Padding(
                          padding: const EdgeInsets.only(left: 42.0),
                          child: Align(
                            alignment: Alignment.topCenter,
                            child: RaisedButton(onPressed: (){
                                _showDialog();
                            },
                          ),
                        ),

Padding(
                padding: const EdgeInsets.only(top: 50.0),
                  child: new Text('// Displays text'););

    void _showDialog() {
        showDialog(
          context: context,
          builder: (BuildContext context) {
            // return object of type Dialog
            return AlertDialog(
              title: new Text("Alert Dialog title"),
              content: TextField(
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                    hintText: 'Enter the number'
                ),
              )
              ,
              actions: <Widget>[
                // usually buttons at the bottom of the dialog
                Row(
                  children: <Widget>[
                   new FlatButton(
                   child: new Text("Cancel"),
                    onPressed: () {
                    Navigator.of(context).pop();
                    },
                  ),
                    new FlatButton(onPressed: (){

                    }, child: new Text("OK"))
                  ],

                ),
              ],
            );
          },
        );
      }
Run Code Online (Sandbox Code Playgroud)

cip*_*nat 5

编辑新解决方案:

// write this in your main page
String onMainPageText;
Run Code Online (Sandbox Code Playgroud)

你可以在你的主页上这样显示!单击_showdialog方法中的确定后Text(onMainPageText)

_showDialog使用以下代码更改您的方法。

  void _showDialog() {
    String dialogText;
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Alert Dialog title"),
          content: TextField(
            onChanged: (String textTyped) {
              setState(() {
                dialogText = textTyped;
              });
            },
            keyboardType: TextInputType.number,
            decoration: InputDecoration(hintText: 'Enter the number'),
          ),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            Row(
              children: <Widget>[
                new FlatButton(
                  child: new Text("Cancel"),
                  onPressed: () {
                    setState(() {
                      onMainPageText = '';
                    });
                    Navigator.of(context).pop();
                  },
                ),
                new FlatButton(
                    onPressed: () {
                      setState(() {
                        onMainPageText = dialogText;
                      });
                      Navigator.of(context).pop();
                    },
                    child: new Text("OK"))
              ],
            ),
          ],
        );
      },
    );
  }

Run Code Online (Sandbox Code Playgroud)

旧答案:

创建一个全局 TextEditingController 将处理您的问题,您可以使用以下命令访问文本字段文本textEditingConroller.text

不要忘记在类中定义 textEditingController

class YourMainPageState extends State<YourMainPage>{
  TextEditingController textEditingController = new TextEditingController();

}
Run Code Online (Sandbox Code Playgroud)
  void _showDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Alert Dialog title"),
          content: TextField(
            controller: textEditingController,
            keyboardType: TextInputType.number,
            decoration: InputDecoration(hintText: 'Enter the number'),
          ),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            Row(
              children: <Widget>[
                new FlatButton(
                  child: new Text("Cancel"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                new FlatButton(onPressed: () {print(textEditingController.text);}, child: new Text("OK"))
              ],
            ),
          ],
        );
      },
    );
  }

Run Code Online (Sandbox Code Playgroud)

您可以使用该代码显示键入的文本:

Padding(
                padding: const EdgeInsets.only(top: 50.0),
                  child: new Text(texEditingController.text););
Run Code Online (Sandbox Code Playgroud)