flutter - Flutter:如何将 Textfield 输入放入列表中以构建 ListView.builder
问题描述
几天以来,我一直在尝试构建一个 listviewbuilder。为此,我需要来自另一个屏幕的文本字段输入。我看了很多教程和问题,但它不起作用。我试图将来自多个文本字段的输入放入多个列表中以构建一个 Listview 构建器。如果我可以在按下平面按钮时保存所有文本字段输入,那将是最好的。我希望有一个人可以帮助我。
第一页
List<String> time = ["8:00"];List<String>
List<String> where = ["Am See"];
List<String> who = ["Eric"];
List<String> when = ["Donnerstag 21.4.21"];
body: SingleChildScrollView(
physics: ScrollPhysics(),
child: Column(children: [
Upperscreen(size: size),
ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: where.length,
itemBuilder: (BuildContext context, int Index) {
return Column(children: [
SizedBox(
height: 40,
),
Container(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Meet1()));
},
child: Container(
width: size.width * 0.9,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(70)),
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomRight,
colors: [
Colors.green,
Colors.orange,
],
),
),
child: Column(children: <Widget>[
SizedBox(
height: 10,
),
Padding(
padding: EdgeInsets.all(20),
child: Column(
children: <Widget>[
Text(
time[Index],
style: TextStyle(
color: Colors.white,
fontSize: 40,
fontWeight:
FontWeight.bold),
),
SizedBox(
height: 10,
),
Text(
who[Index],
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight:
FontWeight.bold),
),
Text(
when[Index],
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight:
FontWeight.bold),
),
Text(
where[Index],
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight:
FontWeight.bold),
第二页
child: Column(children: <Widget>[
SizedBox(
height: 10,
),
Padding(
padding: EdgeInsets.all(20),
child: Column(
children: <Widget>[
TextField(decoration: InputDecoration(hintText: " Time ")),
SizedBox(
height: 10,
),
TextField(
decoration: InputDecoration(hintText: " Who "),
),
SizedBox(
height: 10,
),
TextField(
decoration: InputDecoration(hintText: " Date "),
),
SizedBox(
height: 10,
),
TextField(
decoration: InputDecoration(hintText: " Where "),
),
SizedBox(height: 10)
],
),
),
]));
这里的 Flatbutton 添加所有。
return FlatButton(
child: Icon(
Icons.check_circle_outline_rounded,
color: Colors.green,
size: 120,
),
onPressed: () {
Navigator.of(context).popUntil((route) => route.isFirst);
},
解决方案
使用 a TextEditingController()
,就像这样 -
TextEditingController() myController = TextEditingController();
然后将此控制器分配给控制器属性TextField()
-
TextField(
controller: myController,
),
然后用于myController.text
从中检索文本TextField()
,并将其作为String
参数传递给其他页面 -
例子 -
class Screen1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
//....
body: FlatButton(
child: Icon(
Icons.check_circle_outline_rounded,
color: Colors.green,
size: 120,
),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (builder) {
return Screen2(text: myController.text);
}));
},
//....
),
);
}
}
第二页 -
class Screen2 extends StatelessWidget {
String text;
Screen2({this.text});
@override
Widget build(BuildContext context) {
return Scaffold(
//....
body: Text(text),
);
}
}
现在,在这里我只使用了 1 个参数“文本”。您可以根据您的要求使用多个参数,例如“text1”、“text2”、“text3”等,并为此使用尽可能多TextEditingController()
的参数。
*****还要注意使用 ofFlatButton()
是贬值的,你可以用 aTextButton()
代替
推荐阅读
- docker - docker 卷位于哪里?
- macos - 如何在 MacOS 中设置目录 /var/log 的权限
- python - 是否有有效的方法来验证许多变量并发出适当的通知?
- xml - 使用 SQL 更改 XML 中特定节点的属性值
- soap - 通过 ONVIF 以编程方式访问海康威视摄像机时的 GetProfiles 问题
- angular - 带有可选参数的角度计算
- c++ - VS10 C++ 错误 C2833:“操作符字符串”不是可识别的操作符或类型
- java - 如何保持对 Fragment 的绑定 -> Intent -> 返回 Fragment
- angular - 使用 BottomNavigation(不是 TabView)的选项卡导航崩溃
- python - 如何在networkx图的图中绘制矩形?