【发布时间】:2021-02-15 16:32:29
【问题描述】:
我正在尝试从 ListTile 更改所选磁贴的背景。
我搜索并找到了以下两个帖子,但是没有一个可以解决我的问题。
在@CopsOnRoad 的回答的帮助下,我得到了更好的帮助。
使用以下代码,如果我选择多个图块,则全部保持选中状态。如何一次只选择一个,然后取消选择之前选择的?
图块索引受itemCount: is books.length限制。
List<Favorited> books;
// todo: this needs to be changed, has a hard coded value of 200
List<bool> _selected = List.generate(200, (i) => false); // Pre filled list
@override
Widget build(BuildContext context) {
final booksProvider = Provider.of<Model>(context);
return Container(
child: StreamBuilder(
stream: booksProvider.getUserFavList('103610812025'),
builder: (context, AsyncSnapshot<List<Favorited>> snapshot) {
if (snapshot.hasData) {
books= snapshot.data.toList();
return ListView.builder(
itemCount: books.length,
itemBuilder: (buildContext, index) {
return Container(
color: _selected[index] ? Colors.amber : Colors.transparent,
child: ListTile(
title: InkWell(
child: Text(snapshot.data[index].title),
onTap:() {
setState(() {
_selected[index] = !_selected[index];
});
}),
subtitle: Text(snapshot.data[index].name),
),
);
});
} else {
return Text('Fetching');
}
}),
);
【问题讨论】: