0

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),
            ),
          ],
...
eno2
  • 73
  • 11
  • why do you have hundreds of files instead of doing something more efficient? – Anan Saadi Oct 06 '21 at 16:49
  • That's because each file needs a different YouTube video and individual "favorites". – eno2 Oct 06 '21 at 17:32
  • the way you do it seems extremely inefficient, can't you store the videos in a list with something like a custom class and generate them later ? – Anan Saadi Oct 06 '21 at 17:46
  • I'm also a beginner, and the YouTube api and app bar name change every time, but I couldn't find an efficient way to handle this. – eno2 Oct 06 '21 at 18:40

1 Answers1

0

You can watch this tutorial in order to accomplish something like that: https://www.youtube.com/watch?v=TRB0qZ2XaO4. It's basically the same idea. Press a button and add it somewhere else. Hope this might help.

tobsq
  • 27
  • 4