Giter VIP home page Giter VIP logo

color-theme-provider's Introduction

Color Theme Provider

Use your custom theme without relying on Material Design or Cupertino design tokens.

The ColorThemeProvider package lets you define your own color scheme according to your app's needs.

Features

  • Define your own color properties. No need to depend on Google's ColorScheme or Apple's CupertinoThemeData
  • Automatically uses the theme parameter when the device is in light mode
  • Uses the darkTheme parameter when the device is in dark mode
  • Properly scoped and accessible within the widget tree

image caption

Usage

Import it

Use pub add in your project:

flutter pub add color_theme_provider

Or manually add the package to your pubspec.yaml

dependencies:
  
  color_theme_provider: 1.1.1

Defining your own theme

To make your own theme, create a base class that extends or implements the ColorTheme abstract class and define your required properties.

For example:

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

abstract class MyTheme implements ColorTheme {
  Color get mainColor;
  Color get containerColor;
  Color get backgroundColor;
  Color get textColor;
}

final class LightMyTheme implements MyTheme {
  @override
  final Color mainColor = const Color(0xFF7BD3EA);

  @override
  final Color containerColor = const Color(0xFFA1EEBD);

  @override
  final Color backgroundColor = const Color(0xFFFCFCFC);

  @override
  final Color textColor = Colors.black;
}

final class DarkMyTheme implements MyTheme {
  @override
  final Color mainColor = const Color(0xFF7BD3EA);

  @override
  final Color containerColor = const Color(0xFF007F73);

  @override
  final Color backgroundColor = const Color(0xFF2C2C2C);

  @override
  final Color textColor = Colors.white;
}

Accessing your theme through ColorThemeProvider

The ColorThemeProvider uses an InheritedNotifier under the hood. This means your custom theme can be accessed if your widgets have the same scope as the theme provider.

ColorThemeProvider(
    theme: LightMyTheme,
    darkTheme: DarkMyTheme(),
    child: const MyApp(),
),

Using the color theme

If your widgets are under the ColorThemeProvider widget tree, you can access your theme through the context. You can get the nearest ColorTheme using the BuildContext's extension function colorTheme<T>.

final theme = context.colorTheme<MyTheme>();
//Access the properties of your custom theme
theme.mainColor;
theme.backgroundColor;
theme.textColor;

Setting the color theme dynamically

It is possible to change the theme used for light or darkmode while running the app through the ColorThemeManager. Obtain this instance using the BuildContext.

  final colorThemeManager = context.colorThemeManager<MyTheme>();

  /// Update current light theme
  colorThemeManager.setTheme(newTheme);

  /// Update current dark theme
  colorThemeManager.setDarkTheme(newTheme);

Checking if platform is in dark mode

Use the ColorThemeManager to check whether the platform is in dark mode or not.

  final colorThemeManager = context.colorThemeManager<MyTheme>();

  /// Check whether app is in light or darkmode
  /// [Brightness.dark] for dark mode and [Brightness.light] for light mode;
  colorThemeManager.getCurrentMode();

color-theme-provider's People

Contributors

themobilecoder avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.