Skip to content

docs(widget-previewer): update examples to match PreviewThemeData API - #13861

Open
maranix wants to merge 4 commits into
flutter:mainfrom
maranix:fix-13799
Open

docs(widget-previewer): update examples to match PreviewThemeData API#13861
maranix wants to merge 4 commits into
flutter:mainfrom
maranix:fix-13799

Conversation

@maranix

@maranix maranix commented Sep 8, 2026

Copy link
Copy Markdown

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

  • If you are unwilling, or unable, to sign the CLA, even for a tiny, one-word PR, please file an issue instead of a PR.
  • If this PR is not meant to land until a future stable release, mark it as draft with an explanation.
  • This PR follows the Google Developer Documentation Style Guidelines—for example, it doesn't use i.e. or e.g., and it avoids I and we (first-person pronouns).
  • This PR uses semantic line breaks
    of 80 characters or fewer.

- Align Custom and Transformative Preview  examples with recent PreviewThemeData API changes in docs.

- Added appropriate Screenshot and GIF for visual reference
@maranix
maranix requested review from a team and sfshaza2 as code owners September 8, 2026 06:55

@gemini-code-assist gemini-code-assist Bot left a comment

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.

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.

Comment on lines +188 to 206
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,
);
}

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,
      );
}

Comment on lines +217 to 264
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,
);
}
}

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,
    );
  }
}

@maranix

maranix commented Sep 8, 2026

Copy link
Copy Markdown
Author

Is gemini-code-assist not familiar with Primary Constructors and dot-shorthand?

Official Dart repo does provide skills for that so it should be, unless it is not using them. Anyways if there is a need for the examples to be in standard "old" dart then let me know and i will update them.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

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.

@flutter-website-bot

Copy link
Copy Markdown
Collaborator

Staged preview of the updated docs.flutter.dev site (updated for commit 2adb33b):

https://flutter-docs-prod--docs-pr13861-fix-13799-0tqjc88x.web.app

@flutter-website-bot

Copy link
Copy Markdown
Collaborator

Staged preview of the updated flutter.dev site (updated for commit 2adb33b):

https://flutter-dev-230821--www-pr13861-fix-13799-de3rfxci.web.app

@jesskuras

Copy link
Copy Markdown
Contributor

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.
For the animation asset, we are currently migrating all GIFs across the site to WebP (see #13860)—could you provide this as a WebP instead of a GIF?

@maranix

maranix commented Sep 9, 2026

Copy link
Copy Markdown
Author

@jesskuras

  • Updated Presubmit checklist
  • Replaced transformative-preview.gif with transformative-preview.webp

Re-ran tests and built the docs website along with check-links and check-link-references.

Thanks and let me know if there is anything else is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Widget Preview Documentation and PreviewThemeData API are out-of-sync

3 participants