A Flutter package that provides a simple way to manage theme modes in your app.
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.
❗ 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
- Import the
ThemeGeniusWrapper
widget, wrapMaterialApp
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.
- Set the
defaultThemeMode
inThemeGeniusWrapper
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) inThemeGeniusWrapper
with:
@override
Widget build(BuildContext context) {
return ThemeGeniusWrapper(
builder: (themeMode) {
return MaterialApp(...);
},
placeholder: const Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
),
);
}
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.
MIT © 2012-2023 Scott Chacon and others