Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/superpenguin/create_theme
codegen for flutter ThemeExtension
https://github.com/superpenguin/create_theme
dart flutter
Last synced: 12 days ago
JSON representation
codegen for flutter ThemeExtension
- Host: GitHub
- URL: https://github.com/superpenguin/create_theme
- Owner: SuperPenguin
- License: mit
- Created: 2022-07-14T13:06:24.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-25T05:02:37.000Z (5 months ago)
- Last Synced: 2024-10-11T09:55:21.017Z (29 days ago)
- Topics: dart, flutter
- Language: Dart
- Homepage:
- Size: 36.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Create Theme
codegen for flutter ThemeExtension# Installing
```
flutter pub add create_theme_annotation
flutter pub add --dev create_theme
```# Example
`my_widget.dart`
```dart
import 'package:create_theme_annotation/create_theme_annotation.dart';
import 'package:flutter/material.dart';part 'my_widget.g.dart';
MyWidgetThemeData _createDefault(ThemeData theme) {
return MyWidgetThemeData(
headerColor: theme.colorScheme.primary,
headerTextStyle: TextStyle(
color: theme.colorScheme.onPrimary,
),
backgroundColor: theme.scaffoldBackgroundColor,
);
}@CreateTheme(
themeProperties: {
'headerColor': CreateThemeColor(),
'headerTextStyle': CreateThemeTextStyle(),
'backgroundColor': CreateThemeColor(),
},
createDefault: _createDefault,
)
class MyWidget extends StatelessWidget {
const MyWidget({
Key? key,
}) : super(key: key);@override
Widget build(BuildContext context) {
final theme = MyWidgetTheme.of(context);return Column(
children: [
Material(
color: theme.headerColor,
child: DefaultTextStyle.merge(
style: theme.headerTextStyle,
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Center(
child: Text('Header'),
),
),
),
),
Expanded(
child: Material(
color: theme.backgroundColor,
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Center(
child: Text('Body'),
),
),
),
),
],
);
}
}class CreateThemeColor extends CreateThemeProperties {
const CreateThemeColor() : super(propertiesType: Color, lerp: Color.lerp);
}class CreateThemeTextStyle extends CreateThemeProperties {
const CreateThemeTextStyle()
: super(propertiesType: TextStyle, lerp: TextStyle.lerp);
}```
`my_widget.g.dart`
```dart
// GENERATED CODE - DO NOT MODIFY BY HANDpart of 'my_widget.dart';
// **************************************************************************
// CreateThemeGenerator
// **************************************************************************class MyWidgetThemeData extends ThemeExtension {
const MyWidgetThemeData({
this.headerColor,
this.headerTextStyle,
this.backgroundColor,
});final Color? headerColor;
final TextStyle? headerTextStyle;
final Color? backgroundColor;@override
MyWidgetThemeData copyWith({
Color? headerColor,
TextStyle? headerTextStyle,
Color? backgroundColor,
}) {
return MyWidgetThemeData(
headerColor: headerColor ?? this.headerColor,
headerTextStyle: headerTextStyle ?? this.headerTextStyle,
backgroundColor: backgroundColor ?? this.backgroundColor,
);
}@override
MyWidgetThemeData lerp(
ThemeExtension? other,
double t,
) {
if (other is! MyWidgetThemeData) return this;return MyWidgetThemeData(
headerColor: Color.lerp(headerColor, other.headerColor, t),
headerTextStyle:
TextStyle.lerp(headerTextStyle, other.headerTextStyle, t),
backgroundColor: Color.lerp(backgroundColor, other.backgroundColor, t),
);
}MyWidgetThemeData merge(MyWidgetThemeData? other) {
if (other == null) return this;return copyWith(
headerColor: other.headerColor,
headerTextStyle: other.headerTextStyle,
backgroundColor: other.backgroundColor,
);
}@override
bool operator ==(Object other) {
if (identical(this, other)) return true;return other is MyWidgetThemeData &&
other.headerColor == headerColor &&
other.headerTextStyle == headerTextStyle &&
other.backgroundColor == backgroundColor;
}@override
int get hashCode {
return Object.hashAll([
headerColor,
headerTextStyle,
backgroundColor,
]);
}
}class MyWidgetTheme extends InheritedWidget {
const MyWidgetTheme({
super.key,
required this.theme,
required super.child,
});final MyWidgetThemeData theme;
@override
bool updateShouldNotify(MyWidgetTheme oldWidget) {
return oldWidget.theme != theme;
}static MyWidgetThemeData of(BuildContext context) {
final widget = context.dependOnInheritedWidgetOfExactType();
final localTheme = widget?.theme;final theme = Theme.of(context);
final rootTheme = theme.extensions[MyWidgetThemeData] as MyWidgetThemeData?;final MyWidgetThemeData defaultTheme = _createDefault(theme);
final result = defaultTheme.merge(rootTheme?.merge(localTheme));return result;
}
}
```