Theme Management in Flutter with Firestore
Theme management in Flutter allows developers to create visually dynamic apps with personalized user experiences. Integrating Firestore allows themes to be managed and updated remotely, enabling real-time customization. Customizing themes in Flutter using Firestore enhances user experience by allowing them to switch between Light, Dark, and Custom themes.
In this guide, we’ll learn how to implement a multi-theme selection feature in your Flutter application and persist those preferences using Firebase Firestore. The goal is to enable smooth theme switching and remote theme management for future scalability.
Covered Topics:
- Creating a Theme Uploader
- Defining Light, Dark, and Custom Themes
- Implementing a Theme Controller
- Implementing Theme Switching in main.dart
- Firebase Firestore Structure for Theme Storage
Why Multi-Theme Selection?
Multi-theme selection enhances user experience by allowing them to personalize the app’s appearance. It is beneficial for providing light and dark modes or custom themes based on brand colours.
Steps to Implement Multi-Theme Selection in Flutter
Add Dependencies
To get started first, install the required dependencies in pubspec.yaml:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.6
firebase_core: ^3.5.0
Step 1: Creating a Theme Uploader
We need to store theme configurations in Firebase Firestore for remote management.
Structure Firebase Firestore:
Firestore structure for “themes” collection
themes (Collection)
├── light (Document)
│ ├── bodyBgColorTop: "#FFFFFF"
│ ├── bodyBgColorBottom: "#F5F5F5"
│ ├── appBarIconsColor: "#000000"
│ └── avatarBgColor: "#E0E0E0"
├── dark (Document)
│ ├── bodyBgColorTop: "#121212"
│ ├── bodyBgColorBottom: "#1E1E1E"
│ ├── appBarIconsColor: "#FFFFFF"
│ └── avatarBgColor: "#2C2C2C"
└── custom (Document)
├── bodyBgColorTop: "#015F80"
├── bodyBgColorBottom: "#D0E1F9"
├── appBarIconsColor: "#FFFFFF"
└── avatarBgColor: "#F2F7F9"
Uploading Themes to Firebase:
import 'package:cloud_firestore/cloud_firestore.dart';
class ThemeUploader {
static Future uploadThemes() async {
final FirebaseFirestore firestore = FirebaseFirestore.instance;
Map<String, Map> themes = {
"light": {
"bodyBgColorTop": "#FFFFFF",
"bodyBgColorBottom": "#F5F5F5",
"appBarIconsColor": "#000000",
"avatarBgColor": "#E0E0E0"
},
"dark": {
"bodyBgColorTop": "#121212",
"bodyBgColorBottom": "#1E1E1E",
"appBarIconsColor": "#FFFFFF",
"avatarBgColor": "#2C2C2C"
},
"custom": {
"bodyBgColorTop": "#015F80",
"bodyBgColorBottom": "#D0E1F9",
"appBarIconsColor": "#FFFFFF",
"avatarBgColor": "#F2F7F9"
}
};
try {
for (String themeName in themes.keys) {
await firestore.collection("themes").doc(themeName).set(themes[themeName]!);
}
print("Themes uploaded successfully!");
} catch (e) {
print("Error uploading themes: $e");
}
}
}
Step 2: Defining Light, Dark, and Custom Themes
Define default light and dark themes using Flutter’s ThemeData. These themes will be applied directly and serve as fallbacks if Firestore fails.
import 'package:flutter/material.dart';
class AppThemes {
static final ThemeData lightTheme = ThemeData(
brightness: Brightness.light,
primaryColor: Colors.white,
scaffoldBackgroundColor: Colors.white,
appBarTheme: AppBarTheme(color: Colors.white, iconTheme: IconThemeData(color: Colors.black)),
);
static final ThemeData darkTheme = ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.black,
scaffoldBackgroundColor: Colors.black,
appBarTheme: AppBarTheme(color: Colors.black, iconTheme: IconThemeData(color: Colors.white)),
);
}
Step 3: Implementing Theme Controller
The ThemeController fetches and applies themes dynamically.
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'app_themes.dart';
class ThemeController extends ChangeNotifier {
ThemeData _themeData = AppThemes.lightTheme;
ThemeData get themeData => _themeData;
Map colors = {};
Future fetchAndApplyTheme(String themeName) async {
try {
final doc = await FirebaseFirestore.instance.collection('themes').doc(themeName).get();
if (doc.exists) {
colors = {
"bodyBgColorTop": _hexToColor(doc.data()!["bodyBgColorTop"] ?? "#FFFFFF"),
"bodyBgColorBottom": _hexToColor(doc.data()!["bodyBgColorBottom"] ?? "#F5F5F5"),
"appBarIconsColor": _hexToColor(doc.data()!["appBarIconsColor"] ?? "#000000"),
"avatarBgColor": _hexToColor(doc.data()!["avatarBgColor"] ?? "#E0E0E0"),
};
notifyListeners();
}
} catch (e) {
print("Failed to fetch theme: $e");
}
}
static Color _hexToColor(String hexColor) {
return Color(int.parse(hexColor.replaceAll('#', '0xFF')));
}
}
Step 4: Implementing Theme Switching in main.dart
Modify main.dart to integrate theme switching.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'theme_controller.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => ThemeController(),
child: Consumer(
builder: (context, themeController, child) {
return MaterialApp(
theme: themeController.themeData,
home: ThemeSelectionScreen(),
);
},
),
);
}
}
Step 5: Creating Theme Selection UI
Provide a UI for users to select between Light, Dark, or Custom themes. Each button triggers the theme fetch and applies it via the ThemeController.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'theme_controller.dart';
class ThemeSelectionScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final themeController = Provider.of(context);
return Scaffold(
appBar: AppBar(title: Text("Select Theme")),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildThemeButton(context, "Light Theme", "light"),
_buildThemeButton(context, "Dark Theme", "dark"),
_buildThemeButton(context, "Custom Theme", "custom"),
],
),
),
);
}
Widget _buildThemeButton(BuildContext context, String label, String themeName) {
return ElevatedButton(
onPressed: () {
Provider.of(context, listen: false).fetchAndApplyTheme(themeName);
},
child: Text(label),
);
}
}
Enhancing Flutter Apps with Dynamic Theme Management
Multi-theme support is more than just a cosmetic upgrade; it’s about giving users control over their experience. This blog demonstrated how to manage themes in Flutter with Firebase Firestore. Integrating remote theme configurations and dynamic switching makes your Flutter app more user-friendly, scalable, and adaptable to branding needs. With this approach, you can easily update themes remotely without redeploying the app, opening up endless possibilities for customization and brand identity.
Implementing multi-theme support becomes efficient and manageable with the proper structure, like Firestore, to hold theme data and provide for state management. So, let your users choose how they want their app to look and feel!



