flutter - How to use a ListView.builder to output a widget when a form is submitted
问题描述
Hi I have a text form field and when the form is filled I want to output the values on multiple pages with the listview.builder currently I can output the data’s in a single container and when I’d fill the form again it doesn’t add another container it just updates the current container’s values If anyone knows how to do it please help
解决方案
A complete example:
Source:
import 'package:flutter/material.dart';
class Demo extends StatefulWidget {
@override
_DemoState createState() => new _DemoState();
}
class _DemoState extends State<Demo> {
List<String> _items = new List(); // to store comments
final myController = TextEditingController();
void _addComment() {
if (myController.text.isNotEmpty) {
// check if the comments text input is not empty
setState(() {
_items.add(myController.text); // add new commnet to the existing list
});
myController.clear(); // clear the text from the input
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("DEMO")),
body: Container(
padding: EdgeInsets.all(15),
child: Column(children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: TextField(
style: TextStyle(color: Colors.black),
controller: myController,
maxLines: 5,
keyboardType: TextInputType.multiline,
)),
SizedBox(width: 15),
InkWell(
onTap: () {
_addComment();
},
child: Icon(Icons.send))
]),
SizedBox(height: 10),
Expanded(
child: ListView.builder(
itemCount: _items.length,
itemBuilder: (BuildContext ctxt, int index) {
return Padding(
padding: EdgeInsets.all(10),
child: Text("${(index + 1)}. " + _items[index]));
}))
])));
}
}
推荐阅读
- fonts - Django xhtml2pdf 有孟加拉字体问题(损坏的格式)
- spring - 匹配任何文本到搜索框中
- r - 如何正确转义 Rmarkdown 文档中的 XML 特殊字符?
- sql - SQL Server 上所有数据库中的孤立用户
- couchdb - 如何从给定的 couchdb 数据库中检索最旧的“未删除”文档
- rubygems - bundle install --with development 也是从“test”和“production”组安装 gems
- python - 散景:CheckboxButtonGroup 的个别颜色
- performance - 如何在页面加载后导入第三方JS文件以获得良好的页面分数
- javascript - 如何通过单击输入标签周围的 + 和 - 按钮来增加和减少跨度标签中的添加?
- active-directory - Jupyterhub AD 集成错误:查找属性“sAMAccountName”时未找到用户条目