Skip to content

Commit

Permalink
rename theme mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
aprosail committed Jun 30, 2024
1 parent c12bac9 commit 238bfee
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
10 changes: 5 additions & 5 deletions example/common/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class App extends StatelessWidget {
return builder((context) {
final platformBrightness = MediaQuery.of(context).platformBrightness;
final brightness = context.findAndTrust<Brightness>();
final theme = context.findAndTrust<ThemeData>();
final theme = context.findAndTrust<Theme>();
return [
'Platform brightness: ${platformBrightness.name}'.asText,
'Theme brightness: ${brightness.name}'.asText,
Expand All @@ -24,19 +24,19 @@ class App extends StatelessWidget {
].asColumn;
})
.center
.theme(light: const ThemeData.light(), dark: const ThemeData.dark())
.theme(light: const Theme.light(), dark: const Theme.dark())
.ensureDirection(context)
.ensureMedia(context);
}
}

class ThemeData with Theme {
const ThemeData.light({
class Theme with ThemeMixin {
const Theme.light({
this.background = MonoColors.snow,
this.foreground = MonoColors.ink,
});

const ThemeData.dark({
const Theme.dark({
this.background = MonoColors.night,
this.foreground = MonoColors.lunar,
});
Expand Down
42 changes: 32 additions & 10 deletions lib/src/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,35 @@ import 'package:flutter/widgets.dart';
import 'inherit.dart';
import 'wrap.dart';

mixin Theme {
/// Default background of the theme.
/// It's recommended to use a final parameter rather than a getter.
/// All handled theme data should have basic properties defined in this mixin.
///
/// The [foreground] and [background] are elementary components of a theme,
/// without such parameters, the theme will be useless.
/// So that they are required.
///
/// It's recommended to use a final parameter rather than a getter.
/// It is defined as a getter here because of the limit of Dart syntax.
/// For example:
///
/// ```dart
/// class Theme with ThemeMixin {
/// const Theme({
/// required this.background,
/// required this.foreground,
/// })
///
/// @override
/// final Color background;
///
/// @override
/// final Color foreground;
/// }
/// ```
mixin ThemeMixin {
/// Default background of the theme, see [ThemeMixin].
Color get background;

/// Default foreground, text and icon color, of the theme.
/// It's recommended to use a final parameter rather than a getter.
/// Default foreground, text and icon color, of the theme, see [ThemeMixin].
Color get foreground;
}

Expand All @@ -27,7 +49,7 @@ extension WrapTheme on Widget {
/// [light] theme directly when there's no [dark] theme,
/// because even if the [dark] theme is not provided,
/// it might be modifier by its descendants.
Widget theme<T extends Theme>({
Widget theme<T extends ThemeMixin>({
required T light,
T? dark,
ThemeMode mode = ThemeMode.system,
Expand All @@ -40,11 +62,11 @@ extension WrapTheme on Widget {
);
}

/// Handle a [Theme] and the [ThemeMode],
/// Handle a [ThemeMixin] and the [ThemeMode],
/// and it will also provide the [Brightness] of current theme.
/// And you can also modify current [Theme] and [ThemeMode] from
/// And you can also modify current [ThemeMixin] and [ThemeMode] from
/// its descendants in the widget tree via the context.
class ThemeHandler<T extends Theme> extends StatefulWidget {
class ThemeHandler<T extends ThemeMixin> extends StatefulWidget {
const ThemeHandler({
super.key,
required this.light,
Expand All @@ -62,7 +84,7 @@ class ThemeHandler<T extends Theme> extends StatefulWidget {
State<ThemeHandler<T>> createState() => _ThemeHandlerState<T>();
}

class _ThemeHandlerState<T extends Theme> extends State<ThemeHandler<T>> {
class _ThemeHandlerState<T extends ThemeMixin> extends State<ThemeHandler<T>> {
late T _light = widget.light;
late T _dark = widget.dark;
late ThemeMode _mode = widget.mode;
Expand Down
2 changes: 1 addition & 1 deletion test/theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void main() {
});
}

class CustomizedTheme with Theme {
class CustomizedTheme with ThemeMixin {
const CustomizedTheme.light({
this.background = MonoColors.snow,
this.foreground = MonoColors.coal,
Expand Down

0 comments on commit 238bfee

Please sign in to comment.