我正在構建一個對話框,允許用戶通過選中區域名稱旁邊的復選框來使用多個區域。
我想知道的是如何將 Checkbox 選中的值添加到 int 串列中,以便稍后發送到 API
以及如何進行不影響其他復選框值的多選,我的意思是如果我選中該框,整個串列將保持未選中狀態
請問我已經嘗試了很長時間,但一無所獲

showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text('???? ?????? ???????',
textAlign: TextAlign.center,
style: mediumStyle(
fontSize: 21,
color: AppColors.blackColor)),
SizedBox(
height: 20.h,
),
SizedBox(
width: 500,
height: 250,
child: ListView.builder(
itemCount: state.selectedCity!.regions.length,
itemBuilder: (context, index) {
bool isChecked = false;
return CheckboxListTile(
title: Text(
state.selectedCity!.regions[index].name,
style: mediumStyle(fontSize: 20.sp, color: AppColors.blackColor),
),
value: isChecked,
onChanged: (value) {
setState(() => isChecked = value!);
List regions = [];
},
);
},
),
)
],
),
);
uj5u.com熱心網友回復:
嘗試這個:
Map<String, bool> selectedRegions = {};
state.selectedCity!.regions.for(region){
selectedRegions[region.name]=false;
}
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text('???? ?????? ???????',
textAlign: TextAlign.center,
style: mediumStyle(
fontSize: 21,
color: AppColors.blackColor)),
SizedBox(
height: 20.h,
),
SizedBox(
width: 500,
height: 250,
child: ListView(
children: selectedRegions.keys.map((String key) {
return new CheckboxListTile(
title: Text(key,
style: mediumStyle(fontSize: 20.sp, color: AppColors.blackColor),),
value: selectedRegions[key],
onChanged: (bool value) {
setState(() {
selectedRegions[key] = value;
});
},
);
}).toList(),
);
uj5u.com熱心網友回復:
你可以嘗試這樣的事情,使用你的變數:
Future<void> showADialog(List<String> items) async {
final selectedIndexes = <int>[];
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'???? ?????? ???????',
textAlign: TextAlign.center,
),
const SizedBox(
height: 20,
),
SizedBox(
width: 500,
height: 250,
child: StatefulBuilder(
builder: (context, setState) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return CheckboxListTile(
title: Text(
item,
),
value: selectedIndexes.contains(index),
onChanged: (value) {
if (value == null) return;
setState(() {
value
? selectedIndexes.add(index)
: selectedIndexes.remove(index);
});
},
);
},
);
},
),
)
],
),
);
});
print(selectedIndexes);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/525200.html
標籤:扑列表镖列表显示复选框
上一篇:具有嵌套關聯的介面投影
下一篇:如何使用axios獲取api?
