From e24013a922e45d636e312ee5eccd54f9301f1174 Mon Sep 17 00:00:00 2001 From: Ben Konyi Date: Wed, 29 Jan 2025 16:37:07 -0500 Subject: [PATCH] Add WidgetPreview and @Preview() annotation (#159219) This is the initial work for the upcoming Widget Preview functionality. The `WidgetPreview` class allows for users to specify overrides and details to apply to its child `Widget`. Initially, this includes: - Setting arbitrary width and height constraints to mimic changes in device resolution - Custom text scaling factors The `Preview` class is an annotation used to identify top-level, public functions as widget previews --- .../lib/src/widgets/widget_preview.dart | 79 +++++++++++++++++++ packages/flutter/lib/widgets.dart | 1 + 2 files changed, 80 insertions(+) create mode 100644 packages/flutter/lib/src/widgets/widget_preview.dart diff --git a/packages/flutter/lib/src/widgets/widget_preview.dart b/packages/flutter/lib/src/widgets/widget_preview.dart new file mode 100644 index 0000000000..be2dd5544f --- /dev/null +++ b/packages/flutter/lib/src/widgets/widget_preview.dart @@ -0,0 +1,79 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'framework.dart'; + +/// Annotation used to mark functions that return a widget preview. +/// +/// NOTE: this interface is not stable and **will change**. +/// +/// {@tool snippet} +/// +/// Functions annotated with `@Preview()` must return a `WidgetPreview` +/// and be public, top-level functions. +/// +/// ```dart +/// @Preview() +/// WidgetPreview widgetPreview() { +/// return const WidgetPreview(name: 'Preview 1', child: Text('Foo')); +/// } +/// ``` +/// {@end-tool} +/// +/// See also: +/// +/// * [WidgetPreview], a data class used to specify widget previews. +// TODO(bkonyi): link to actual documentation when available. +class Preview { + /// Annotation used to mark functions that return widget previews. + const Preview(); +} + +/// Wraps a [Widget], initializing various state and properties to allow for +/// previewing of the [Widget] in the widget previewer. +/// +/// WARNING: This interface is not stable and **will change**. +/// +/// See also: +/// +/// * [Preview], an annotation class used to mark functions returning widget +/// previews. +// TODO(bkonyi): link to actual documentation when available. +class WidgetPreview { + /// Wraps [child] in a [WidgetPreview] instance that applies some set of + /// properties. + const WidgetPreview({ + required this.child, + this.name, + this.width, + this.height, + this.textScaleFactor, + }); + + /// A description to be displayed alongside the preview. + /// + /// If not provided, no name will be associated with the preview. + final String? name; + + /// The [Widget] to be rendered in the preview. + final Widget child; + + /// Artificial width constraint to be applied to the [child]. + /// + /// If not provided, the previewed widget will attempt to set its own width + /// constraints and may result in an unbounded constraint error. + final double? width; + + /// Artificial height constraint to be applied to the [child]. + /// + /// If not provided, the previewed widget will attempt to set its own height + /// constraints and may result in an unbounded constraint error. + final double? height; + + /// Applies font scaling to text within the [child]. + /// + /// If not provided, the default text scaling factor provided by [MediaQuery] + /// will be used. + final double? textScaleFactor; +} diff --git a/packages/flutter/lib/widgets.dart b/packages/flutter/lib/widgets.dart index 7652640d9e..4458e361f5 100644 --- a/packages/flutter/lib/widgets.dart +++ b/packages/flutter/lib/widgets.dart @@ -172,6 +172,7 @@ export 'src/widgets/view.dart'; export 'src/widgets/viewport.dart'; export 'src/widgets/visibility.dart'; export 'src/widgets/widget_inspector.dart'; +export 'src/widgets/widget_preview.dart'; export 'src/widgets/widget_span.dart'; export 'src/widgets/widget_state.dart'; export 'src/widgets/will_pop_scope.dart';