Home > database >  flutter changed value with set state falls after refresh
flutter changed value with set state falls after refresh

Time:09-05

In the code below, after pressing the key, the color of the program's theme changes, the problem is that upon exiting and entering the program, the color of the program changes to the previous state. What is your suggestion?

class _FeedScreenState extends State<FeedScreen> {
  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;

    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor:
          width > webScreenSize ? webBackgroundColor : mobileBackgroundColor,
      appBar: width > webScreenSize
          ? null
          : AppBar(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(25.0),
                      bottomRight: Radius.circular(25.0))),
              toolbarHeight: 40,
              backgroundColor: d,
              foregroundColor: w,
              bottomOpacity: 0.1,
              centerTitle: false,
              title: SvgPicture.asset(
                'assets/ic_instagram.svg',
                color: primaryColor,
                height: 25,
              ),
              actions: [
                IconButton(
                  icon: Icon(
                    Icons.mail,
                    color: Color.fromARGB(255, 255, 0, 191),
                  ),
                  onPressed: () {
                    setState(() {
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) => colorset(),
                        ),
                      );
                    });
                  },
                ),
                IconButton(
                  icon: Icon(
                    Icons.mail,
                    color: Color.fromARGB(255, 1, 132, 255),
                  ),
                  onPressed: () {
                    d = Color.fromARGB(255, 1, 132, 255);
                    setState(
                      () => d = Color.fromARGB(255, 1, 132, 255),
                    );
                    // d:Color.fromARGB(255, 1, 132, 255);
                    // setState(() {
                    //   d:
                    //   Color.fromARGB(255, 1, 132, 255);
                    // });
                  },
                ),
                Text(''),
              ],
            ),

Code u see change this line in utils:

 Color d = Color.fromARGB(255, 233, 229, 8);

CodePudding user response:

You Need To Save The State Of The App in A Storage File Or Db, Then Load The Stored State And Use It On App Start. I Would Suggest Hive For Being Fast, Simple And Reliable. Create An AppState Class And Generate Hive Adapter To Save Object And Get/Set The Hive To The Current App State It Works With All Platforms With Some Special integration for Web Using flutter Hive

  • Related