開始時,我從填充 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: () {},
);
}
uj5u.com熱心網友回復:
你可以用 ValueListenableBuilder 包裹你的 ListTile,如下所示:
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 的狀態將被更新,不需要為完整的串列視圖設定狀態。
uj5u.com熱心網友回復:
要更新 Listtile(標題和副標題)中的資料,您需要使用 Stream 和 Streambuilder,它們將根據資料庫中的流更改更新資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/399136.html
上一篇:將C#Dictionary<string,string>傳遞到TypeScriptMap<string,string>
下一篇:從標簽頁傳遞資料時標簽不顯示文本
