【问题标题】:How to get value from textfield and display in textfromfield (another screen)如何从文本字段中获取值并在文本字段中显示(另一个屏幕)
【发布时间】:2019-06-02 14:52:17
【问题描述】:

我是新手,我试图从文本字段传递一个值,当我单击按钮提交时,将其显示在另一个屏幕的 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}"),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter textfield


    【解决方案1】:

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

    
       Navigator.push(context,
                MaterialPageRoute(builder: (context) => ResultPage(controllerTxt.text)));
    
    
    class ResultPage extends StatefulWidget {
      final String result;
     ResultPage(this.result);
    
    

    【讨论】:

    • 我删除 setState
    • 是的,你不需要 setState
    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多