Тёмный

Flutter - Dynamic Persistent Theme Switcher using Provider and Shared Preferences 

Appwriters
Подписаться 10 тыс.
Просмотров 10 тыс.
50% 1

Dynamically switch between themes in flutter during runtime using provider as well as make the chosen theme permanent between app sessions using shared preferences.
Source Code (github.com/loh...)
Use start branch for following through the tutorial.
_______________________________________________
Repository of my UI Challenges (github.com/loh...)
Download from Play Store (play.google.co...)
Download from App Store (apps.apple.com...)
** -------------------------------------------------------------------------------- **
Let's get connected
github.com/loh...
reactbits
lohanida...
/ lohani.damodar
/ lohanidamodar
/ lohanidamodar

Опубликовано:

 

5 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 59   
@wickedgummybear3104
@wickedgummybear3104 4 года назад
Finally a video that explains it properly.. thank you
@appwriters
@appwriters 4 года назад
Thank you. Glad that it was helpful
@ajinata849
@ajinata849 3 года назад
this sharedprefs tutorial is black magic fuckery, i can instantly understand it, thank you
@appwriters
@appwriters 3 года назад
Thank you
@cjt5570
@cjt5570 4 года назад
Finally I found a tutorial that I can understand and applied to my project. Thanks bro.
@appwriters
@appwriters 4 года назад
Glad it helped! Awesome, keep fluttering!
@MrQooje2
@MrQooje2 4 года назад
Thank you very much! I've been struggling with sharedPref for days and your tutorial put it together.
@appwriters
@appwriters 4 года назад
Great to hear!
@leonardorey1705
@leonardorey1705 Месяц назад
finally the easier way to do it :D thank u!
@rahul2
@rahul2 4 года назад
Superb Video Sir .Nice tutorial ..thank u for source code
@appwriters
@appwriters 4 года назад
Most welcome. Keep supporting.
@mvn3949
@mvn3949 3 года назад
Thanks very much, quick, simple and easy, I was able to implement this feature in my app. Thanks again, loved it.
@appwriters
@appwriters 3 года назад
Wow! glad to hear that. Thank you for your support and awesome feedback.
@moritz22
@moritz22 4 года назад
I followed this tutorial and it is working perfectly, I also added a system default setting
@appwriters
@appwriters 4 года назад
Awesome!
@moritz22
@moritz22 4 года назад
@@appwriters It is also the first time I use Provider and a getter, so thank you very much
@manishmeshram51
@manishmeshram51 4 года назад
you used shared preferences so easily and here I'm stuck from 2 hr that how to use it.
@appwriters
@appwriters 4 года назад
Go through more tutorials, check out more samples on github how other's have done, you will get there in no time.
@manishmeshram51
@manishmeshram51 4 года назад
@@appwriters actully want to use dark mode and I have customised my buttons color on dark mode as well as light mode. I have made a function to get and set the value on shared preferences. Here are my functions void setBoolToSF(bool boolValue) async { SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setBool('darkMode', boolValue); } Future getBoolValuesSF() async { SharedPreferences prefs = await SharedPreferences.getInstance(); //Return bool bool boolValue = prefs.getBool('boolValue'); return boolValue; } And here is init function : @override void initState() { super.initState(); _darkMode = _prefs.then((SharedPreferences prefs) { return (prefs.getInt('darkMode') ?? false); }); } Now the issue is how to call the getboolvaluesf? I tried several times and it says future bool connot be assigned to bool value. Then how to call getboolvaluesf ()... On the color argument where I'm using ternary function. Can you help?
@njcresta
@njcresta 4 года назад
Thank you sir for great time and motivate me, you are really great man
@yiqingtan9530
@yiqingtan9530 3 года назад
Thank you bro!
@innocentokama8347
@innocentokama8347 3 года назад
Thanks buddy
@tchatterjee2828
@tchatterjee2828 3 года назад
Thank you brother 😌 Good one!
@internetcollections6453
@internetcollections6453 4 года назад
I have Subscribed...Perfect Tutorials
@appwriters
@appwriters 4 года назад
Awesome, thank you!
@TheMrSmileyCool
@TheMrSmileyCool 4 года назад
Oh thanks god !! you make my day
@appwriters
@appwriters 4 года назад
You're welcome!!
@Ali-cb3xt
@Ali-cb3xt 4 года назад
Thanks a lot!
@appwriters
@appwriters 4 года назад
You're welcome! Thank you for your support.
@Ali-cb3xt
@Ali-cb3xt 4 года назад
@@appwriters can you make a video about 'add to favourite' with provider and sqflite or hive. Please nested local json 🤗
@gourav7315
@gourav7315 4 года назад
Thank Youuu 😇
@appwriters
@appwriters 4 года назад
You're welcome 😊
@mahadyhasan8789
@mahadyhasan8789 4 года назад
thanks man you save my day :)
@appwriters
@appwriters 4 года назад
Awesome!!
@internetcollections6453
@internetcollections6453 4 года назад
Great work... Sir, I have tried this code to make my theme persistent but can not implement it. Sir, I need your help. Given is my code on GitHub please kindly review the code and please correct it. Thank You, Sir.
@appwriters
@appwriters 4 года назад
Hi, please add a runnable flutter code in the repository and send me the link, that way I will be able to run and test and would be help you quicker :)
@internetcollections6453
@internetcollections6453 4 года назад
@@appwriters Sir I have created a repository on GitHub. Please Kindly Check It. Thank You, Sir.
@alibaranseloral
@alibaranseloral 4 года назад
Hello, thanks for this great video. I had a question. My app save app settings to Firestore for each user. One of setting is dark theme. I want switch dynamically themeMode of MaterialApp with user data in Firebase. (Without Shared Preferences)
@appwriters
@appwriters 4 года назад
1. Create a splash screen, independent of theme (dark or light), and until the user data is loaded from firestore, display the splash screen, once loaded then load the theme data, to do that you might have use two steps (first a MaterialApp or WidgetsApp, with default theme for splash and loding stuff, then once loaded, another MaterialApp with the theme applied.). Let me know how you did it.
@naingtheawperc3817
@naingtheawperc3817 3 года назад
What is the purpose of getter in you change notifier class? Can you explain me pls?
@appwriters
@appwriters 3 года назад
Getter is for private fields, we don't want fields changed outside notifier (as it will not notify) so we make it private, when a variable is private to access it outside the scope of notifier we need a getter
@naingtheawperc3817
@naingtheawperc3817 3 года назад
@@appwriters Thank you so much! Your video and reply help me so much!
@IsmailAbuElmagd
@IsmailAbuElmagd 3 года назад
I use the same code and when start my app it s flash for second from default theme to the presist one?
@appwriters
@appwriters 3 года назад
Yes that happens. For that you can use a theme agnostic splash screen that is used until the persisted value is loaded
@tommyhauk
@tommyhauk 3 года назад
_saveToPrefs is async method and calling notifyListeners right after this in toggleTheme looks like an error
@appwriters
@appwriters 3 года назад
Nope, saving to sharedprefs is in background. However we have already updated in the change notifier so we can notify right await to change theme
@tommyhauk
@tommyhauk 3 года назад
@@appwriters got it
@aytunch
@aytunch 4 года назад
10:48 Thanks for this but when we hot restart the App shows dark theme for a second before accessing SharedPreferences. How can we overcome it?
@appwriters
@appwriters 4 года назад
If you want to prevent that, just introduce a bool loading in the theme notifier, which is by default true and after successfully loaded from shared preferences it's false, and in app, until loading is true, just show a progress bar or splash screen and display the app only after loading is false...
@krishnakumarramachandran5888
@krishnakumarramachandran5888 4 года назад
Thanks for this video brother, you are declaring Themedata in another file, that file does not contain context, if i want to override some of the properties of theme by using copywith ? how to do that.
@appwriters
@appwriters 4 года назад
Create a function that returns themedata instead of variable, pass context as parameter
@doylekadp
@doylekadp 4 года назад
Only 2 themes? Mine uses 4.
@appwriters
@appwriters 4 года назад
Do share, if you have a public repo, it will be helpful for others..
@doylekadp
@doylekadp 4 года назад
@@appwriters I used this guy's example medium.com/flutter-community/dynamic-theming-with-flutter-78681285d85f I use it to change basically everything dynamically(Titles, labels, map markers, etc) play.google.com/store/apps/details?id=popcornbeerfootball.kc_gatefinder and the state management stuff I use MobX.
@BrYarDJ
@BrYarDJ 3 года назад
how can use for some pages?
@appwriters
@appwriters 3 года назад
Not sure I understand what you mean?
@BrYarDJ
@BrYarDJ 3 года назад
@@appwriters i want use dark mod and light mod use for same screen when i use switch in drawer , when switch to dark all screens get to dark. i hope you understand.
@appwriters
@appwriters 3 года назад
@@BrYarDJ oh, to do that, you just need to use a stateful widget and based on theme selected (don't change the theme in material app) change on that file only
@BrYarDJ
@BrYarDJ 3 года назад
@@appwriters bro im sorry but i dont understand, this your code in main page its work in main but i dont know how call in second screen or more. void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return ChangeNotifierProvider( create: (_)=>ThemeNotifier(), child: Consumer( builder: (context,ThemeNotifier notifier,child){ return MaterialApp( title: 'demo', theme:notifier.darkTheme ? dark :light , debugShowCheckedModeBanner: false, home: MyHomePage(), ); }, ), ); } } ............................ i use this in second screen but its not work full response . @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeNotifier().darkTheme ? dark :light, home: Scaffold( i hope fix it
Далее
Flutter UI - Auth Screen (Login/Signup) - Speed Code
6:50
Why Are Open Source Alternatives So Bad?
13:06
Просмотров 646 тыс.
@ItsMamix учу делать сигму😎
00:12
Просмотров 411 тыс.
Bro's Using 3 Weapons
00:36
Просмотров 3,3 млн
Flutter: Dynamic Dark/Light Theme with Provider
5:21
Flutter :: From StreamBuilder to MultiProvider
7:59
Просмотров 12 тыс.
Flutter Shared Preferences: Simple Data Storage | 2024
10:24
@ItsMamix учу делать сигму😎
00:12
Просмотров 411 тыс.