嗨,我有一个文本表单字段,当表单被填充时,我想使用listview.builder在多个页面上输出值。当前,我可以在单个容器中输出数据,当我再次填充表单时,可以不添加其他容器,它只是更新当前容器的值 如果有人知道该怎么做,请帮忙
答案 0 :(得分:1)
完整的示例:
来源:
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]));
}))
])));
}
}