Home > front end >  Save Theme Primary Color and Theme Brightness, and load on startup Flutter
Save Theme Primary Color and Theme Brightness, and load on startup Flutter

Time:02-14

I am using this code to switch between light mode and dark mode and choose the primary color. Works well, but it doesn't save nor load on app restart. How to fix that?

CodePudding user response:

You're not storing the data anywhere. You can either use flutter_secure_storage, shared_preferences, or any other package you want.

You can achieve it by storing the ThemeMode in the device, when you're updating the value, then loading it on startup, and when you load you can simply update the Provider's value

CodePudding user response:

You can save user's option in his device using package localstorage.

Init localstorage with

final storage = new LocalStorage('my_data.json')

Set data with

const String themeKey = 'theme'; // or any key you want
await storage.setItem(themeKey , 'light'); // or 'dark' or any value you want

And get it before runApp() function:

storage.getItem(themeKey) // don't forget declare themeKey

and use that value to specify choosed theme.

  • Related