DEV Community

SultanUlArefin47
SultanUlArefin47

Posted on

How to save the color of my favorite button in Hive db?

0

I am trying to create a favorite button for my app.Which work is to change and save color, while the user press it.So i decided to use hive db for it.The problem i am facing is, when i tap the button; the color get changed,but when i move to other page or hot start/reload the page,the color changed back to it former self automatically. As i am a beginner i cant understand what value i have to put in the hive db to save the button-color.To indicate the user its being marked as favorite.
class p1 extends StatefulWidget {
@override
_p1State createState() => _p1State();
}

      class _p1State extends State<p1> {
      bool _isFavorite = true;
      @override
      Widget build(BuildContext context) {
      return MaterialApp(
     home: Scaffold(
     body:Stack(
   children:<Widget>[
   Image(
   image:AssetImage("Image/Chowsun1.jpg"),
 fit:BoxFit.cover,
 width: double.infinity,
 height: double.infinity,
 ),
  Align(alignment: Alignment.center,
      child: Text('   Its you '
          ,style: TextStyle(fontSize: 35.0,
              color: Colors.white,
              fontFamily: "Explora",
              fontWeight: FontWeight.w900 ) )



     ),
     Stack ( children: [Positioned(
       top:90,
       right: 20,
       child:const Text('   1 ',
         style: TextStyle(
             fontSize: 25.0,
             color: Colors.white,
             fontFamily: "Comforter"
           ),
           ),
           )], ),





Align(
    alignment: Alignment.bottomCenter,
    child: (
        IconButton(
            icon: Icon(
              Icons.favorite,
                color:_isFavorite ? Colors.white: Colors.red


            ),
            onPressed: (){
              setState(() {
                _isFavorite= !_isFavorite;
              });

              Hive.box(FAVORITES_BOX).put(_isFavorite, 
              !_isFavorite);

            }
        )
    )
)])
Enter fullscreen mode Exit fullscreen mode

),
);
} }

Discussion (0)