Flutter – Implement Light Mode and Dark Mode
Last Updated :
26 Apr, 2025
In this article, we are working on the light theme and dark theme in the flutter application. A sample video is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the material package
Adding material package that gives us the essential functions and calls the runApp method in the main function that will call our application.
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
Step 3: Creating Stateful Widget
Now we have to make a Stateful widget because our application goes to change its state and then returns the MaterialApp widget which allows us the set the title and theme and many more.
class RunMyApp extends StatelessWidget {
const RunMyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp();
}
}
Step 4: Creating Scaffold Widget
Give the home property and there can be a scaffold widget that has the property of AppBar and body. AppBar allows us to give the title of AppBar, color, leading, and trailing icon.
home: Scaffold(
appBar: AppBar(
title: Text('Always Light Theme'),
),
body: Center(child:Text('GeeksforGeeks'));
),
Always Light Theme Mode
Now move back to adding the theme in the MaterialApp, MaterialApp allows to set of the theme, then the theme further takes the Theme Data, Then using primary Swatch gives it color.
theme: ThemeData(primarySwatch: Colors.green),
Code
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.green),
debugShowCheckedModeBanner: false ,
home: Scaffold(
appBar: AppBar(
title: Text( 'Always Dark Mode' ),
),
body: Center(
child: Text( 'Geeks for Geeks' ),
),
),
);
}
}
|
Output:
Always Dark Theme Mode
Now for making the Dark theme just add another parameter in the MaterialApp called darkTheme, which takes the themeData.dark.
darkTheme: ThemeData.dark(), // standard dark theme
Code
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.green),
darkTheme: ThemeData.dark(),
debugShowCheckedModeBanner: false ,
home: Scaffold(
appBar: AppBar(
title: Text( 'Always Dark Mode' ),
),
body: Center(
child: Text( 'Geeks for Geeks' ),
),
),
);
}
}
|
Output:
Device Controlled Theme Mode
Now again to make the system-controlled theme mode, you need to add another parameter in the MaterialApp called themeMode.
themeMode: ThemeMode.system,
Code
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.green),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode. system ,
debugShowCheckedModeBanner: false ,
home: Scaffold(
appBar: AppBar(
title: Text( 'Device Controlled theme Mode' ),
),
body: Center(
child: Text( 'Geeks for Geeks' ),
),
),
);
}
}
|
Output:
User-Controlled Theme Mode
Step by Step Implementation
First, you need to make two buttons in the body of the application so that we can switch between the themes. And call the method changeTheme with the parameter ThemeMode.
ElevatedButton(
onPressed: () {
changeTheme(ThemeMode.light);
},
child: Text('Light theme')),
ElevatedButton(
onPressed: () {
changeTheme(ThemeMode.dark);
},
child: Text('Dark theme')),
Now we have to make the new variable and then define the method.
ThemeMode _themeMode = ThemeMode.system;
void changeTheme(ThemeMode themeMode) {
setState(() {
_themeMode = themeMode;
});
}
Code
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
ThemeMode _themeMode = ThemeMode. system ;
void changeTheme(ThemeMode themeMode) {
setState(() {
_themeMode = themeMode;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.green),
darkTheme: ThemeData.dark(),
themeMode: _themeMode,
debugShowCheckedModeBanner: false ,
home: Scaffold(
appBar: AppBar(
title: Text( 'Device Controlled theme Mode' ),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Choose your theme:' ,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
changeTheme(ThemeMode.light);
},
child: Text( 'Light theme' )),
ElevatedButton(
onPressed: () {
changeTheme(ThemeMode.dark);
},
child: Text( 'Dark theme' )),
],
),
],
),
),
),
);
}
}
|
Output:
Similar Reads
Flutter - Implementing Calendar
Nowadays in most of the apps, we see Calendar in most of the apps for displaying birth dates or for any appointment application. Displaying the date in the app with the help of the Calendar view gives a better user experience. In this article, we are going to see how to implement Calendar in our Flu
3 min read
Flutter - Implement map() Method
In Flutter, the map() method is used to transform and manipulate the elements of a collection (such as a List, Set, or Iterable) and produce a new collection with the transformed values. It allows you to apply a function to each element of the collection and create a new collection based on the resu
4 min read
Flutter - Implement a Simple VideoPlayer
To implement a simple video player in Flutter, you can use the video_player package, which allows you to play videos from various sources. In this article, we are going to see a step-by-step procedure to implement a VideoPlayer in a Flutter App. A sample video is given below to get an idea about wha
3 min read
Flutter - Implementing Badges
Badges can be used for various purposes in an application. For example, showing the number of messages, number of items in the cart, etc. In this article, we will see the implementation of badges in Flutter using the badges Flutter package. Install the library: In the pubspec.yaml file, add badges l
5 min read
Flutter - Implementing Swipe to Dismiss Feature
The Swipe to dismiss feature is used by us in many mobile apps. In this article, we will create a list of items and implement a swipe to dismiss in it. When we want to dismiss the content using swipe then we can do this with the help of swipe to dismiss widget in a flutter. Generally, we use this wh
2 min read
Flutter - Implementing Overlay
Overlays let independent child widgets float visual elements on top of other widgets by inserting them into the overlay's Stack. This article discusses the implementation of Overlays in Flutter. To implement Overlay in Flutter, we need to know about two Flutter built-in classes: OverlayEntry class a
10 min read
Day Night Time Picker in Flutter
day_night_time_picker is a Day Night Time Picker for flutter, beautiful day and night animation with the sun and moon assets. we don't need to add explicit assets. A sample video is given below to get an idea about what we are going to do in this article. [video mp4="https://media.geeksforgeeks.org/
3 min read
Flutter - Implement Animated TextSwitcher
TextSwitcher is specifically designed for switching between different text with animations. It's commonly used to create smooth text transitions. There is no direct widget of TextWsitcher in Flutter but we can Implement this by using AnimatedSwitcher.This widget allows us to smoothly transition betw
4 min read
Flutter - Implement Status Alert
In Flutter, a "status alert" typically refers to a visual feedback mechanism that informs the user about the status or outcome of a certain operation or event. The status_alert package in Flutter is a third-party package that provides an easy way to create and display such status alerts. These alert
4 min read
Flutter - Implement IndexedStack Widget
The IndexedStack widget in Flutter is used to display a single child from a list of children at a given index. It's commonly used when you want to show one child widget while hiding the others, such as in a tabbed interface. In this article, we are going to implement the IndexedStack widget and expl
5 min read