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

Dart 如何在颤振中将数据从警报对话框传递到同一页面,dart,flutter,Dart,Flutter,我想从警报对话框传递数据。警报对话框包含文本字段,因此无论用户在文本字段上键入什么,该文本都应传递到主页面(屏幕)。下面是警报对话框的代码 Padding( padding: const EdgeInsets.only(left: 42.0), child: Align( alignment: Alignment.topCente

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

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

                ),
              ],
            );
          },
        );
      }
填充(
填充:仅限常量边集(左:42.0),
子对象:对齐(
对齐:alignment.topCenter,
子项:升起按钮(按下时:(){
_showDialog();
},
),
),
填充物(
填充:仅限常量边集(顶部:50.0),
子级:新文本(“//显示文本”);
void_showDialog(){
显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
//返回对话框类型的对象
返回警报对话框(
标题:新文本(“警报对话框标题”),
内容:TextField(
键盘类型:TextInputType.number,
装饰:输入装饰(
hintText:'输入号码'
),
)
,
行动:[
//通常是对话框底部的按钮
划船(
儿童:[
新扁平按钮(
子项:新文本(“取消”),
已按下:(){
Navigator.of(context.pop();
},
),
新扁平按钮(按下时:(){
},子项:新文本(“确定”)
],
),
],
);
},
);
}
编辑新解决方案:

// write this in your main page
String onMainPageText;
单击\u showdialog方法
文本(onMainPageText)

使用以下代码更改您的
\u 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"))
              ],
            ),
          ],
        );
      },
    );
  }


textfield有一个名为onChanged的参数:您可以使用它来传递函数

TextField(
            keyboardType: TextInputType.number,
            onChange: onChange
            decoration: InputDecoration(
                hintText: 'Enter the number'
            ),
          )
在主屏幕中使用以下命令:

 void onChange(String text) {
 //do stuff here with text like maybe setState
 }

谢谢,但是如何在主屏幕上显示我在textfield中键入的文本。我假设您正在从主屏幕调用
\u showDialog
方法,所以您需要做的就是编写代码
TextEditingController TextEditingController=new TextEditingController()
在您的主屏幕类中,并在我的回答中使用_showDialog方法,您可以使用
textEditingController访问文本。text
谢谢,但您正在logcat上显示此打印。我的问题是如何在主页上显示文本而不是在打印中显示。好的,在您希望显示的位置张贴代码和注释t、 我可以修改它,但基本上你可以通过“textEditingController.text”访问键入的文本字段。我已经更新了上面的代码,并对文本应该得到的位置进行了注释。
Padding(
                padding: const EdgeInsets.only(top: 50.0),
                  child: new Text(texEditingController.text););
TextField(
            keyboardType: TextInputType.number,
            onChange: onChange
            decoration: InputDecoration(
                hintText: 'Enter the number'
            ),
          )
 void onChange(String text) {
 //do stuff here with text like maybe setState
 }