如何在Flutter中将数据从警报对话框传递到同一页面 [英] How to pass data from alert dialog to same page in flutter

查看:79
本文介绍了如何在Flutter中将数据从警报对话框传递到同一页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to pass data from alert dialog.Alert dialog contains textfield,so whatever the user type on the textfield that text should pass to the main page (screen).Below is the code of the alert dialog

    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"))
                  ],

                ),
              ],
            );
          },
        );
      }


推荐答案

编辑新解决方案:

// write this in your main page
String onMainPageText;

您可以在首页的上显示以下内容!!在您的 _showdialog 方法 Text(onMainPageText)

you can display like this in on your main page! after clicking the okey in your _showdialog method Text(onMainPageText)

更改 _showDialog 方法,并包含以下代码。

change your _showDialog method with the following code.

  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"))
              ],
            ),
          ],
        );
      },
    );
  }

旧答案:

创建一个全局TextEditingController将处理您的问题,您可以使用 textEditingConroller.text

create a global TextEditingController will handle your problem you can access the text field text with textEditingConroller.text

不要忘记在您的类中定义textEditingController

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

}



  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"))
              ],
            ),
          ],
        );
      },
    );
  }

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

You can display typed text with that code :

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

这篇关于如何在Flutter中将数据从警报对话框传递到同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆