shared_theme 0.1.2

  • Example
  • Installing
  • Versions
  • 77

Shared Theme

Easily share a theme between Flutter and the web.


A Theme is a ColorSet, FontSet, and an ElementSet, which are abstract containers for defining properties such as border, padding, etc.

Themes are typically combined into a single ThemeSet per application. ThemeSet adds support for bundled FontFaces, and ThemeSet.toString() returns an SCSS string that fully represents it.

In Flutter, a Theme is converted into Flutter's ThemeData by using the shared_theme_flutter package's themeData() function.


There is a complete example included, and in particular, see its ThemeSet definition.


Mobile lightMobile dark
Web lightWeb dark

[0.1.2] - September 27, 2018

  • Add persistence to examples, doc improvements.

[0.1.1] - September 26, 2018

  • Analyzer fixes.

[0.1.0] - September 26, 2018

  • Doc improvements.

[0.0.2] - September 26, 2018

  • First release.


// For a complete working example, see
import 'package:shared_theme/shared_theme.dart';

/// See the theme colors at
final themeset = ThemeSet(themes: [
      name: 'Light',
      brightness: Brightness.light,
      colors: _lightColors,
      fonts: _darkFonts,
      elements: _lightElements),
      name: 'Dark',
      brightness: Brightness.dark,
      colors: _darkColors,
      fonts: _lightFonts,
      elements: _darkElements),
], fontFaces: _fontFaces);

// Declaring common theme colors here allows you to refer to them as a default
// value in contructors.
const _primary = ContrastingColors(Color(0xff2e7d32), Colors.white);
const _primaryLight = ContrastingColors(Color(0xff60ad5e),;
const _primaryDark = ContrastingColors(Color(0xff005005), Colors.white);
const _secondary = ContrastingColors(Color(0xffffab00),;
const _secondaryLight = ContrastingColors(Color(0xffffdd4b),;
const _secondaryDark = ContrastingColors(Color(0xffc67c00),;
const _error = ContrastingColors(Colors.error, Colors.onError);
const _notice = ContrastingColors(Color(0xffb39ddb),;

/// This helps define [ColorSet]s that share the same colors (e.g. brand colors).
class _ThemeColors extends ColorSet {
  const _ThemeColors({
    ContrastingColors background,
    ContrastingColors surface,
    ContrastingColors divider,
  }) : super(
          primary: _primary,
          primaryLight: _primaryLight,
          primaryDark: _primaryDark,
          secondary: _secondary,
          secondaryLight: _secondaryLight,
          secondaryDark: _secondaryDark,
          error: _error,
          notice: _notice,
          background: background,
          scaffold: surface,
          dialog: surface,
          card: surface,
          divider: divider,
          selectedRow: divider,
          indicator: _secondary,
          textSelection: _primaryLight,
          textSelectionHandle: _primaryDark,

const _lightColors = _ThemeColors(
  background: ContrastingColors(Color(0xfffefefe),,
  surface: ContrastingColors(Color(0xfffefefe),,
  divider: ContrastingColors(Color(0xffeeeeee),,

const _darkColors = _ThemeColors(
  background: ContrastingColors(Color(0xff333333), Colors.white),
  surface: ContrastingColors(Color(0xff333333), Colors.white),
  divider: ContrastingColors(Color(0xff484848), Colors.white),

final _baseFonts = FontSet.dark.apply(family: 'Open Sans');

// The default font set.
final _darkFonts = _baseFonts.copyWith(
    headline: FontSet.dark.headline
        .copyWith(weight: 700, height: 2.5, family: 'Ubuntu'),
    title: FontSet.dark.title.copyWith(weight: 700, family: 'Ubuntu'),
    subhead: FontSet.dark.subhead
        .copyWith(size: 18.0, height: 1.75, weight: 300, family: 'Ubuntu'),
    body2: FontSet.dark.body2.copyWith(size: 18.0, weight: 600),
    body1: FontSet.dark.body1.copyWith(size: 16.0, height: 1.15),
    caption: FontSet.dark.caption.copyWith(size: 16.0));

// Copy and apply the corresponding light color to each font.
final _lightFonts = _darkFonts.lighten();

class _ButtonBase extends Element {
      {Color color: Colors.transparent,
      Font font,
      ShadowElevation shadow: ShadowElevation.none})
      : super(
            padding: BoxSpacing.symmetric(vertical: 4.0, horizontal: 8.0),
            border: Border(radii: BorderRadius(4.0)),
            font: font ?? _darkFonts.button,
            shadow: shadow,
            color: color);

/// Default elements.
final _lightElements = ElementSet(
  primaryButton: _ButtonBase(
      color: _lightColors.secondary.color,
      font: _darkFonts.button
          .copyWith(color: _lightColors.secondary.contrast, size: 16.0),
      shadow: ShadowElevation.dp8),
  secondaryButton: _ButtonBase(
      color: _lightColors.primary.color,
      font: _darkFonts.button.copyWith(color: _lightColors.primary.contrast)),
  tertiaryButton: _ButtonBase(),
  inputBase: Element.outlineInput,

/// Same as [_lightElements] but with a different text color on the tertiary button.
final _darkElements = _lightElements.copyWith(
    tertiaryButton: _lightElements.tertiaryButton.copyWith(
        font: _lightElements.tertiaryButton.font
            .copyWith(color: _lightFonts.button.color)));

/// These will be included in the CSS, and someday into clients' Flutter app,
/// but for now they must be copied into your Flutter app's pubspec.yaml. The
/// URL is exactly the same though.
final _fontFaces = [
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-Regular.ttf'),
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-SemiBold.ttf',
      weight: 500),
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-Light.ttf',
      weight: 300),
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-Bold.ttf',
      weight: 700),
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-Italic.ttf',
      style: FontStyle.italic),
      family: 'Ubuntu',
      url: 'packages/sharedtheme_example/assets/fonts/Ubuntu-Regular.ttf'),
      family: 'Ubuntu',
      url: 'packages/sharedtheme_example/assets/fonts/Ubuntu-Light.ttf',
      weight: 300),
      family: 'Ubuntu',
      url: 'packages/sharedtheme_example/assets/fonts/Ubuntu-Bold.ttf',
      weight: 700),

void main() {
  /// A very long string.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  shared_theme: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:shared_theme/shared_theme.dart';
Version Uploaded Documentation Archive
0.1.2 Sep 27, 2018 Go to the documentation of shared_theme 0.1.2 Download shared_theme 0.1.2 archive
0.1.1 Sep 26, 2018 Go to the documentation of shared_theme 0.1.1 Download shared_theme 0.1.1 archive
0.1.0 Sep 26, 2018 Go to the documentation of shared_theme 0.1.0 Download shared_theme 0.1.0 archive
0.0.2 Sep 26, 2018 Go to the documentation of shared_theme 0.0.2 Download shared_theme 0.0.2 archive
0.0.1 Sep 26, 2018 Go to the documentation of shared_theme 0.0.1 Download shared_theme 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:shared_theme/shared_theme.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.1.6 1.1.6
Dev dependencies
test ^1.3.2