Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 58 additions & 39 deletions sites/docs/src/content/tools/widget-previewer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Comment on lines +188 to 206

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Dart code example contains several syntax errors:

  1. final class const MyCustomPreview is invalid. In Dart, const is a constructor modifier, not a class modifier.
  2. this : super(...) is invalid syntax for a constructor initializer list.
  3. final class const PreviewMaterialThemeData() is invalid.
  4. .from and .fromSeed are missing their target class names (ThemeData and ColorScheme).
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,
      );
}

```

![Providing ThemeData using custom Preview annotation in Flutter Widget Previewer](/assets/images/docs/tools/widget-previewer/custom-preview.png "Injecting ThemeData in Preview annotation")

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Dart code example contains several syntax errors:

  1. final class const TransformativePreview is invalid. In Dart, const is a constructor modifier, not a class modifier.
  2. final class const PreviewMaterialThemeData is invalid.
  3. .from and .fromSeed are missing their target class names (ThemeData and ColorScheme).
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,
    );
  }
}

```

![Runtime transformations to Preview annotation in Flutter Widget Previewer](/assets/images/docs/tools/widget-previewer/transformative-preview.webp "Runtime transformations in Preview annotation")

## Creating multiple preview configurations

Creating multiple previews with different configurations can be as
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.