Dark Mode

Setting Up Light/Dark Mode and Fonts in a Flutter App

Setting Up Light/Dark Mode

The theme mode will be divided into three options:

  • Light Mode

  • Dark Mode

  • System Mode (which follows the system settings)

However, the actual UI will only display two primary modes: Light and Dark.

extension MyThemeModeContext on BuildContext {
  bool get isDarkMode => switch (myThemeMode) {
    ThemeMode.system => MediaQuery.of(this).platformBrightness == Brightness.dark,
    ThemeMode.light => false,
    ThemeMode.dark => true,
  };
}

Creating a Global Variable to Manage Themes

We will define a global variable to manage the application's theme:

ThemeMode myThemeMode = ThemeMode.system;

Handling Theme Persistence

A common issue with this approach is that the theme setting will not persist after the app is completely closed. There are several solutions to this problem:

  1. Using shared_preferences – A package that allows saving and retrieving values when initializing the app. Learn more here.

  2. Storing the Theme in a BLoC State – This ensures that the theme is managed within the state management logic.

Example of storing the theme in a Bloc:

For more details on how to persist values using BLoC, check out my article on the topic here.

Bloc

Applying Light and Dark Themes in the UI

Defining Theme-Specific Colors

Each theme (light and dark) has its own set of colors. We can determine which color to use based on the theme mode.

How to Use

Handling context Accessibility

One potential issue with this approach is that context is required to access theme-specific colors, which may not always be convenient.

I have a simple way to access context from anywhere in the project. Check out my article for more details:

Context Anywhere

Switching Between Light and Dark Themes

To change the theme mode, use the following code:

The second line forces the entire application to redraw the screen, ensuring the theme change is applied immediately.

Light and Dark Themes with Custom Fonts

Fonts play a crucial role in UI design. Currently, I use two methods to add custom fonts to my Flutter applications.

Method 1: Adding Fonts from Asset Files

Define a TextStyle using a custom font stored in the assets folder:

You can include fonts in your pubspec.yaml and reference them in the TextStyle. If using fonts from a package, ensure you specify the package argument correctly.

Method 2: Using Google Fonts

If your desired font is available on Google Fonts, you can use it directly with the google_fonts package:

Handling Font Scaling for Different Screen Sizes

When the screen size changes, fonts should scale proportionally to maintain design consistency. One way to achieve this is by defining a ratio based on the screen width:

Applying Scaled Font Styles from Figma Design

Once we have the ratio, we can create text styles dynamically:

This approach ensures your fonts scale properly across different devices, keeping your UI responsive and visually consistent.

Method 2: Using the google_fonts Library

You can use the google_fonts package if your desired font is available on Google Fonts.

Handling Font Scaling for Different Screen Sizes

A common issue when changing screen sizes is that fonts need to scale proportionally to maintain a harmonious design.

In this case, I use a ratio based on the screen width. This is calculated by taking the current screen width and dividing it by the original design width (e.g., 1440px in Figma).

Applying Scaled Font Styles from Figma Design

Once we have the scaling ratio, we can create text styles dynamically based on the design specifications:

Full Code Overview

Usage Example

You can apply the text styles as follows:

Customizing Styles with Colors

You can fully customize styles according to your design system. Here’s an example of modifying the text color dynamically:

With this setup, when the light/dark mode changes, the text color will automatically adapt based on the selected theme.

This concludes my guide on setting up light/dark mode in Flutter. You can explore more about creating a UI package at

Core UI

Buy Me a Coffee | Support Me on Ko-fi

Last updated