如何从文本字段中获取价值并在textfromfield中显示(另一个屏幕)

时间:2019-06-02 14:52:17

标签: flutter textfield

我是新手,我试图从文本字段传递值,然后单击提交按钮,在另一个屏幕的textformfield中显示它,这是我的问题,我不知道获取价值的正确方法

某些代码:       字符串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}"),
          ],
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

我通过构造器传递数据


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

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