gpt4 book ai didi

flutter - 使用Dart/Flutter,如何在ListViewBuilder中添加收藏夹?

转载 作者:行者123 更新时间:2023-12-03 03:21:47 25 4
gpt4 key购买 nike

我试图允许用户将由ListViewBuilder构建的项目标记为收藏夹。使用我当前的代码,当用户收藏一个情节时,所有情节都被标记为收藏。我希望用户能够将每个剧集分别添加为收藏夹,并在重新启动后保留该收藏夹。我已将数据保存到Firebase数据库,但似乎应在应用程序本身中处理。

做这个的最好方式是什么?谢谢!

这是我当前的代码:

class Epi {
final String endTime;
final String name;
final String networkName;
final String showName;
final String startTime;

Epi({this.endTime, this.name, this.networkName, this.showName, this.startTime});

factory Epi.fromJson(Map<dynamic, dynamic> parsedJson) {
DateTime endTimeCon = DateTime.parse(parsedJson['endTime']);
String newEndTime = formatDate(endTimeCon, [yyyy, '/', mm, '/', dd, ' ', hh, ':', nn, ':', ss, ' ', am]);

DateTime startTimeCon = DateTime.parse(parsedJson['startTime']);
String newStartTime = formatDate(startTimeCon, [yyyy, '/', mm, '/', dd, ' ', hh, ':', nn, ':', ss, ' ', am]);

return Epi(
endTime: newEndTime,
name: parsedJson['name'],
networkName: parsedJson['networkName'],
showName: parsedJson['showName'],
startTime: newStartTime,
);
}
}
  bool _isFavorited = true;
void _toggleFavorite() {
setState(() {
if (_isFavorited) {
_isFavorited = false;
} else {
_isFavorited = true;
}
});
}
body: Column(
children: <Widget>[
SizedBox(height: 5.0),
Expanded(
child: ListView.builder(
itemCount: elist.length,
itemBuilder: (context, index) {
return InkWell(
onTap: () {
selectEpisode(index);
},
child: Card(
child: Column(
children: <Widget>[
ListTile(
title: Text(elist[index].name),
subtitle: Text(elist[index].startTime),
leading: IconButton(
icon: (_isFavorited ? Icon(Icons.favorite_border) : Icon(Icons.favorite)),
color: Colors.red[500],
onPressed: _toggleFavorite,
),
trailing: Icon(Icons.arrow_forward_ios)
)
],
),
),
);
}),
),
],
)

最佳答案

在我的Congress Fahrplan应用程序(Github)中,我正在做您想要实现的目标。

favorite_provider中,我将值存储在对象本身中,并将其添加到list of favorited objects中。每当将对象添加到此列表时,该列表就会通过file_storage类作为JSON写入磁盘。

重新启动应用程序后,将从REST API中获取对象。然后,我将其ID与本地JSON中的对象进行匹配,并设置是否将其收藏夹以恢复收藏夹状态。

关于flutter - 使用Dart/Flutter,如何在ListViewBuilder中添加收藏夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62374104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com