SharedPreferences for Dark and Light Theme - Flutter Explained

preview_player
Показать описание
#DarkTheme and #LightTheme is one of the most popular trends in 2020. This is for me reason enough to explain on the example the provider package in #Flutter. Today in #FlutterExplained we solve the mystery of how to implement and use the provider package to create a dark and light theme depending on the user's selection.

⭐️⭐️ Helpful Ressources ⭐️⭐️

⭐️⭐️ Timeline ⭐️⭐️

⭐️⭐️ Follow me ⭐️⭐️

⭐️⭐️ Support ⭐️⭐️
If you read this so far, thank you once more. If you like to support me, here are some links that could help me a lot.

Buy me a coffee - You are free to donate me a coffee so I can keep up my work.

Amazon Affiliate Link - You buy for the same price and I get a small amount of it.
This leads to an amazing Flutter book that I can recommend to every beginner of flutter

For every developer, a must-have and better said a must-read is the Clean Code book from Uncle Bob.

⭐️⭐️ Special Thanks ⭐️⭐️

Photo by Sebastian Voortman from Pexels
Рекомендации по теме
Комментарии
Автор

One thing I would like to add to this video. In the main function where you pass the boolean value to the constructor of the ThemeProvider from shared preferences, you have to do a null check. If a user performs a clean install of the app there is nothing in shared preferences, so the app will crash at startup. It is not happening in the video, because you saved the boolean data to shared preferences earlier, but if you would erase the cache of the app on your phone, it would fail to start up. So the very simple solution is the following:

create: (BuildContext context) => ThemeProvider(isDarkMode: prefs.getBool("isDarkTheme") ?? false

You just have to add this part : ' ?? false '
This means the following: If the value of prefs.getBool("isDarkTheme") is null (so there is no saved data), set the value of isDarkMode to false. Otherwise give it the value from shared preferences.
This way, if you perform a clean install, the app will launch just fine and the active them will be the light one. If the user then changes the theme, the app works as expected, because it will find the saved data in shared prefs.

Great videos btw, you helped me a lot to set up my dark and light theme, thanks :)

laszloszurok
Автор

Sir please adding these video its help a lot plz don't stop

jaituteja
Автор

Bro, i got stuck trying to change the theme and saved in the shared preferences, there was a lot of errors in my app but with this video everything looks more simple and easy, thanks for sharing this type of content, as a begginer in flutter, i really appreciate this, ty <3

LuisMartinez-senp
Автор

The video sound is pretty good, beyond my imagination

ThaiNguyen-dxnk
Автор

Hi! I can't make it to your streams but was really enjoying your videos. I've been creating an ultimate flutter seed with basic features that every production app would need. You helped me with a few topics very much. The only thing that I've been stuck on for a while now is testing. Do you know anything about this topic? Maybe some great written/video tutorials?

moshyfawn
Автор

hello Sir, in the video you have class ChangeNotifierProvider, Consumer defined in a file called change_notifier but this is not in the git repo. And I can see its referenced, in the theme.dart and main.dart. Can you help?

MuzeTrends
Автор

Can you help me in storing the qrvalue received after scanning the qrcode using the qrscan flutter package in pub.dev

akshayv
Автор

how to implement this on all screens of your app??

nomikhan