shared_theme 0.1.2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new67

Shared Theme

Easily share a theme between Flutter and the web.

Synopsis

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.

Example

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

Screenshots

Mobile lightMobile dark
mobile-lightmobile-dark
Web lightWeb 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.

example/example.dart

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

/// See the theme colors at
/// https://material.io/tools/color/#!/?view.left=1&view.right=0&secondary.color=FFAB00&primary.color=2E7D32
final themeset = ThemeSet(themes: [
  Theme(
      name: 'Light',
      brightness: Brightness.light,
      colors: _lightColors,
      fonts: _darkFonts,
      elements: _lightElements),
  Theme(
      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), Colors.black);
const _primaryDark = ContrastingColors(Color(0xff005005), Colors.white);
const _secondary = ContrastingColors(Color(0xffffab00), Colors.black);
const _secondaryLight = ContrastingColors(Color(0xffffdd4b), Colors.black);
const _secondaryDark = ContrastingColors(Color(0xffc67c00), Colors.black);
const _error = ContrastingColors(Colors.error, Colors.onError);
const _notice = ContrastingColors(Color(0xffb39ddb), Colors.black);

/// 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), Colors.black),
  surface: ContrastingColors(Color(0xfffefefe), Colors.black),
  divider: ContrastingColors(Color(0xffeeeeee), Colors.black),
);

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 {
  _ButtonBase(
      {Color color: Colors.transparent,
      Font font,
      ShadowElevation shadow: ShadowElevation.none})
      : super(
            align: TextAlign.center,
            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 = [
  FontFace(
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-Regular.ttf'),
  FontFace(
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-SemiBold.ttf',
      weight: 500),
  FontFace(
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-Light.ttf',
      weight: 300),
  FontFace(
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-Bold.ttf',
      weight: 700),
  FontFace(
      family: 'Open Sans',
      url: 'packages/sharedtheme_example/assets/fonts/OpenSans-Italic.ttf',
      style: FontStyle.italic),
  FontFace(
      family: 'Ubuntu',
      url: 'packages/sharedtheme_example/assets/fonts/Ubuntu-Regular.ttf'),
  FontFace(
      family: 'Ubuntu',
      url: 'packages/sharedtheme_example/assets/fonts/Ubuntu-Light.ttf',
      weight: 300),
  FontFace(
      family: 'Ubuntu',
      url: 'packages/sharedtheme_example/assets/fonts/Ubuntu-Bold.ttf',
      weight: 700),
];


void main() {
  /// A very long string.
  print(themeset.toString());
}

Use this package as a library

1. Depend on it

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


dependencies:
  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
Popularity:
Describes how popular the package is relative to other packages. [more]
35
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
67
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.12.4

Platforms

Detected platforms: Flutter, web, other

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

Dependencies

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