Craigt
Craigt

Reputation: 3580

Is it possible "extend" ThemeData in Flutter

I might very well be missing something as I'm so new to flutter, but I'm finding ThemeData's options very limited (at least with my understanding of how to implement it).

If you look at this random design below from MaterialUp, I'd want to model something roughly like:

Themedata.cyclingColor = Color.pink; ThemeData.runningColor = Color.green;

That way everywhere in my app I can reference cycling, running, swimming, gym colors (Or whatever colors make sense in the context of my app/design) and keep things consistent.

Random design from MaterialUp

Is there a recommended way to achieve this currently in Flutter? What are my options?

Upvotes: 63

Views: 31880

Answers (12)

mrgnhnt96
mrgnhnt96

Reputation: 3945

I have also found that the ThemeData is restricting. What I have done, and will be doing for all of my apps in the future is creating my own ThemeData.

I have created a file named color_themes.dart and created a class named ColorThemes with constructors with the name of the colors that I desire. such as cyclingColor;

class ColorThemes {
    static const cyclingColor = const Color(0xffb74093); 
}

You can then call these colors by importing the file and calling ColorThemes.cyclingColor You can assign these values within your ThemeData to have these colors default to your ColorThemes. One of the benefits with using this method is that you do not need to use/reference context like so ThemeData.of(context) making it a lot easier to use your code in extracted widgets.

Upvotes: 5

MDAB_flutterdev
MDAB_flutterdev

Reputation: 151

Instead of extending, you can use the new feature ThemeExtension in flutter. We can add custom styling and even use class type theme configuration in css.

example:


import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

@immutable
class MyColors extends ThemeExtension<MyColors> {
  const MyColors({
    required this.brandColor,
    required this.danger,
  });

  final Color? brandColor;
  final Color? danger;

  @override
  MyColors copyWith({Color? brandColor, Color? danger}) {
    return MyColors(
      brandColor: brandColor ?? this.brandColor,
      danger: danger ?? this.danger,
    );
  }

  @override
  MyColors lerp(ThemeExtension<MyColors>? other, double t) {
    if (other is! MyColors) {
      return this;
    }
    return MyColors(
      brandColor: Color.lerp(brandColor, other.brandColor, t),
      danger: Color.lerp(danger, other.danger, t),
    );
  }

  // Optional
  @override
  String toString() => 'MyColors(brandColor: $brandColor, danger: $danger)';
}

void main() {
  // Slow down time to see lerping.
  timeDilation = 5.0;
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool isLightTheme = true;

  void toggleTheme() {
    setState(() => isLightTheme = !isLightTheme);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: MyApp._title,
      theme: ThemeData.light().copyWith(
        extensions: <ThemeExtension<dynamic>>[
          const MyColors(
            brandColor: Color(0xFF1E88E5),
            danger: Color(0xFFE53935),
          ),
        ],
      ),
      darkTheme: ThemeData.dark().copyWith(
        extensions: <ThemeExtension<dynamic>>[
          const MyColors(
            brandColor: Color(0xFF90CAF9),
            danger: Color(0xFFEF9A9A),
          ),
        ],
      ),
      themeMode: isLightTheme ? ThemeMode.light : ThemeMode.dark,
      home: Home(
        isLightTheme: isLightTheme,
        toggleTheme: toggleTheme,
      ),
    );
  }
}

class Home extends StatelessWidget {
  const Home({
    Key? key,
    required this.isLightTheme,
    required this.toggleTheme,
  }) : super(key: key);

  final bool isLightTheme;
  final void Function() toggleTheme;

  @override
  Widget build(BuildContext context) {
    final MyColors myColors = Theme.of(context).extension<MyColors>()!;
    return Material(
      child: Center(
          child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(width: 100, height: 100, color: myColors.brandColor),
          const SizedBox(width: 10),
          Container(width: 100, height: 100, color: myColors.danger),
          const SizedBox(width: 50),
          IconButton(
            icon: Icon(isLightTheme ? Icons.nightlight : Icons.wb_sunny),
            onPressed: toggleTheme,
          ),
        ],
      )),
    );
  }
}

from Flutter API Documentation

Upvotes: 10

Lakshay Parnami
Lakshay Parnami

Reputation: 215

2022: Use ThemeExtensions introduced in flutter 3

Here's a link! to the medium article I wrote.

  1. Create your ThemeExtension class
import 'package:flutter/material.dart';

@immutable
class MyCardTheme extends ThemeExtension<MyCardTheme> {
  const MyCardTheme({
    this.background = Colors.white,
    this.shape = const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(
        Radius.circular(8),
      ),
    ),
  });

  final Color background;
  final ShapeBorder shape;

  @override
  MyCardTheme copyWith({
    Color? background,
    ShapeBorder? shape,
  }) {
    return MyCardTheme(
      background: background ?? this.background,
      shape: shape ?? this.shape,
    );
  }

  @override
  MyCardTheme lerp(ThemeExtension<MyCardTheme>? other, double t) {
    if (other is! MyCardTheme) {
      return this;
    }
    return MyCardTheme(
      background: Color.lerp(background, other.background, t) ?? Colors.white,
      shape: ShapeBorder.lerp(shape, other.shape, t) ??
          const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(8),
            ),
          ),
    );
  }

  @override
  String toString() => 'MyCardTheme('
      'background: $background, radius: $shape'
      ')';
}
  1. Create dark and light themes as per requirements
MyCardTheme lightCardTheme = MyCardTheme(
  background: Colors.blueGrey[200]!,
  shape: const RoundedRectangleBorder(
    borderRadius: BorderRadius.all(
      Radius.circular(24),
    ),
  ),
);

MyCardTheme darkCardTheme = MyCardTheme(
  background: Colors.blueGrey[800]!,
  shape: const RoundedRectangleBorder(
    borderRadius: BorderRadius.all(
      Radius.circular(24),
    ),
  ),
);
  1. Add extensions to your ThemeData for both, light and dark themes.
 theme: ThemeData(
    primarySwatch: Colors.green,
    cardTheme: const CardTheme(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(8),
        ),
      ),
      color: Colors.green,
    ),
    extensions: <ThemeExtension<dynamic>>[
      lightCardTheme,
    ],
  ),
  darkTheme: ThemeData(
    brightness: Brightness.dark,
    primarySwatch: Colors.green,
    cardTheme: const CardTheme(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(8),
        ),
      ),
      color: Colors.green,
    ),
    extensions: <ThemeExtension<dynamic>>[
      darkCardTheme,
    ],
  ),
  1. Use them in your build methods
final MyCardTheme customCardTheme =
        Theme.of(context).extension<MyCardTheme>()!;
Card(
    shape: customCardTheme.shape,
    color: customCardTheme.background,
    child: Container(
             padding: const EdgeInsets.all(16),
             child: const Text('Card styled from custom theme')),
          ),
    ),

Upvotes: 19

Limtis
Limtis

Reputation: 115

To extend (pun not intended) the answer of Maxim Saplin:

You may encounter a problem, where theme stays on the last one initialized in your code. This is happening because InputDecorationTheme is always the same for all of yours themes.

What solved it for me, was changing key (InputDecorationTheme) in _own to something unique, like themeID (you'll have to implement it somehow).

Upvotes: 0

Maxim Saplin
Maxim Saplin

Reputation: 4652

Updated for null-safety

I've extended standard ThemeData class so that at any time one could access own theme fields like that:

Theme.of(context).own().errorShade

Or like that:

ownTheme(context).errorShade

A theme can be defined and extended with new fields as follows(via addOwn() called on a certain ThemeData instance):

final ThemeData lightTheme = ThemeData.light().copyWith(
    accentColor: Colors.grey.withAlpha(128),
    backgroundColor: Color.fromARGB(255, 255, 255, 255),
    textTheme: TextTheme(
      caption: TextStyle(
          fontSize: 17.0, fontFamily: 'Montserrat', color: Colors.black),
    ))
  ..addOwn(OwnThemeFields(
      errorShade: Color.fromARGB(240, 255, 200, 200),
      textBaloon: Color.fromARGB(240, 255, 200, 200)));

final ThemeData darkTheme = ThemeData.dark().copyWith( ...
...

Themes can be applied to MaterialApp widget in a conventional way:

MaterialApp(
...
  theme: lightTheme,
  darkTheme: darkTheme,
)

The idea is to put all custom fields required for theming in a separate class OwnThemeFields.

Then extend ThemeData class with 2 methods:

  1. addOwn() that connects a certain instance of ThemedData to OwnThemeFields instance
  2. own() that allows to lookup for own fields associated with the given theme data

Also ownTheme helper method can be created to shorten the extraction of own fields.

class OwnThemeFields {
  final Color? errorShade;
  final Color? textBaloon;

  const OwnThemeFields({Color? errorShade, Color? textBaloon})
      : this.errorShade = errorShade,
        this.textBaloon = textBaloon;

  factory OwnThemeFields.empty() {
    return OwnThemeFields(errorShade: Colors.black, textBaloon: Colors.black);
  }
}
    
extension ThemeDataExtensions on ThemeData {
  static Map<InputDecorationTheme, OwnThemeFields> _own = {};

  void addOwn(OwnThemeFields own) {
    _own[this.inputDecorationTheme] = own;
  }

  static OwnThemeFields? empty = null;

  OwnThemeFields own() {
    var o = _own[this.inputDecorationTheme];
    if (o == null) {
      if (empty == null) empty = OwnThemeFields.empty();
      o = empty;
    }
    return o!;
  }
}

OwnThemeFields ownTheme(BuildContext context) => Theme.of(context).own();

Complete source: https://github.com/maxim-saplin/dikt/blob/master/lib/ui/themes.dart

Upvotes: 29

Stefan Galler
Stefan Galler

Reputation: 831

I solved this problem also for multiple themes by creating a CustomThemeData class like this:

class CustomThemeData {
    final double imageSize;

    CustomThemeData({
        this.imageSize = 100,
    });
}

Then, creating instances for each Theme:

final _customTheme = CustomThemeData(imageSize: 150);
final _customDarkTheme = CustomThemeData();

And writing an extension on ThemeData:

extension CustomTheme on ThemeData {
    CustomThemeData get custom => brightness == Brightness.dark ? _customDarkTheme : _customTheme;
}

Finally, the value can be accessed like this:

Theme.of(context).custom.imageSize

For more information see: https://bettercoding.dev/flutter/tutorial-themes-riverpod/#custom_attributes_extending_themedata

Upvotes: 4

use this lib adaptive_theme for theme switch. And create extension of ColorSheme

extension MenuColorScheme on ColorScheme {
      Color get menuBackground => brightness == Brightness.light
          ? InlLightColors.White
          : InlDarkColors.Black;
}

In widget use that

Container(
      color: Theme.of(context).colorScheme.menuBackground,
...
)

This way is very simple and elegance. Nice to codding.

Upvotes: 3

i4guar
i4guar

Reputation: 677

I created an implementation analog to the implementation of ThemeData:

@override
Widget build(BuildContext context) {
     final Brightness platformBrightness = Theme.of(context).brightness;
     final bool darkTheme = platformBrightness == Brightness.dark;

     return CustomAppTheme(
               customAppTheme:
                   darkTheme ? CustomAppThemeData.dark : CustomAppThemeData.light,
               child: Icon(Icons.add, color: CustomAppTheme.of(context).addColor,),
     );
}
import 'package:calendarflutter/style/custom_app_theme_data.dart';
import 'package:flutter/material.dart';

class CustomAppTheme extends InheritedWidget {
  CustomAppTheme({
    Key key,
    @required Widget child,
    this.customAppTheme,
  }) : super(key: key, child: child);

  final CustomAppThemeData customAppTheme;

  static CustomAppThemeData of(BuildContext context) {
    return context
        .dependOnInheritedWidgetOfExactType<CustomAppTheme>()
        .customAppTheme;
  }

  @override
  bool updateShouldNotify(CustomAppTheme oldWidget) =>
      customAppTheme != oldWidget.customAppTheme;
}
import 'package:flutter/material.dart';

class CustomAppThemeData {
  final Color plusColor;

  const CustomAppThemeData({
    @required this.plusColor,
  });

  static CustomAppThemeData get dark {
    return CustomAppThemeData(
      plusColor: Colors.red,
    );
  }

  static CustomAppThemeData get light {
    return CustomAppThemeData(
      plusColor: Colors.green,
    );
  }
}

Upvotes: 0

Nic Bradley
Nic Bradley

Reputation: 11

A simple workaround if you are not using all the textTheme headlines you can set some colors of some of them and use them like you normally use other colors.

set the headline1 color: ThemeData(textTheme: TextTheme(headline1: TextStyle(color: Colors.red),),),

Use it: RawMaterialButton(fillColor: Theme.of(context).textTheme.headline1.color,onPressed: onPressed,)

Upvotes: 1

bruceyuan
bruceyuan

Reputation: 75

Dart 2.7 later, extension support

you can add extension for system class

only add instance property is easy, but if you would get a dynamic color

you need think about it. for example, Use a constant to get the colors in light and dark modes

Determine if it is dark mode

two ways

  • MediaQuery.of(context).platformBrightnes == Brightness.dark;
  • Theme.of(context).brightness == Brightness.dark;

As you can see, you need the context, the context

Add Extension for BuildContext

Here is the code

extension MYContext on BuildContext {
  Color dynamicColor({int light, int dark}) {
    return (Theme.of(this).brightness == Brightness.light)
        ? Color(light)
        : Color(dark);
  }

  Color dynamicColour({Color light, Color dark}) {
    return (Theme.of(this).brightness == Brightness.light)
        ? light
        : dark;
  }

  /// the white background
  Color get bgWhite => dynamicColor(light: 0xFFFFFFFF, dark: 0xFF000000);
}

How to use

import 'package:flutter/material.dart';
import 'buildcontext_extension.dart';

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: context.bgWhite,
    );
  }
}

Also

This color may require multiple files, so you can create a public.dart file to manage it all

Like This

public.dart


library public;

// Export some common header files

// extensions
export 'buildcontext_extension.dart';

DarkMode images support

Put the light images in the same category as the dark ones

some code

static String getImgPath(String name, {
    String folder = '', 
    String format = 'png', 
    bool isDark = false, 
    bool needDark = true
  }) {
    String finalImagePath;
    if (needDark) {
      final folderName = isDark ? '${folder}_dark' : folder;
      finalImagePath = 'assets/images/$folderName/$name.$format';
    } else {
      finalImagePath = 'assets/images/$folder/$name.$format';
    }
    String isDarkPath = isDark ? "🌙 DarkMode" : "🌞 LightMode";
    print('$isDarkPath imagePath 🖼 $finalImagePath');
    return finalImagePath;
  }

Upvotes: 5

fhucho
fhucho

Reputation: 34530

I recommend this approach, which is simple, works with hot reload and can be easily extended to support switching between dark and light themes.

First create your own analog to ThemeData, let's call it AppThemeData:

class AppThemeData {
  final BorderRadius borderRadius = BorderRadius.circular(8);

  final Color colorYellow = Color(0xffffff00);
  final Color colorPrimary = Color(0xffabcdef);

  ThemeData get materialTheme {
    return ThemeData(
        primaryColor: colorPrimary
    );
  }
}

The materialTheme can be used whenever the standard ThemeData is needed.

Then create a widget called AppTheme, which provides an instance of AppThemeData using the provider package.

class AppTheme extends StatelessWidget {
  final Widget child;

  AppTheme({this.child});

  @override
  Widget build(BuildContext context) {
    final themeData = AppThemeData(context);
    return Provider.value(value: themeData, child: child);
  }
}

Finally, wrap the whole app with AppTheme. To access the theme you can call context.watch<AppThemeData>(). Or create this extension...

extension BuildContextExtension on BuildContext {
  AppThemeData get appTheme {
    return watch<AppThemeData>();
  }
}

... and use context.appTheme. I usually put final theme = context.appTheme; on the first line of the widget build method.

Upvotes: 29

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657148

You can't extend ThemeData because then material components won't find it anymore.

You can just create and provide MyThemeData in addition to the ThemeData included in Flutter the same way.

Create a widget CustomThemeWidget that extends InheritedWidget and provide your custom theme there.

When you want to get a value from the current theme use

myTheme = CustomThemeWidget.of(context).myTheme;

To change the current theme change the MyThemeData in CustomThemeWidget.myTheme

Update

Like shown in https://github.com/flutter/flutter/pull/14793/files, it should be possible to extend ThemeData and provide it as ThemeData by overriding runtimeType

See also the comment in https://github.com/flutter/flutter/issues/16487#event-1573761656

Upvotes: 7

Related Questions