我想创建一个"收藏夹";在flutter中使用shared_preferences的函数
在没有数据库的情况下使用此方法进行本地存储。
要使用的页面是带有bool变量的globals.dart
,button.dart
是页面选择窗口(添加书签时更改按钮颜色),还有一个文件home0_01.dart
,上面有一个"收藏图标按钮(点击将该按钮添加到收藏)"
这里我想使用shared_prefer
将其实现为本地存储但是即使看了很多示例代码,也很难实现。我的代码中有数百个favoriteButton_0_01
,因为有很多home0_01.dart
文件。在这种情况下我应该如何编码?
globals.dart
library prj.com.globals;
bool favoriteButton_0_01 = true;
bool favoriteButton_0_02 = false;
...
button.dart
...
LearnLevelButton(
color: favoriteButton_0_01 ? Colors.orange : Color(0xff7ba6f9),
text: '',
onTap: () async {
await Navigator.pushReplacement(context, MaterialPageRoute(builder: (context){
return home0_01();
}));
},
),
...
home0_01.dart
void delete() async {
setState(() {
favoriteButton_0_01 = false;
print(favoriteButton_0_01);
});
}
void saved() async {
setState(() {
favoriteButton_0_01 = true;
print(favoriteButton_0_01);
});
}
...
actions: <Widget>[
IconButton(
onPressed: favoriteButton_0_01 ? delete : saved,
icon: favoriteButton_0_01
? Icon(Icons.bookmark_rounded, color: Colors.yellow[800], size: 30)
: Icon(Icons.bookmark_add_outlined, color: Colors.yellow[800],size: 30),
),
],
...
您可以观看本教程,以便完成类似的操作:https://www.youtube.com/watch?v=TRB0qZ2XaO4。基本上是一样的。按一个按钮,把它添加到其他地方。希望这对你有帮助。