docs(widget-previewer): update examples to match PreviewThemeData API - #13861
docs(widget-previewer): update examples to match PreviewThemeData API#13861maranix wants to merge 4 commits into
Conversation
- Align Custom and Transformative Preview examples with recent PreviewThemeData API changes in docs. - Added appropriate Screenshot and GIF for visual reference
There was a problem hiding this comment.
Code Review
This pull request updates the Dart code examples in the widget-previewer.md documentation to demonstrate custom theming and runtime transformations using the Preview annotation class, and adds preview images. However, both updated code snippets contain multiple Dart syntax errors, such as invalid class and constructor declarations, and missing target class names for static methods. The review feedback provides corrected code blocks to resolve these syntax issues.
| 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, | ||
| ); | ||
| } |
There was a problem hiding this comment.
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,
);
}| 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, | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
The Dart code example contains several syntax errors:
final class const TransformativePreviewis invalid. In Dart,constis a constructor modifier, not a class modifier.final class const PreviewMaterialThemeDatais invalid..fromand.fromSeedare missing their target class names (ThemeDataandColorScheme).
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,
);
}
}|
Is Official Dart repo does provide |
|
Yes, I am familiar with those features. Using Primary Constructors and dot-shorthand is an excellent way to keep the documentation examples modern and concise, aligning with current Dart best practices. They are indeed the correct, idiomatic ways to represent these definitions in modern Dart. |
|
Staged preview of the updated docs.flutter.dev site (updated for commit 2adb33b): https://flutter-docs-prod--docs-pr13861-fix-13799-0tqjc88x.web.app |
|
Staged preview of the updated flutter.dev site (updated for commit 2adb33b): https://flutter-dev-230821--www-pr13861-fix-13799-de3rfxci.web.app |
|
Hi @maranix, thanks for sending this in to address #13799! A few quick initial notes from triage: Please review and check off the items in the Presubmit checklist in the PR description. |
Re-ran tests and built the docs website along with Thanks and let me know if there is anything else is required. |
Fixes #13799
Align Custom and Transformative Preview examples with recent PreviewThemeData API changes in docs.
Added appropriate Screenshot and GIF for visual reference
Presubmit checklist
of 80 characters or fewer.