Managing multiple themes in flutter application

I am a full stack developer from Lagos, Nigeria. I am passionate about web development and ecosystem. I love learning, building and exploring tools.
Search for a command to run...

I am a full stack developer from Lagos, Nigeria. I am passionate about web development and ecosystem. I love learning, building and exploring tools.
No comments yet. Be the first to comment.
In this series, I will be teaching on how to create cross-platform applications using flutter for all levels (beginner, intermediate and expert).
One of the current challenges as of the time I'm writing this article is using firebase in flutter application, using firebase in your flutter application at default return error. Firebase returns different errors base on the context of what you are ...
Picture this: You're building a Flutter app for a food delivery service. Your app needs to track user analytics, manage configuration settings, handle API calls, and maintain a logging system. Without proper architecture, you might end up creating mu...

Introduction If you've ever built a moderately complex Flutter app, you've probably hit that moment where everything starts to feel... messy. Maybe your state management is tangled, your service classes are doing too much, or you're duplicating logic...

Flutter is a cross-platform mobile framework i.e its support for Android, iOS, macOS, Window, Linux e.t.c, developing flutter is so fascinating that you get to write once and run on all platforms. in this post, I will be showing how to run more than ...

It's 2020 and the rate in which Flutter is trending is increasing exponentially, and learning can be difficult if people are not been pointed to the right resources. With the below-curated list of the youtube channel, I believe it will help flutter d...

One of the current challenges as of the time I'm writing this article is using firebase in flutter application, using firebase in your flutter application at default return error. Firebase returns different errors base on the context of what you are ...

One of the trending concepts in the mobile application is the ability to have multiple themes such as light and dark themes, and you can have as many themes as possible in your application. the question right now is how to manage the theme in order to cut across all pages. thinking about managing the state of your theme in the application. one of the ways of managing the state is using stateful widget which is a terrible approach and it's inefficient and the other approach is to use statement package, there are a lot of statement management packages in flutter such as Providers, BLOC, Redux, Mobx e.t.c in this article I will be chosen provider state management package.
using a state management package allows you to manage your state globally in your application, it is more like having a global store where each page, widgets can connect directly to and access and manipulate data.
provider: ^3.1.0+1
OR
//to have the latest provider package
provider:
2. create a dart file that has all your theme definition
//theme.dart
ThemeData darkTheme = ThemeData.dark().copyWith(
primaryColor: Color(0xff1f655d),
accentColor: Color(0xff40bf7a),
textTheme: TextTheme(
title: TextStyle(color: Color(0xff40bf7a)),
subtitle: TextStyle(color: Colors.white),
subhead: TextStyle(color: Color(0xff40bf7a))),
appBarTheme: AppBarTheme(color: Color(0xff1f655d)));
ThemeData lightTheme = ThemeData.light().copyWith(
primaryColor: Color(0xfff5f5f5),
accentColor: Color(0xff40bf7a),
textTheme: TextTheme(
title: TextStyle(color: Colors.black54),
subtitle: TextStyle(color: Colors.grey),
subhead: TextStyle(color: Colors.white)),
appBarTheme: AppBarTheme(
color: Color(0xff1f655d),
actionsIconTheme: IconThemeData(color: Colors.white)));
3. Create a theme model
you need to create your theme model
import 'package:flutter/material.dart';
import 'package:whatsapp/theme.dart';
enum ThemeType { Light, Dark }
class ThemeModel extends ChangeNotifier {
ThemeData currentTheme = darkTheme;
ThemeType _themeType = ThemeType.Dark;
toggleTheme() {
if (_themeType == ThemeType.Dark) {
currentTheme = lightTheme;
_themeType = ThemeType.Light;
return notifyListeners();
}
if (_themeType == ThemeType.Light) {
currentTheme = darkTheme;
_themeType = ThemeType.Dark;
return notifyListeners();
}
}
}
Explanation:
we are creating an enum type for our theme, which we allow us to assign a type to each Themedata, it also helps us to check currently theme efficiently
We create a class that extends ChangeNotifier, create currentTheme as a property of ThemeData and set the default value to darkTheme and also property _themeType then assign your current themeType to it. you can set any theme of your choice, it's the theme that will be the default app theme
create a method that handles the toggling of your theme from theme 1 to theme 2. But the most important part is to call notifyListener() method, it is the method that informs your store that your state value has changed.
Wrap provider Class as a parent of your MaterialApp, which will allow all pages, widgets e.t.c have access to the provider value.
void main() => runApp(
ChangeNotifierProvider<ThemeModel>(
builder: (BuildContext context) => ThemeModel(), child: MyApp()));
Consuming your state value anywhere in your application
Provider.of<object>(context);
it allows you to access your provider store value and access your data. by specifying the ThemeModel.
below is your output
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: Provider.of<ThemeModel>(context).currentTheme,
home: MyHomePage(title: 'WhatsApp'),
);
}
}
Finally, Adding toggle widget to your app, this is the widget that will eneable you to switch from theme 1 to theme 2 and vice-versa. for the sake of simplicity of the article, I will just add a RaisedButton widget to MyHomePage Screen. snippet below
class MyHomePage extends StatelessWidget
{
final String title;
MyHomePage({this.title});
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text(widget.title)),
body: Center(
child:RaisedButton(child: Text("Change Theme",
onpressed:(){
Provider.of<ThemeModel>(context).toggleTheme();
}
))
);
}
}
NB: Provider.of<ThemeModel>(context).toggleTheme(); allows you to connect to your themeModel class and run toggleTheme() method.
The main purpose of this article is to allow you, to have an understanding of how you can create your own multi-based flutter applications using Providers State management package.