Flutter Themes: Light and Dark Mode Switching
Modern mobile users expect applications to support both light and dark themes. Whether it’s to reduce eye strain, conserve battery, or simply for aesthetic preference, theme switching is now a must-have feature. Fortunately, Flutter makes it incredibly easy to implement light and dark mode themes across your application using its flexible theming system.
In this blog, we’ll explore how to implement light and dark theme switching in a Flutter app, including setup, switching logic, persistent storage, and best practices.
Why Support Light and Dark Modes?
Supporting both modes enhances user experience:
Improves readability in low-light conditions
Saves battery (especially on OLED screens)
Adapts to user system preferences
Modern design expectation in both Android and iOS apps
Flutter allows dynamic theme switching at runtime without needing a full app restart, which makes it ideal for seamless user experiences.
Step 1: Define Your Themes
Start by defining light and dark ThemeData objects in your main.dart or a separate theme file.
dart
final ThemeData lightTheme = ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.blue,
scaffoldBackgroundColor: Colors.white,
appBarTheme: AppBarTheme(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
);
final ThemeData darkTheme = ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.deepPurple,
scaffoldBackgroundColor: Colors.black,
appBarTheme: AppBarTheme(
backgroundColor: Colors.black,
foregroundColor: Colors.white,
),
);
Step 2: Set Up Theme Switching with Provider
Use state management to toggle themes. A common choice is the provider package:
yaml
Copy
Edit
dependencies:
flutter:
sdk: flutter
provider: ^6.0.0
Create a ThemeProvider class:
dart
Copy
Edit
class ThemeProvider extends ChangeNotifier {
bool isDarkMode = false;
ThemeMode get currentTheme => isDarkMode ? ThemeMode.dark : ThemeMode.light;
void toggleTheme(bool isOn) {
isDarkMode = isOn;
notifyListeners();
}
}
In your main.dart:
dart
Copy
Edit
void main() {
runApp(
ChangeNotifierProvider(
create: (_) => ThemeProvider(),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp();
@override
Widget build(BuildContext context) {
final themeProvider = Provider.of<ThemeProvider>(context);
return MaterialApp(
title: 'Flutter Theme Switch',
theme: lightTheme,
darkTheme: darkTheme,
themeMode: themeProvider.currentTheme,
home: const HomePage(),
);
}
}
Step 3: Add the Toggle Switch
Inside your UI (like a settings page or app bar menu), add a switch to toggle themes:
dart
Copy
Edit
Switch(
value: themeProvider.isDarkMode,
onChanged: (value) {
themeProvider.toggleTheme(value);
},
)
This allows users to switch between light and dark themes instantly.
Step 4: Persist User Choice
To retain theme preference across sessions, use shared_preferences:
yaml
Copy
Edit
dependencies:
shared_preferences: ^2.2.0
In your ThemeProvider, update to:
dart
Copy
Edit
Future<void> toggleTheme(bool isOn) async {
isDarkMode = isOn;
notifyListeners();
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('isDarkMode', isDarkMode);
}
Future<void> loadThemePreference() async {
final prefs = await SharedPreferences.getInstance();
isDarkMode = prefs.getBool('isDarkMode') ?? false;
notifyListeners();
}
Call loadThemePreference() during provider initialization.
Best Practices
Respect system preferences using MediaQuery.of(context).platformBrightness.
Test theme responsiveness across widgets and screens.
Customize colors for better contrast and accessibility.
Avoid hard-coded colors; use Theme.of(context).colorScheme instead.
Final Thoughts
Adding light and dark mode switching in Flutter is not only easy but also elevates your app’s user experience. By defining clean ThemeData, managing state with a provider, and saving user preferences with shared_preferences, you can offer a polished and modern UI that adapts to your users’ needs — day or night
Learn : Master Flutter Development with Expert Training
Read More: Using ListView and GridView in Flutter
Read More: Integrating Firebase with Your Flutter App
Read More: Deploying Flutter Apps to Play Store and App Store
Visit our IHUB Talent training in Hyderabad
Comments
Post a Comment