MultiPreview class abstract base

The base class used to define a custom 'multi-preview' annotation.

Marking functions that return a widget preview with an instance of MultiPreview is the equivalent of applying each Preview instance in the previews field to the function.

This sample shows two ways to define multiple previews for a single preview function.

The first approach uses a MultiPreview implementation that creates previews using light and dark mode themes.

The second approach uses multiple Preview annotations to achieve the same result.

link
final class BrightnessPreview extends MultiPreview {
  const BrightnessPreview();

  @override
  // ignore: avoid_field_initializers_in_const_classes
  final List<Preview> previews = const <Preview>[
    Preview(name: 'Light', brightness: Brightness.light),
    Preview(name: 'Dark', brightness: Brightness.dark),
  ];
}

// Using a multi-preview to create 'Light' and 'Dark' previews.
@BrightnessPreview()
WidgetBuilder brightnessPreview() {
  return (BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return Text('Brightness: ${theme.brightness}');
  };
}

// Using multiple Preview annotations to create 'Light' and 'Dark' previews.
@Preview(name: 'Light', brightness: Brightness.light)
@Preview(name: 'Dark', brightness: Brightness.dark)
WidgetBuilder brightnessPreviewManual() {
  return (BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return Text('Brightness: ${theme.brightness}');
  };
}

Important Note: all values provided to MultiPreview derived annotations must be constant, and callback parameters must also be static and non-private. These restrictions do not apply when creating instances of MultiPreview at runtime, including within Preview.transform().

See also:

Constructors

MultiPreview()
Creates a MultiPreview annotation instance.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
previews List<Preview>
The set of Previews to be created for the annotated function.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
transform() List<Preview>
Applies a transformation to each Preview in previews.

Operators

operator ==(Object other) bool
The equality operator.
inherited