首先,我从填充ListView的数据库加载数据。这些数据显示在ListView标题和副标题上。当用户点击列表中的某一项时,会弹出带有字段的showModalBottomSheet更新列表(索引)。此更新已成功执行,但在showModalBottomSheet结束时,每个ListView项上的值将刷新为默认值(来自数据库的数据)。
请注意,如果不将ListView刷新为初始数据值,如何更新ListView项?
Widget _buildSubjects(BuildContext context, int index) {
response = getFieldData(_snapshot!.data[index]);
return ListTile(
trailing: IconButton(
icon: Icon(Icons.add),
onPressed: () {
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) {
return SingleChildScrollView(
child: Container(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: Text(
_snapshot!.data[index]['name']
.toString()
.toUpperCase(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
),
),
Form(
key: _scoreForm,
child: Column(
children: [
scoreFields(_snapshot!.data[index]),
SizedBox(
height: 10.0,
),
ElevatedButton(
onPressed: () {
if (!_scoreForm.currentState!.validate())
return;
_scoreForm.currentState!.save();
setState(() {
response = "New Value";
});
//close bottomsheet
Navigator.pop(context);
},
child: Text("Save Score"),
),
],
),
),
],
),
),
);
},
);
},
),
title: Text(
_snapshot!.data[index]['name'].toString().toUpperCase(),
style: TextStyle(
fontWeight: FontWeight.w400,
),
),
subtitle: Text(
response,
),
onTap: () {},
);
}发布于 2021-12-31 10:52:25
您可以使用ListTile包装ValueListenableBuilder,如下所示:
ValueNotifier<bool> newData = ValueNotifier(false);
ValueListenableBuilder<bool>(
valueListenable: newData,
builder: (context, value, child) {
return ListTile(
trailing: IconButton(
icon: Icon(Icons.add), //... rest of your code而不是打电话
setState(() {
response = "New Value";
});在没有setState的情况下调用下面
response = "New Value";
newData.value = !newData.value;因此,现在将更新ListTile的状态,不需要setState来获得完整的列表视图。
发布于 2021-12-31 10:35:25
要更新列表(title和subtitle)中的数据,您需要使用Stream和Streambuilder,这将根据数据库中的流更改来更新数据。
https://stackoverflow.com/questions/70541166
复制相似问题