Extended theme - Extended themes for Flutter

Overview

Extended Theme

This package will allow you to extend theming in Flutter so you can define and use your own properties.

What does it solve?

Say you want to style custom widgets in your app, but the properties in ThemeData don't quite fit your need. You'd rather add your own properties that are more descriptive. There are several ways to do this.

One way is to create extensions for Theme. But they will not be as dynamic unless you start putting logic in them.

Another way is to create your own Object that contains your custom styles and provide them to the widget tree, just like Theme does, with an InheritedWidget (or you could use a package like provider).

If you have one single style and don't support light and dark theme, this could be fine.

But what happens if you do want to support dark and light themes, and what if your app has two very distinct parts that should use their own Theme?

You'll end up with:

  • Main Theme light
  • Main Theme dark
  • Main Custom Theme light (for your custom styling)
  • Main Custom Theme dark
  • Secondairy Theme light
  • Secondairy Theme dark
  • Secondairy Custom Theme light
  • Secondairy Custom Theme dark

These are 8 themes that you would keep track of so you are using both the right themes (Main and Secondary), but also both the right brightness (light/dark) variants of these themes.

You could end up with 16 different configurations, while only 4 are valid.

And this is exaxtly what Extended Theme solves. It will take the default Theme and merges it with your custom properties into a single custom theme so you can never mix them up.

How does it work?

Extended Theme relies on code generation to create a whole new custom theme with all the needed utilities based off the definition of your custom properties.

A very basic definition example:

@ExtendedTheme()
abstract class _MyTheme {
    Color get myCustomColor;
}

Now you can run the following command in the terminal:

flutter pub run build_runner build

This will generate the following:

  1. A widget called MyTheme (based of the definition name) which you can use almost the same as the normal Theme widget. In fact, this widget will also add the default Theme to the tree so whenever you scope MyTheme it will keep your custom properties and the defualt theme in sync.
  2. A class called MyThemeData which will hold your custom properties as well as the default ThemeData.
  3. An InheritedWidget called $MyTheme that allows you to get MyThemeData anywhere in the tree.

How do I use it?

Now that you've got your custom theme you can start using it.

Defining your themes

You simply define your theme just like you used to do with ThemeData.

final myLightTheme = MyThemeData(
    themeData: ThemeData.light(),
    myCustomColor: Colors.orange,
);

final myDarkTheme = MyThemeData(
    themeData: ThemeData.dark(),
    myCustomColor: Colors.red,
);

Add the Extended Theme to the tree

To have access to your custom properties, we must add MyTheme to the tree.

Usually you set ThemeData to the theme field of MaterialApp, this will put it above the Navigator and makes sure it's available throughout the app.

We can't just put our MyThemeData in the theme field, because it's not a ThemeData subtype.

To inject MyTheme above the MaterialApp Navigator, we can use the builder field.

You can do it manually:

return MaterialApp(
    title: 'ExtendedTheme Demo',
    builder: (context, child) => MyTheme(
        light: myLightTheme,
        dark: myDarkTheme,
        child: child,
    ),
    home: const Content(),
);

Which might be usefull if you're injecting other widgets, or you can use the conveniently generated builder:

return MaterialApp(
    title: 'ExtendedTheme Demo',
    builder: MyTheme.builder(
        light: myLightTheme,
        dark: myDarkTheme,
    ),
    home: const Content(),
);

If you happen to have multiple distinctive themes in your app, as mentioned in the beginning, you can scope MyTheme to a specific section of the app by just adding it in the widget tree whereever you need it:

return MyTheme(
    light: mySecondaryLightTheme,
    dark: mySecondaryDarkTheme,
    child: SecondaryThemedPage(),
),

Using your custom properties

Accessing your custom properties is as easy as:

return Container(
    color: MyTheme.of(context).myCustomColor,
);

ExtendedThemeData

In our previous example we only used a simple property. But what if we want to add styling for a custom widget?

Just like the ExtendedTheme you are able to define ExtendedThemeData.

Let's say we have a custom widget which has a couple of properties:

@ExtendedThemeData
abstract class _MyWidget {
    Color get backgroundColor;
    Color get foregroundColor;
    double? get width;
    double? get height;
    BoxShape get shape;
}

This will generate a class called MyThemeData that will hold the properties we've defined and a proper constructor.

TODO

  • Add convenient methods copyWith, merge, etc
  • Add default values
You might also like...

A set of utilities, themes and components for RTU MIREA applications by Mirea Ninja.

A set of utilities, themes and components for RTU MIREA applications by Mirea Ninja.

rtu-app-core - это красиво оформленный пакет компонентов графического интерфейса и многоцелевой набор утилит, разработанный для Flutter. rtu-app-core

Aug 10, 2022

🎨 An opinionated, effective and correct way to provide multiple themes to your app.

🎨 An opinionated, effective and correct way to provide multiple themes to your app.

theming This is an opinionated and effective way to provide multi-theme choice for your app. theming depends on provider and shared_preference for sta

Nov 28, 2022

A Flutter material theme editor

A Flutter material theme editor

Flutter Theme ⚠️ WARNING: This app is still under development so please expect bugs and missing features in the app. ⚠️ Inspired by Panache, a Flutter

Jan 2, 2022

Responsive Blog Theme using Flutter | Web, macOS, Android, iOS

 Responsive Blog Theme using Flutter | Web, macOS, Android, iOS

Responsive Blog Theme using Flutter | Web, macOS, Android, iOS Watch it on YouTube Packages we are using: flutter_svg: link get: link Flutter recently

Dec 9, 2022

XDG theme icons for Flutter

XDG theme icons for Flutter

XDG Icons XDG theme icons for Flutter. https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html TODO api cleanup docs intern

Jul 12, 2022

Flutter theme demo using riverpod and shared preferences

flutter_theme_demo A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you start

Dec 27, 2021

Easiest way to add support for light and dark theme in your flutter app.

Easiest way to add support for light and dark theme in your flutter app.

Adaptive Theme Easiest way to add support for light and dark theme in your Flutter app. It allows to manually set light or dark theme and also lets yo

Dec 27, 2022

🎨 Flutter Material Theme editor

🎨 Flutter Material Theme editor

🎨 Panache A Flutter Material Theme editor. Panache helps you to create beautiful Material themes for your Flutter applications. Customize widgets col

Dec 30, 2022
Comments
  • ext_theme_generator doesn't work in combination with some other generators

    ext_theme_generator doesn't work in combination with some other generators

    I've noticed that when I use ext_theme_generator as a single generator, all is fine, but in case there another generators like , e.g. auto_route_generator or hive_generator theme generator seems to be skipped (all the generation process finished with no issues) and no *.g.dart file with custom themes :-/ . FYI: When I use theme generator in parallel with (only) freezed generator, all is fine and I it ends up with generated theme file as expected. I've tested it on the example project from this package and the result is the same as in my project.

    Doctor summary (to see all details, run flutter doctor -v):
    [✓] Flutter (Channel stable, 2.10.3, on macOS 12.2.1 21D62 darwin-x64, locale en-PL)
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0-rc4)
    [✓] Xcode - develop for iOS and macOS (Xcode 13.2)
    [✓] Android Studio (version 2021.1)
    [✓] VS Code (version 1.65.0)
    [!] Connected device
        ! Error: XXX iPhone is not connected. Xcode will continue when XXX iPhone is connected. (code -13)
    [✓] HTTP Host Availability
    
    ! Doctor found issues in 1 category.
    
    bug 
    opened by speszke 3
  • ext_theme can not use with dart_json_mapper.

    ext_theme can not use with dart_json_mapper.

    Hi, I am using your library and dart_json_mapper. Both use Reflection to generate the file. Your library requires a very new version of Reflection, so the two cannot be used together. Can you reduce the version Reflection is using in your library?

    Resolving dependencies...
    The current Dart SDK version is 2.16.0-134.5.beta.
    
    Because dart_json_mapper <0.1.1 requires SDK version >=1.12.0 <=2.0.0-dev.58.0 and dart_json_mapper >=1.0.3 <1.1.0 depends on build ^0.12.7+3, dart_json_mapper <0.1.1-∞ or >=1.0.3 <1.1.0-∞ requires build ^0.12.7+3.
    And because dart_json_mapper >=0.1.3 <1.0.3 depends on build ^0.12.0 and dart_json_mapper >=1.5.9 <1.5.16 depends on reflectable ^2.2.4, dart_json_mapper <0.1.1-∞ or >=0.1.3 <1.1.0-∞ or >=1.5.9 <1.5.16-∞ requires build ^0.12.0 or reflectable ^2.2.4.
    And because dart_json_mapper >=1.5.7 <1.5.9 depends on reflectable ^2.2.3 and dart_json_mapper >=1.3.4 <1.5.7 depends on reflectable ^2.2.1+2, dart_json_mapper <0.1.1-∞ or >=0.1.3 <1.1.0-∞ or >=1.3.4 <1.5.16-∞ requires build ^0.12.0 or reflectable ^2.2.1+2.
    And because dart_json_mapper >=1.3.3 <1.3.4 depends on reflectable 2.2.0 and dart_json_mapper >=1.2.7 <1.3.3 depends on reflectable ^2.2.1, dart_json_mapper <0.1.1-∞ or >=0.1.3 <1.1.0-∞ or >=1.2.7 <1.5.16-∞ requires build ^0.12.0 or reflectable 2.2.0 or >=2.2.1 <3.0.0.
    And because dart_json_mapper >=1.2.0 <1.2.7 depends on reflectable ^2.2.0 and dart_json_mapper >=1.1.12 <1.2.0 depends on reflectable ^2.1.0, dart_json_mapper <0.1.1-∞ or >=0.1.3 <1.1.0-∞ or >=1.1.12 <1.5.16-∞ requires build ^0.12.0 or reflectable ^2.1.0.
    And because dart_json_mapper >=1.1.11 <1.1.12 depends on reflectable ^2.0.12 and dart_json_mapper >=1.1.2 <1.1.11 depends on reflectable ^2.0.10+1, dart_json_mapper <0.1.1-∞ or >=0.1.3 <1.1.0-∞ or >=1.1.2 <1.5.16-∞ requires build ^0.12.0 or reflectable ^2.0.10+1.
    And because dart_json_mapper >=1.1.0 <1.1.2 depends on reflectable ^2.0.8 and dart_json_mapper >=0.1.1 <0.1.3 depends on reflectable ^2.0.0, dart_json_mapper <1.5.16 requires build ^0.12.0 or reflectable ^2.0.0.
    And because reflectable >=0.1.1 <2.0.2 requires SDK version >=1.8.0 <2.0.0-dev.infinity and dart_json_mapper >=2.1.17 depends on analyzer ^2.7.0, dart_json_mapper <1.5.16-∞ or >=2.1.17 requires build ^0.12.0 or reflectable ^2.0.2 or analyzer ^2.7.0.
    And because dart_json_mapper >=2.1.13 <2.1.17 depends on analyzer ^2.5.0 and dart_json_mapper >=2.1.7 <2.1.13 depends on analyzer ^2.2.0, dart_json_mapper <1.5.16-∞ or >=2.1.7 requires build ^0.12.0 or reflectable ^2.0.2 or analyzer ^2.2.0.
    And because dart_json_mapper >=2.1.4 <2.1.7 depends on analyzer ^2.0.0 and dart_json_mapper >=2.1.3 <2.1.4 depends on analyzer ^1.7.0, dart_json_mapper <1.5.16-∞ or >=2.1.3 requires build ^0.12.0 or reflectable ^2.0.2 or analyzer ^1.7.0 or ^2.0.0.
    And because dart_json_mapper >=2.1.0 <2.1.3 depends on analyzer ^1.5.0 and dart_json_mapper >=2.0.1 <2.1.0 depends on analyzer ^1.1.0, dart_json_mapper <1.5.16-∞ or >=2.0.1 requires build ^0.12.0 or reflectable ^2.0.2 or analyzer >=1.1.0 <2.0.0 or ^2.0.0.
    And because dart_json_mapper >=1.6.3 <2.0.1 depends on analyzer >=0.40.3 <=0.50.4 and dart_json_mapper >=1.6.0 <1.6.3 depends on reflectable ^2.2.6, dart_json_mapper <1.5.16-∞ or >=1.6.0 requires build ^0.12.0 or reflectable ^2.0.2 or analyzer >=0.40.3 <=0.50.4 or >=1.1.0 <2.0.0 or ^2.0.0.
    And because dart_json_mapper >=1.5.16 <1.6.0 depends on reflectable ^2.2.5 and every version of ext_theme_generator depends on analyzer ^3.1.0, if dart_json_mapper any and ext_theme_generator any then build ^0.12.0 or reflectable ^2.0.2.
    Because reflectable >=2.0.2 <3.0.0-nullsafety.0 depends on logging ^0.11.0 and build >=2.0.0 depends on logging ^1.0.0, reflectable >=2.0.2 <3.0.0-nullsafety.0 is incompatible with build >=2.0.0.
    Thus, one of build >=2.0.0 or dart_json_mapper any or ext_theme_generator any must be false.
    And because every version of ext_theme_generator depends on build ^2.2.1, dart_json_mapper is incompatible with ext_theme_generator.
    So, because langlearning depends on both dart_json_mapper any and ext_theme_generator any, version solving failed.
    
    enhancement 
    opened by o7planning 1
Owner
Stephan E.G. Veenstra
Flutter enthousiast.
Stephan E.G. Veenstra
Flutter App Change Theme With Getx and Save Theme Stage with Hive

Flutter Change app Theme With GetX Flutter App Change Theme With Getx. Theme Stage saved using Hive. instead of hive you can use get-storage or shared

Azraf Al Monzim 12 Oct 22, 2022
An extended version of Flutter Colors with more swatches and more flexibility to generate your own custom swatch.

Colours An extended version of Flutter Colors with more swatches and more flexibility to generate your own custom swatch. Getting Started In your flut

Salman S 4 Nov 23, 2021
Flutter chat-app UI with multiple themes & light + Dark mode.

Chat-App UI Only a PART of code available, for complete code ping here Features of the app - Light Mode + Dark Mode 4 different color themes - pink/te

Deepa Pandey 31 Oct 5, 2022
Implements GTK Widgets, themes and titlebar buttons in Flutter. Based on the GNOME HIG

GTK ❤️ Flutter Unofficial implementation of GTK Widgets, themes and titlebar buttons in Flutter. Based on the GNOME Human Interface Guidelines. Featur

Prateek SU 164 Dec 26, 2022
The Flutter plugin that help you can choose dates and years with rounded calendars and customizable themes.

Flutter Rounded Date Picker The Flutter plugin that help you can choose dates and years with rounded calendars and customizable themes. Installing Add

benznest 313 Dec 22, 2022
A Flutter package to make and use beautiful color scheme based themes.

FlexColorScheme Use FlexColorScheme to make beautiful color scheme based Flutter themes, with optional primary color surface blends. The themes are ba

Rydmike 459 Jan 1, 2023
A Package to implement, change and use Themes in Flutter

Modern Themes Github: https://github.com/Jules-Media/Modern_Themes Pub.dev: https://pub.dev/packages/modern_themes About With this Plugin you can impl

Jules Media 2 Jun 22, 2022
Flutter themes consistent with GitHub's Primer style guidelines

primer_flutter Flutter themes consistent with Primer style guidelines DISCLAIMER: This project is not affiliated with the Primer or GitHub organizatio

Reuben Turner 6 Aug 24, 2022
This perfect starter kit is an app based on React Native and UI Kitten library with Light and Dark themes support.

Kitten Tricks This perfect starter kit is an app based on React Native and UI Kitten library with Light and Dark themes support. It’s completely free

Akveo 7k Dec 30, 2022
Experiment with dark themes based on popular apps.

Fast Dark Theme This project is a Flutter web experiment. It allows you to prototype dark mode colors based on popular apps: WhatsApp, Twitter and Sha

Bernardo Ferrari 47 Dec 10, 2021