【发布时间】:2022-11-02 21:09:09
【问题描述】:
我有一个要在其中选择元素的列表,但是当我选择某个元素时,我希望将其添加到此列表顶部的列表中。如何实施?我认为值得创建一个空数组来添加所选元素,但到目前为止我还没有成功
我的代码是这样的:
/// add selected items from list
List multipleSelected = [];
/// another list to form the new list above previous one
List chosenListsAbove = [];
Widget chosendataBase() {
return FutureBuilder<List>(
future: BasesService().GetBases(),
builder: (context, snapshot) {
List? baseNames = snapshot.data;
print(baseNames);
return ListView.builder(
shrinkWrap: true,
itemCount: baseNames?.length ?? 0,
itemBuilder: (context, i) {
void _onCategorySelected(bool selected, id) {
if (selected == true) {
setState(() {
multipleSelected.add(id);
});
} else {
setState(
() {
multipleSelected.remove(id);
},
);
}
}
return ListTile(
title: Padding(
padding: const EdgeInsets.only(left: 1.0),
child: Text(
baseNames?[i]['name'] ?? 'not loading',
style: TextStyle(
fontFamily: 'fonts/Montserrat',
fontSize: 24,
fontWeight: FontWeight.w900,
color: Colors.white),
),
),
leading: Checkbox(
activeColor: Colors.green,
checkColor: Colors.green,
side: BorderSide(width: 2, color: Colors.white),
value: multipleSelected.contains(baseNames?[i]['id']),
onChanged: (bool? selected) {
_onCategorySelected(selected!, baseNames?[i]['id']);
},
)
//you can use checkboxlistTile too
);
},
);
},
);
}
【问题讨论】:
-
你能更具体地问问题吗?