flutter - 如何从文本字段中获取值并在文本字段中显示(另一个屏幕)
问题描述
我是新手,我试图从文本字段传递一个值,当我单击按钮提交时,将其显示在另一个屏幕的文本表单字段中,我的问题,我不知道获取值的正确方法
一些代码: 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}"),
],
),
),
);
}
}
解决方案
我通过构造函数传递数据做了同样的事情
Navigator.push(context,
MaterialPageRoute(builder: (context) => ResultPage(controllerTxt.text)));
class ResultPage extends StatefulWidget {
final String result;
ResultPage(this.result);
推荐阅读
- ajax - Blazor Webassembly - 从浏览器到服务器 API 的请求返回 302 HTTP 代码(重定向) - 如何处理
- puppeteer - 单击输入按钮未按预期工作
- alteryx - 如何将其生成为 alteryx 表中的新列?
- centos7 - 如何在 Centos 7 上安装 Lmms
- python - 每天迭代,计算前 3 小时和最后 3 小时的平均价格,并在 Python 中取这些平均值的差值
- python - 如何使用 Beautiful Soup 更快地提出请求?
- javascript - 是否可以在 Jenkins 的构建后操作中执行 JavaScript 脚本?
- visual-studio - 通过工具 > 外部工具在包管理器控制台上执行脚本
- excel - 通过“ListBox”传递数据
- django - 如何从 html 更新 django 数据库?