-
Notifications
You must be signed in to change notification settings - Fork 3.5k
docs(widget-previewer): update examples to match PreviewThemeData API #13861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maranix
wants to merge
4
commits into
flutter:main
Choose a base branch
from
maranix:fix-13799
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−39
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2adb33b
fix(widget-preview): update examples to match PreviewThemeData API
maranix bd5f435
Merge branch 'main' into fix-13799
maranix 1686cb3
fix(widget-preview): transformative-preview.gif -> webp
maranix ec9b098
fix(widget-previewer): update custom-preview.png to scale instead of …
maranix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -182,71 +182,90 @@ a common set of properties, the [`Preview`][] annotation class can be | |
| extended to create custom preview annotations tailored for your project. | ||
|
|
||
| Here's an example of a custom preview annotation that provides | ||
| theming data: | ||
| custom theming data to material widgets: | ||
|
|
||
| ```dart | ||
| final class MyCustomPreview extends Preview { | ||
| const MyCustomPreview({ | ||
| super.name, | ||
| super.group, | ||
| super.size, | ||
| super.textScaleFactor, | ||
| super.wrapper, | ||
| super.brightness, | ||
| super.localizations, | ||
| }) : super(theme: MyCustomPreview.themeBuilder); | ||
|
|
||
| static PreviewThemeData themeBuilder() { | ||
| return PreviewThemeData( | ||
| materialLight: ThemeData.light(), | ||
| materialDark: ThemeData.dark(), | ||
| ); | ||
| } | ||
| final class const MyCustomPreview({ | ||
| super.name, | ||
| super.group, | ||
| super.size, | ||
| super.textScaleFactor, | ||
| super.wrapper, | ||
| super.brightness, | ||
| super.localizations, | ||
| }) extends Preview { | ||
| this : super(theme: PreviewMaterialThemeData.new); | ||
| } | ||
|
|
||
| final class const PreviewMaterialThemeData() extends PreviewThemeData { | ||
| @override | ||
| Widget apply(BuildContext context, Widget child) => Theme( | ||
| data: .from(colorScheme: .fromSeed(seedColor: Colors.lime)), | ||
| child: child, | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
|  | ||
|
|
||
| Extending the [`Preview`][] annotation class also allows for overriding | ||
| the [`Preview.transform()`][] method. This method is invoked by the widget previewer | ||
| and can be used to modify the preview at runtime, allowing for preview | ||
| configurations that would not otherwise be possible in a `const` context: | ||
|
|
||
| ```dart | ||
| final class TransformativePreview extends Preview { | ||
| const TransformativePreview({ | ||
| super.name, | ||
| super.group, | ||
| super.size, | ||
| super.textScaleFactor, | ||
| super.wrapper, | ||
| super.brightness, | ||
| super.localizations, | ||
| }); | ||
|
|
||
| // Note: this is no longer public or static as it's injected | ||
| // at runtime when transform() is invoked. | ||
| PreviewThemeData _themeBuilder() { | ||
| return PreviewThemeData( | ||
| materialLight: ThemeData.light(), | ||
| materialDark: ThemeData.dark(), | ||
| ); | ||
| } | ||
| final class const TransformativePreview({ | ||
| super.name, | ||
| super.group, | ||
| super.size, | ||
| super.textScaleFactor, | ||
| super.wrapper, | ||
| super.brightness, | ||
| super.localizations, | ||
| }) extends Preview { | ||
| PreviewThemeData _themeBuilder() => | ||
| PreviewMaterialThemeData(brightness: brightness); | ||
|
|
||
| @override | ||
| Preview transform() { | ||
| final originalPreview = super.transform(); | ||
|
|
||
| final themeVariant = switch (brightness) { | ||
| null => 'Responsive', | ||
| final b => b.name, | ||
| }; | ||
|
|
||
| // Creates a PreviewBuilder that can be used to modify | ||
| // the preview contents. | ||
| final builder = originalPreview.toBuilder(); | ||
| builder | ||
| ..name = 'Transformed - ${originalPreview.name}' | ||
| ..name = 'Transformed - ${originalPreview.name}[$themeVariant]' | ||
| ..theme = _themeBuilder; | ||
|
|
||
| // Returns the updated Preview instance. | ||
| return builder.toPreview(); | ||
| return builder.build(); | ||
| } | ||
| } | ||
|
|
||
| final class const PreviewMaterialThemeData({final Brightness? _brightness}) | ||
| extends PreviewThemeData { | ||
| @override | ||
| Widget apply(BuildContext context, Widget child) { | ||
| return Theme( | ||
| data: .from( | ||
| colorScheme: .fromSeed( | ||
| seedColor: Colors.lime, | ||
| brightness: _brightness ?? MediaQuery.platformBrightnessOf(context), | ||
| ), | ||
| ), | ||
| child: child, | ||
| ); | ||
| } | ||
| } | ||
|
Comment on lines
+217
to
264
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Dart code example contains several syntax errors:
final class TransformativePreview extends Preview {
const TransformativePreview({
super.name,
super.group,
super.size,
super.textScaleFactor,
super.wrapper,
super.brightness,
super.localizations,
});
PreviewThemeData _themeBuilder() =>
PreviewMaterialThemeData(brightness: brightness);
@override
Preview transform() {
final originalPreview = super.transform();
final themeVariant = switch (brightness) {
null => 'Responsive',
final b => b.name,
};
// Creates a PreviewBuilder that can be used to modify
// the preview contents.
final builder = originalPreview.toBuilder();
builder
..name = 'Transformed - ${originalPreview.name}[$themeVariant]'
..theme = _themeBuilder;
// Returns the updated Preview instance.
return builder.build();
}
}
final class PreviewMaterialThemeData extends PreviewThemeData {
const PreviewMaterialThemeData({Brightness? brightness}) : _brightness = brightness;
final Brightness? _brightness;
@override
Widget apply(BuildContext context, Widget child) {
return Theme(
data: ThemeData.from(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.lime,
brightness: _brightness ?? MediaQuery.platformBrightnessOf(context),
),
),
child: child,
);
}
} |
||
| ``` | ||
|
|
||
|  | ||
|
|
||
| ## Creating multiple preview configurations | ||
|
|
||
| Creating multiple previews with different configurations can be as | ||
|
|
||
Binary file added
BIN
+155 KB
sites/docs/web/assets/images/docs/tools/widget-previewer/custom-preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.89 MB
.../docs/web/assets/images/docs/tools/widget-previewer/transformative-preview.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Dart code example contains several syntax errors:
final class const MyCustomPreviewis invalid. In Dart,constis a constructor modifier, not a class modifier.this : super(...)is invalid syntax for a constructor initializer list.final class const PreviewMaterialThemeData()is invalid..fromand.fromSeedare missing their target class names (ThemeDataandColorScheme).final class MyCustomPreview extends Preview { const MyCustomPreview({ super.name, super.group, super.size, super.textScaleFactor, super.wrapper, super.brightness, super.localizations, }) : super(theme: PreviewMaterialThemeData.new); } final class PreviewMaterialThemeData extends PreviewThemeData { const PreviewMaterialThemeData(); @override Widget apply(BuildContext context, Widget child) => Theme( data: ThemeData.from( colorScheme: ColorScheme.fromSeed(seedColor: Colors.lime), ), child: child, ); }