【发布时间】:2021-05-12 08:17:31
【问题描述】:
我正在开发 Flutter 应用程序,我在 BottomSheet 弹出窗口中使用 ListView.builder 类。我需要开发一些东西,用户可以从 ListView 中选择多个数据。
为此,我使用Map mapProfCredsT = new Map<dynamic, bool>(); 来存储列表视图中每个单元格的点击事件。
另外,我在每个点击事件上都使用了setState() {},以便我的数据应该更新。但即使在此之后,我也无法在点击事件后更新每个单元格的 UI。
请考虑以下代码sn-p:
Map mapProfCredsT = new Map<dynamic, bool>();
在 API 调用的响应中,我将所有标志添加为每个 ID 为 false,
for (int i = 0; i < arrProfCredsCount; i++) {
int id_key = respProfCreds['payload']['user_type'][i]['id'];
mapProfCredsT[id_key] = false; }
现在容器和图标的颜色取决于容器的点击事件如下:
Widget build(BuildContext context) {
return Material(
type: MaterialType.transparency,
child: Scaffold(
key: scaffoldState,
body: SafeArea(
child: Container(
height: double.infinity,
width: double.infinity,
color: Colors.teal,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.all(10.0),
width: double.infinity,
child: Text(
'BottomSheet Sample',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 30.0,
color: Colors.black,
),
),
),
Container(
padding: EdgeInsets.all(20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
GestureDetector(
onTap: () {
setState(() {
if (isChanged) {
isChanged = false;
} else {
isChanged = true;
}
});
},
child: Container(
color: Colors.white,
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Text('Change'),
),
),
GestureDetector(
onTap: () {
// scaffoldState.currentState.showBottomSheet(
// scaffoldState.currentState.showBottomSheet(
showModalBottomSheet(
context: context,
builder: (builder) {
return new Container(
color: Colors.white,
height: 70.0.h,
child: Column(
children: <Widget>[
Container(
height: 10.0.w,
color: Colors.white,
child: Center(
child: Text(
'Professional Creds',
),
),
),
Expanded(
child: Container(
child: ListView.builder(
itemCount: arrProfCredsCount,
itemBuilder: (context, index) {
return ConstrainedBox(
constraints: BoxConstraints(
minHeight: 15.0.w,
),
child: GestureDetector(
onTap: () {
var profID = respProfCreds['payload']['user_type'][index]['id'];
print('Position on bottomsheet : $profID');
setState(() {
// Update text data..
respProfCreds['payload']['user_type'][index]['title'] =
'Test ${respProfCreds['payload']['user_type'][index]['id']}';
});
},
child: Container(
margin: EdgeInsets.all(2.0.w),
// color: Colors.tealAccent,
// color: mapProfCredsT[respProfCreds['payload']['user_type'][index]['id']]? themeYellow: Colors.blueGrey,
color: isChanged ? themeYellow : Colors.blueGrey,
child: Center(
child: Text(
'${respProfCreds['payload']['user_type'][index]['title']}',
textAlign: TextAlign.start,
style: kDataSingleSelectionBottomNav,
),
),
),
),
);
}),
),
),
],
),
);
});
},
child: Container(
color: Colors.white,
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Text('Open'),
),
)
],
),
),
],
),
),
),
),
);}
请让我知道我在这里做错了什么,不允许我更新我的记录。
【问题讨论】:
-
发布你的完整代码,特别是
GestureDetector的父代码 -
@ikerfah 我已经更新了 GestureDetector 的父母,请你帮帮我,这里有什么问题或遗漏。
-
mapProfCredsT[profId] 有什么用?
-
用
StatefulBuilder(builder: (BuildContext context,void Function(void Function()) setState) {return Container(..);)}包裹你的Container(showModalBottomSheet的孩子),确保你使用的是StatefulBuilder中的setState(你可以使用不同的名字以避免混淆),检查并告诉我 -
我将分享一些细节作为答案
标签: flutter flutter-layout flutter-listview