在列表之外显示数据在技术上是不可能的吗?我在网上搜索了一下,但根本找不到答案--_-
除了区段ListViewBuilder
之外,我还想显示列表中数据行的值.
输出:
[ListView生成器屏幕]
姓名:你,年龄:20姓名:他,年龄:20
一张输出照片在那里. enter image description here个
String name = userList[index].name;
int? age = userList[index].age;
class _Passenger extends State<Passenger> {
TextEditingController nameController = TextEditingController();
TextEditingController ageController = TextEditingController();
int currentIndex = 0;
final form = GlobalKey<FormState>();
bool update = false;
final User user = User(name: "", age: int.tryParse(''));
List<User> userList = [
User(name: "You", age: 20),
User(name: "Him", age: 20),
];
String text = '';
int? number = int.tryParse('');
@override
Widget build(BuildContext context) {
return MaterialApp( debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(children: <Widget>[
Column(
children: <Widget>[
Container(
height: 550,
decoration: BoxDecoration(border: Border.all(color: Colors.black)),
child: ListView.builder(
itemCount: userList.length,
itemBuilder: (context, index) {
String name = userList[index].name;
int? age = userList[index].age;
return SizedBox(
width: 20,
child: Card(
color: Colors.grey,
child: Padding(
padding: const EdgeInsets.all(2.0),
child: ListTile(
title: Text( "Name: $name Age: $age"),
))),
);
}),
),
],
),
Container( child: Text("Display the data here, How?") ),
//Add Button
Container(
width: 150,
height: 50,
margin: const EdgeInsets.all(10),
child: ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (context) => SimpleDialog(children: [
TextField(
decoration: const InputDecoration(labelText: 'Name'),
onChanged: (value) {
setState(() {
text = value;
});
},
),
TextField(
keyboardType: TextInputType.number,
decoration: const InputDecoration(labelText: 'Age'),
onChanged: (value) {
setState(() {
number = int.parse(value);
});
},
),
ElevatedButton(
onPressed: () {
setState(() {
userList.add(User(name: text, age: number));
});
},
child: const Text('Add'))
]));
},
child: const Text("Add"),
)),
])));
}
}
class User {
String name;
int? age;
User({
required this.name,
this.age,
});
}