首页 > 解决方案 > 如何从文本字段中获取值并在文本字段中显示(另一个屏幕)

问题描述

我是新手,我试图从文本字段传递一个值,当我单击按钮提交时,将其显示在另一个屏幕的文本表单字段中,我的问题,我不知道获取值的正确方法

一些代码: String txt = ""; TextEditingController controllerTxt = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text('Create'),
        actions: <Widget>[
          FlatButton(
            child: Text('Submit'),
            textColor: Colors.white,
            onPressed: () {
              setState(() {
                //txt = (controllerTxt.text);
                Navigator.pushNamed(context, '/ResultPage');
              });
            },
          ),
        ],
      ),
      body: new Container(
        child: new Column(
          children: <Widget>[
            new TextField(
              controller: controllerTxt,
              maxLines: 5,
              decoration: new InputDecoration(
              ),
            ),
          ],
        ),
      ),
    );
  }
}
class _ResultPageState extends State<ResultPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text('Result'),
      ),
      body: new Container(
        padding: EdgeInsets.all(10.0),
        child: new Column(
          children: <Widget>[
            new TextFormField(
              decoration: InputDecoration(
                labelText: 'Name :',
              ),
            ),
            new Text("${controllerTxt.text}"),
          ],
        ),
      ),
    );
  }
}

标签: fluttertextfield

解决方案


我通过构造函数传递数据做了同样的事情


   Navigator.push(context,
            MaterialPageRoute(builder: (context) => ResultPage(controllerTxt.text)));

class ResultPage extends StatefulWidget {
  final String result;
 ResultPage(this.result);


推荐阅读