I'm trying to create a "favorites" function using shared_preferences in flutter.
Use this method to store locally without a database.
The page to use is globals.dart
with bool variable, button.dart
which is the page selection window (change button color when bookmarked),
And there is a file home0_01.dart
with a "favorite icon button (click to add that button to favorites)".
Here I would like to implement it to be stored locally using shared_prefer
But even looking at a lot of example code, it's hard to implement.
There are hundreds of favoriteButton_0_01
in my code because there are so many home0_01.dart
files. How should I code in this case?
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),
),
],
...