How to create a custom theme in Flutter and apply it across the entire app.

Chathuranga CPM
3 min readMay 12, 2023

--

A little introduction about the Flutter

Flutter is a popular open-source framework for building high-performance, cross-platform mobile applications. With Flutter, you can write code once and deploy it on both Android and iOS platforms, as well as the web and desktop. One of the key benefits of Flutter is its flexibility in creating custom app themes.

What is a Flutter Theme?

In Flutter, a theme is a set of design specifications that define the look and feel of your app. A theme is defined by a set of properties such as colors, fonts, text styles, and sizes. By creating a custom theme, you can apply a consistent design to your entire app and make it look and feel unique.

Creating a Custom Theme

To create a custom theme in Flutter, you need to define a ThemeData object that contains the various properties of your theme. These properties can include colors, fonts, text styles, button styles, and more.

Here’s an example of how to define a custom theme:

import 'package:flutter/material.dart'
final ThemeData myTheme = ThemeData(
primarySwatch: Colors.blue,
backgroundColor: Colors.white,
accentColor: Colors.green,
buttonColor: Colors.blue,
textTheme: TextTheme(
headline6: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
bodyText2: TextStyle(fontSize: 16.0),
),
);

In this example, we define a myTheme constant that defines the app’s primary color, background color, accent color, button color, and text theme. The text theme defines two text styles — headline6 and bodyText2.

The headline6 style has a font size of 20 and a bold weight and is typically used for headings or titles. The bodyText2 style has a font size of 16 and is used for regular body text.

Applying the Theme

Once you have defined your custom theme, you can apply it to your entire app using the MaterialApp widget.

Here’s an example:

import 'package:flutter/material.dart'
import 'package:my_app/theme.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: myTheme,
home: Scaffold(
appBar: AppBar(
title: Text('Flutter App'),
),
body: Center(
child: Text('Hello, World!'),
),
),
);
}
}

In this example, we import the theme.dart file and use the myTheme constant in the MaterialApp widget’s theme property. This will apply the theme to the entire app.

Using the Theme on another screen

Once you have applied the theme to your app, you can use its properties in your widgets. For example, to use the headline6 text style in a widget, you can access it using the Theme.of(context).textTheme.headline6 property.

Here’s an example:

import 'package:flutter/material.dart'
class MyOtherScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Other Screen'),
),
body: Center(
child: Text(
'This is a headline',
style: Theme.of(context).textTheme.headline6,
),
),
);
}
}

In this example, we are using the headline6 style to display the text ‘This is a headline’ in the center of the screen.

Note that to use the headline6 style, we first call Theme.of(context) to get the current theme data, and then access the textTheme.headline6 property to get the headline6 text style. This way, even if you change the theme in the future, the style will update automatically across the entire app.

Thank you for following this guide on creating a custom theme in Flutter! By now, you should have a good understanding of how to create and apply a custom theme to your entire app.

Using themes can greatly improve the design consistency and user experience of your app. With a custom theme, you can ensure that your app looks and feels unique while maintaining a consistent design across all screens.

I hope this article was helpful to you in creating a custom theme in Flutter. ;)

Once again, thank you for reading, and we wish you the best of luck in your Flutter development journey! :)

--

--