Skip to content

A Flutter package that provides a simple way to manage theme modes in your app.

License

Notifications You must be signed in to change notification settings

tizianotedeschi/theme-genius

Repository files navigation

ThemeGenius

version coverage style: very good analysis Powered by Mason License: MIT

A Flutter package that provides a simple way to manage theme modes in your app.


Migration from 1.x to 2.x 🚀

In the new version 2.x, we migrated from the legacy SharedPreferences API to the new SharedPreferencesAsync API. This change allows you to avoid using an API that will be deprecated in the future. See this link for more information.

Installation 💻

❗ In order to start using ThemeGenius you must have the Flutter SDK installed on your machine.

  • Install via flutter pub add:
dart pub add theme_genius
  • Or add this to your pubspec.yaml:
dependencies:
  theme_genius: ^2.0.0

Usage 📖

  • Import the ThemeGeniusWrapper widget, wrap MaterialApp with it:
import 'package:theme_genius/theme_genius.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ThemeGeniusWrapper(
      builder: (themeMode) {
        return MaterialApp(
          themeMode: themeMode,
          theme: ThemeData.light(),
          darkTheme: ThemeData.dark(),
          home: const MyHomePage(
            title: 'Flutter Demo Home Page',
          ),
        );
      },
    );
  }
}
  • Get the current themeMode with:
final themeMode = await ThemeGenius.getThemeMode(context);
  • Change and save the themeMode with:
final themeMode = ThemeMode.dark;
await ThemeGenius.setThemeMode(context, themeMode: themeMode);
  • Load the saved themeMode with:
final themeMode = await ThemeGenius.loadThemeMode();

Check the example folder for a complete example.


Extra 📖

  • Set the defaultThemeMode in ThemeGeniusWrapper with:
@override
Widget build(BuildContext context) {
  return ThemeGeniusWrapper(
    builder: (themeMode) {
      return MaterialApp(...);
    },
    defaultThemeMode: ThemeMode.dark,
  );
}
  • Set a custom placeholder (a widget to display while the theme mode is being loaded) in ThemeGeniusWrapper with:
@override
Widget build(BuildContext context) {
  return ThemeGeniusWrapper(
    builder: (themeMode) {
      return MaterialApp(...);
    },
    placeholder: const Scaffold(
      body: Center(
        child: CircularProgressIndicator(),
      ),
    ),
  );
}

Continuous Integration 🤖

Theme Genius comes with a built-in GitHub Actions workflow powered by Very Good Workflows but you can also add your preferred CI/CD solution.

Out of the box, on each pull request and push, the CI formats, lints, and tests the code. This ensures the code remains consistent and behaves correctly as you add functionality or make changes. The project uses Very Good Analysis for a strict set of analysis options used by our team. Code coverage is enforced using the Very Good Workflows.

License 📄

MIT © 2012-2023 Scott Chacon and others