Implementing Dark Mode in Flutter
Implementing Dark Mode in Flutter – A Complete Guide
Dark Mode is no longer just a trend—it's a must-have feature in modern apps. It reduces eye strain, saves battery on OLED screens, and gives users more control.
In this guide, you’ll learn how to add dark mode to your Flutter app using simple steps, all within 200 lines of code.
Let’s dive in. π§π»
π Step 1: Create a New Flutter Project
If you haven’t already:
π Step 2: Project Structure
We'll be editing mainly:
-
main.dart
-
Adding a toggle button to switch between light and dark themes
π§ Step 3: Define Light and Dark Themes
Open lib/main.dart
and start coding:
π‘ Step 4: Create the Home Screen
Still in main.dart
, below the MyAppState
, add this widget:
π§ͺ Step 5: Run the App
Use:
Try tapping the π/☀️ icon in the top-right. The app instantly switches themes!
✅ Result Preview
-
You get real-time switching between dark and light themes
-
Theme settings apply to AppBar, text, and background
-
Clean and responsive UI
π Optional: Persist Dark Mode with Shared Preferences
Want the app to remember the theme after closing?
-
Add the package:
-
Update your code with:
Now the app remembers the selected theme even after restart.
π Bonus: System-Wide Dark Mode
To match the system theme (Android/iOS):
Let the user choose between:
-
Light Mode
-
Dark Mode
-
System Default
You can build this using a simple dropdown or switch options.
π§ TL;DR – Quick Recap
Step | What You Did |
---|---|
Setup Project | Created a new Flutter project |
Add Themes | Defined light and dark ThemeData |
Add Toggle | Created a switch using IconButton |
Persist Option | Used SharedPreferences (optional) |
System Theme | Supported system-level themes |
✨ Final Thoughts
Dark mode is now a user expectation, not just a luxury.
With Flutter, adding it is super simple using ThemeMode
, ThemeData
, and a few lines of logic. It improves UX, accessibility, and makes your app look more modern.
“Let users choose how they experience your app.”
Comments
Post a Comment