Hi everyone, In this article, we will be learning how to implement the dark theme in our app using the provider package

As we all now Dark theme is trending and most of the popular app has the feature to turn into the dark mode.

There are two ways to turn on the dark mode in any app:

1: Adding a custom option to change to the dark mode (Eg: Twitter, Medium app)

2: Depends on the Phone system setting (Eg: Instagram)

We already have both the options in flutter.

If you check the MaterialApp widget you will see

We have the theme and darkTheme parameter in MaterialApp widget we can provide the dark ThemeData in darkTheme and light ThemeData in theme if we want our app to change the theme according to the system preferences.

And if we have a custom button or something to change the dark theme then we just have to put some condition to change it.

Let's implement the dark mode to an app

Implementation

Step 1: Add the dependencies

Add dependencies to pubspec.yaml file.

dependencies:

flutter:

sdk: flutter

shared_preferences: "<newest version>"

provider: "<newest version>"

we are using the SharedPreferences to set the value in the memory so even if we close the app and reopens it, our data won't lose.

Provider is used to manage the state when the dark theme is implemented on the app.

Step 2: Create a class for SharedPreferences

We are creating a separate class for the SharedPreferences so the code won't mess up.

We have created a class DarkThemePreference where we are storing the bool value in for the dark theme, we have two methods for saving and retrieving the bool value.

Step 3: Create a model class for provider

We are accessing the preference value through the provider so whenever there is any change the notifyListeners() UI will be updated if we have attached the provider to the screen.

Step 4: Add custom theme data for dark mode

if you see we have to provide the ThemeData , so I have created a method for dark and light mode.

Step 5: Add the provider to Material app

void main() {

runApp(MyApp());

}

when we run our app the first widget will be MyApp which will have MaterialApp .

class MyApp extends StatefulWidget {

@override

_MyAppState createState() => _MyAppState();

}



class _MyAppState extends State<MyApp> {





@override

Widget build(BuildContext context) {

return MaterialApp(

debugShowCheckedModeBanner: false,

home: SplashScreen(),

routes: <String, WidgetBuilder>{

AGENDA: (BuildContext context) => AgendaScreen(),

},

);

}

}

Now in the initState of our MyApp we will check for the value that is stored in the SharedPreferences through the Provider.

DarkThemeProvider themeChangeProvider = new DarkThemeProvider();



@override

void initState() {

super.initState();

getCurrentAppTheme();

}



void getCurrentAppTheme() async {

themeChangeProvider.darkTheme =

await themeChangeProvider.darkThemePreference.getTheme();

}

If you see the getCurrentAppTheme method, I am fetching the value from the preferences and set the value in the provider.

Now we will add notifier to the material app which is ChangeNotifierProvider and set a provider model to it, if any change happens in the provider it will notify its descendants.

To listen to the changes in the UI we can use Consumer will listen to the changes and update the MaterialApp .

We can also use this instead of Consumer<T> .

var darkThemeProvider = Provider.of< DarkThemeProvider >(context)

Final Step: Turn on the dark theme

For example, we have a CheckBox through which we are setting the theme.

@override

Widget build(BuildContext context) {

final themeChange = Provider.of<DarkThemeProvider>(context); ... Checkbox(

value: themeChange.darkTheme, onChanged: (bool value) { themeChange.darkTheme = value; })

When the value is “true” the provider will set the theme to dark and turn into a light mode when the value is true.

and Kaboom 💥

We have successfully learned how to add a dark theme to your app.

We also have created a Devfest app for Google Developers Group New Delhi in which we had implemented a dark mode with some animation.

Check out the whole code here

Thanks for reading this article ❤

If I got something wrong 🙈, Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.

Check my GitHub repositories.