Exploring Themes in Flutter using GetX

Rushikesh Tokapure
3 min readJul 28, 2023

--

In this article, we will see how Themes work in Flutter. We will see how you can add Light Theme and Dark Theme to your application using GetX.

At the end of this article, you will be able to change the theme of the application as follows:

Step 1:

Create a flutter project. Add the dependencies as mentioned below. You can use the below command in the terminal to add dependencies, that will be according to your other packages installed, so that you will not face the version issues because of other packages.

flutter pub add <package name>

Step 2:

The folder structure is as follows:

Assets: Declare the fonts, I prefer these to offline. To download the fonts, you can go the this site. You can use online fonts by adding the Google_Fonts dependency.

lib: It contains three folders and the main.dart file.

controller: The controller class that extends GetxController for that specific page.

screens: The UI files.

utils: The utility files that can be used to define the common files.

Step 3:

pubspec.yaml

environment:
sdk: '>=2.19.6 <3.0.0'

dependencies:
flutter:
sdk: flutter

cupertino_icons: ^1.0.2
get: ^4.6.5

flutter:

uses-material-design: true
assets:
- assets/google_fonts/comicneue/
- assets/google_fonts/poppins/

fonts:
- family: Poppins
fonts:
- asset: assets/google_fonts/poppins/Poppins-Regular.ttf

- family: ComicNeue
fonts:
- asset: assets/google_fonts/comicneue/ComicNeue-Regular.ttf

main.dart

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: CustomTheme.lightTheme,
darkTheme: CustomTheme.darkTheme,
themeMode: ThemeMode.system,
home: const Home(),
);
}
}

home.dart

Note: don’t forget to dispose the controller.

class Home extends StatefulWidget {
const Home({super.key});

@override
State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
final HomeController _controller = Get.put(HomeController());

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
title: Obx(
() => Text(_controller.currentTheme.value == ThemeMode.dark
? "Dark Theme"
: "Light Theme"),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Tap on the switch to change the Theme",
style: TextStyle(fontSize: size.height * 0.02),
),
Obx(
() => Switch(
value: _controller.currentTheme.value == ThemeMode.dark,
onChanged: (value) {
_controller.switchTheme();
Get.changeThemeMode(_controller.currentTheme.value);
},
activeColor: CustomTheme.white,
),
)
],
),
),
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}
}

home_controller.dart

class HomeController extends GetxController {
// initializing with the current theme of the device
Rx<ThemeMode> currentTheme = ThemeMode.system.obs;

// function to switch between themes
void switchTheme() {
currentTheme.value = currentTheme.value == ThemeMode.light
? ThemeMode.dark
: ThemeMode.light;
}
}

theme.dart

Note: know more about ThemeData.

class CustomTheme {
static const lightThemeFont = "ComicNeue", darkThemeFont = "Poppins";

// light theme
static final lightTheme = ThemeData(
primaryColor: lightThemeColor,
brightness: Brightness.light,
scaffoldBackgroundColor: white,
useMaterial3: true,
fontFamily: lightThemeFont,
switchTheme: SwitchThemeData(
thumbColor:
MaterialStateProperty.resolveWith<Color>((states) => lightThemeColor),
),
appBarTheme: AppBarTheme(
centerTitle: true,
backgroundColor: white,
scrolledUnderElevation: 0,
titleTextStyle: TextStyle(
fontWeight: FontWeight.w500,
color: black,
fontSize: 23, //20
),
iconTheme: IconThemeData(color: lightThemeColor),
elevation: 0,
actionsIconTheme: IconThemeData(color: lightThemeColor),
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: white,
statusBarIconBrightness: Brightness.dark,
),
),
);

// dark theme
static final darkTheme = ThemeData(
primaryColor: darkThemeColor,
brightness: Brightness.dark,
scaffoldBackgroundColor: black,
useMaterial3: true,
fontFamily: darkThemeFont,
switchTheme: SwitchThemeData(
trackColor:
MaterialStateProperty.resolveWith<Color>((states) => darkThemeColor),
),
appBarTheme: AppBarTheme(
centerTitle: true,
backgroundColor: black,
scrolledUnderElevation: 0,
titleTextStyle: TextStyle(
fontWeight: FontWeight.w500,
color: white,
fontSize: 23, //20
),
iconTheme: IconThemeData(color: darkThemeColor),
elevation: 0,
actionsIconTheme: IconThemeData(color: darkThemeColor),
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: black,
statusBarIconBrightness: Brightness.light,
),
),
);

// colors
static Color lightThemeColor = Colors.red,
white = Colors.white,
black = Colors.black,
darkThemeColor = Colors.yellow;
}

Conclusion:

Now we can use light and dark theme to the app, by using above code.

Do let me know your suggestions in the comment section.

Thank You.

--

--