Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f54cd3e

Browse files
bkonyiash2moon
authored andcommitted
[ Widget Preview ] Add typedefs, replace height and width with size (flutter#168063)
Addresses some comments from [flutter.dev/go/widget-previews-architecture](flutter.dev/go/widget-previews-architecture).
1 parent 0e6c243 commit f54cd3e

File tree

9 files changed

+76
-86
lines changed

9 files changed

+76
-86
lines changed

packages/flutter/lib/src/widget_previews/widget_previews.dart

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import 'package:flutter/cupertino.dart' show CupertinoThemeData;
1010
import 'package:flutter/material.dart' show Brightness, ThemeData;
1111
import 'package:flutter/widgets.dart';
1212

13+
/// Signature for callbacks that build theming data used when creating a [Preview].
14+
typedef PreviewTheme = PreviewThemeData Function();
15+
16+
/// Signature for callbacks that wrap a [Widget] with another [Widget] when creating a [Preview].
17+
typedef WidgetWrapper = Widget Function(Widget);
18+
1319
/// Annotation used to mark functions that return a widget preview.
1420
///
1521
/// NOTE: this interface is not stable and **will change**.
@@ -57,8 +63,7 @@ base class Preview {
5763
/// Annotation used to mark functions that return widget previews.
5864
const Preview({
5965
this.name,
60-
this.width,
61-
this.height,
66+
this.size,
6267
this.textScaleFactor,
6368
this.wrapper,
6469
this.theme,
@@ -70,17 +75,17 @@ base class Preview {
7075
/// If not provided, no name will be associated with the preview.
7176
final String? name;
7277

73-
/// Artificial width constraint to be applied to the previewed widget.
78+
/// Artificial constraints to be applied to the previewed widget.
7479
///
75-
/// If not provided, the previewed widget will attempt to set its own width
76-
/// constraints and may result in an unbounded constraint error.
77-
final double? width;
78-
79-
/// Artificial height constraint to be applied to the previewed widget.
80+
/// If not provided, the previewed widget will attempt to set its own
81+
/// constraints.
82+
///
83+
/// If a dimension has a value of `double.infinity`, the previewed widget
84+
/// will attempt to set its own constraints in the relevant dimension.
8085
///
81-
/// If not provided, the previewed widget will attempt to set its own height
82-
/// constraints and may result in an unbounded constraint error.
83-
final double? height;
86+
/// To set a single dimension and allow the other to set its own constraints, use
87+
/// [Size.fromHeight] or [Size.fromWidth].
88+
final Size? size;
8489

8590
/// Applies font scaling to text within the previewed widget.
8691
///
@@ -96,14 +101,14 @@ base class Preview {
96101
/// Note: this must be a reference to a static, public function defined as
97102
/// either a top-level function or static member in a class.
98103
// TODO(bkonyi): provide an example.
99-
final Widget Function(Widget)? wrapper;
104+
final WidgetWrapper? wrapper;
100105

101106
/// A callback to return Material and Cupertino theming data to be applied
102107
/// to the previewed [Widget].
103108
///
104109
/// Note: this must be a reference to a static, public function defined as
105110
/// either a top-level function or static member in a class.
106-
final PreviewThemeData Function()? theme;
111+
final PreviewTheme? theme;
107112

108113
/// Sets the initial theme brightness.
109114
///

packages/flutter_tools/lib/src/widget_preview/preview_code_generator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ class PreviewCodeGenerator {
114114
'widget_preview.dart',
115115
).newInstance(<Expression>[], <String, Expression>{
116116
if (preview.name != null) PreviewDetails.kName: refer(preview.name!).expression,
117-
...?_buildDoubleParameters(key: PreviewDetails.kHeight, property: preview.height),
118-
...?_buildDoubleParameters(key: PreviewDetails.kWidth, property: preview.width),
117+
if (preview.size != null)
118+
PreviewDetails.kSize: refer(preview.size!, preview.sizeLibraryUri ?? 'dart:ui'),
119119
...?_buildDoubleParameters(
120120
key: PreviewDetails.kTextScaleFactor,
121121
property: preview.textScaleFactor,

packages/flutter_tools/lib/src/widget_preview/preview_detector.dart

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ final class PreviewDetails {
6565
required this.functionName,
6666
required this.isBuilder,
6767
String? name,
68-
String? width,
69-
String? height,
68+
String? size,
7069
String? textScaleFactor,
7170
String? wrapper,
7271
String? wrapperLibraryUri = '',
@@ -75,8 +74,7 @@ final class PreviewDetails {
7574
String? brightness,
7675
String? brightnessLibraryUri = '',
7776
}) : _name = name,
78-
_width = width,
79-
_height = height,
77+
_size = size,
8078
_textScaleFactor = textScaleFactor,
8179
_wrapper = wrapper,
8280
_wrapperLibraryUri = wrapperLibraryUri,
@@ -90,8 +88,7 @@ final class PreviewDetails {
9088
String? functionName,
9189
bool? isBuilder,
9290
String? name,
93-
String? width,
94-
String? height,
91+
String? size,
9592
String? textScaleFactor,
9693
String? wrapper,
9794
String? wrapperLibraryUri,
@@ -104,8 +101,7 @@ final class PreviewDetails {
104101
functionName: functionName ?? this.functionName,
105102
isBuilder: isBuilder ?? this.isBuilder,
106103
name: name ?? this.name,
107-
width: width ?? this.width,
108-
height: height ?? this.height,
104+
size: size ?? this.size,
109105
textScaleFactor: textScaleFactor ?? this.textScaleFactor,
110106
wrapper: wrapper ?? this.wrapper,
111107
wrapperLibraryUri: wrapperLibraryUri ?? this.wrapperLibraryUri,
@@ -117,8 +113,8 @@ final class PreviewDetails {
117113
}
118114

119115
static const String kName = 'name';
120-
static const String kWidth = 'width';
121-
static const String kHeight = 'height';
116+
static const String kSize = 'size';
117+
static const String kSizeLibraryUri = 'sizeLibraryUrl';
122118
static const String kTextScaleFactor = 'textScaleFactor';
123119
static const String kWrapper = 'wrapper';
124120
static const String kWrapperLibraryUri = 'wrapperLibraryUrl';
@@ -140,19 +136,15 @@ final class PreviewDetails {
140136
String? get name => _name;
141137
String? _name;
142138

143-
/// Artificial width constraint to be applied to the [child].
139+
/// Artificial constraints to be applied to the [child].
144140
///
145-
/// If not provided, the previewed widget will attempt to set its own width
141+
/// If not provided, the previewed widget will attempt to set its own
146142
/// constraints and may result in an unbounded constraint error.
147-
String? get width => _width;
148-
String? _width;
143+
String? get size => _size;
144+
String? _size;
149145

150-
/// Artificial height constraint to be applied to the [child].
151-
///
152-
/// If not provided, the previewed widget will attempt to set its own height
153-
/// constraints and may result in an unbounded constraint error.
154-
String? get height => _height;
155-
String? _height;
146+
String? get sizeLibraryUri => _sizeLibraryUri;
147+
String? _sizeLibraryUri;
156148

157149
/// Applies font scaling to text within the [child].
158150
///
@@ -196,10 +188,9 @@ final class PreviewDetails {
196188
switch (key) {
197189
case kName:
198190
_name = source;
199-
case kWidth:
200-
_width = source;
201-
case kHeight:
202-
_height = source;
191+
case kSize:
192+
_size = source;
193+
_sizeLibraryUri = libraryUri;
203194
case kTextScaleFactor:
204195
_textScaleFactor = source;
205196
case kWrapper:
@@ -226,8 +217,8 @@ final class PreviewDetails {
226217
other is PreviewDetails &&
227218
other.functionName == functionName &&
228219
other.isBuilder == isBuilder &&
229-
other.height == height &&
230-
other.width == width &&
220+
other.size == size &&
221+
other.sizeLibraryUri == sizeLibraryUri &&
231222
other.textScaleFactor == textScaleFactor &&
232223
other.wrapper == wrapper &&
233224
other.wrapperLibraryUri == wrapperLibraryUri &&
@@ -240,17 +231,18 @@ final class PreviewDetails {
240231
@override
241232
String toString() =>
242233
'PreviewDetails(function: $functionName isBuilder: $isBuilder $kName: $name '
243-
'$kWidth: $width $kHeight: $height $kTextScaleFactor: $textScaleFactor $kWrapper: $wrapper '
244-
'$kWrapperLibraryUri: $wrapperLibraryUri $kTheme: $theme $kThemeLibraryUri: $themeLibraryUri '
245-
'$kBrightness: $_brightness $kBrightnessLibraryUri: $_brightnessLibraryUri)';
234+
'$kSize: $size $kSizeLibraryUri: $sizeLibraryUri $kTextScaleFactor: $textScaleFactor '
235+
'$kWrapper: $wrapper $kWrapperLibraryUri: $wrapperLibraryUri $kTheme: $theme '
236+
'$kThemeLibraryUri: $themeLibraryUri $kBrightness: $_brightness '
237+
'$kBrightnessLibraryUri: $_brightnessLibraryUri)';
246238

247239
@override
248240
// ignore: avoid_equals_and_hash_code_on_mutable_classes
249241
int get hashCode => Object.hashAll(<Object?>[
250242
functionName,
251243
isBuilder,
252-
height,
253-
width,
244+
size,
245+
sizeLibraryUri,
254246
textScaleFactor,
255247
wrapper,
256248
wrapperLibraryUri,

packages/flutter_tools/templates/widget_preview_scaffold/lib/src/widget_preview.dart.tmpl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class WidgetPreview {
2323
const WidgetPreview({
2424
required this.builder,
2525
this.name,
26-
this.width,
27-
this.height,
26+
this.size,
2827
this.textScaleFactor,
2928
this.brightness,
3029
this.theme,
@@ -38,17 +37,14 @@ class WidgetPreview {
3837
/// A callback to build the [Widget] to be rendered in the preview.
3938
final Widget Function() builder;
4039

41-
/// Artificial width constraint to be applied to the [Widget] returned by [builder].
40+
/// Artificial constraints to be applied to the previewed widget.
4241
///
43-
/// If not provided, the previewed widget will attempt to set its own width
44-
/// constraints and may result in an unbounded constraint error.
45-
final double? width;
46-
47-
/// Artificial height constraint to be applied to the [Widget] returned by [builder].
42+
/// If not provided, the previewed widget will attempt to set its own
43+
/// constraints.
4844
///
49-
/// If not provided, the previewed widget will attempt to set its own height
50-
/// constraints and may result in an unbounded constraint error.
51-
final double? height;
45+
/// If a dimension has a value of `double.infinity`, the previewed widget
46+
/// will attempt to set its own constraints in the relevant dimension.
47+
final Size? size;
5248

5349
/// Applies font scaling to text within the [Widget] returned by [builder].
5450
///

packages/flutter_tools/templates/widget_preview_scaffold/lib/src/widget_preview_rendering.dart.tmpl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ class WidgetPreviewWidgetState extends State<WidgetPreviewWidget> {
250250
},
251251
);
252252

253+
final Size? size = widget.preview.size;
254+
253255
// Add support for selecting only previewed widgets via the widget
254256
// inspector.
255257
preview = ValueListenableBuilder(
@@ -274,8 +276,8 @@ class WidgetPreviewWidgetState extends State<WidgetPreviewWidget> {
274276
child: _WidgetPreviewWrapper(
275277
previewerConstraints: maxSizeConstraints,
276278
child: SizedBox(
277-
width: widget.preview.width,
278-
height: widget.preview.height,
279+
width: size?.width == double.infinity ? null : size?.width,
280+
height: size?.height == double.infinity ? null : size?.height,
279281
child: preview,
280282
),
281283
),
@@ -425,11 +427,11 @@ class WidgetPreviewMediaQueryOverride extends StatelessWidget {
425427
}
426428

427429
var size = Size(
428-
preview.width ?? mediaQueryData.size.width,
429-
preview.height ?? mediaQueryData.size.height,
430+
preview.size?.width ?? mediaQueryData.size.width,
431+
preview.size?.height ?? mediaQueryData.size.height,
430432
);
431433

432-
if (preview.width != null || preview.height != null) {
434+
if (preview.size != null) {
433435
mediaQueryData = mediaQueryData.copyWith(size: size);
434436
}
435437

packages/flutter_tools/test/commands.shard/hermetic/widget_preview/preview_detector_test.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ void main() {
106106
functionName: 'attributesPreview',
107107
isBuilder: false,
108108
name: 'Attributes preview',
109-
width: '100.0',
110-
height: '100',
109+
size: 'Size(width: 100.0, height: 100)',
111110
textScaleFactor: '2',
112111
wrapper: 'testWrapper',
113112
theme: 'theming',
@@ -183,8 +182,7 @@ void main() {
183182
functionName: 'attributesPreview',
184183
isBuilder: false,
185184
name: 'Attributes preview',
186-
width: '100.0',
187-
height: '100',
185+
size: 'Size(width: 100.0, height: 100)',
188186
textScaleFactor: '2',
189187
wrapper: 'testWrapper',
190188
theme: 'theming',
@@ -295,7 +293,7 @@ PreviewThemeData theming() => PreviewThemeData(
295293
cupertinoDark: CupertinoThemeData(primaryColor: Colors.purple),
296294
);
297295
298-
@Preview(name: 'Attributes preview', height: 100, width: 100.0, textScaleFactor: 2, wrapper: testWrapper, theme: theming, brightness: Brightness.dark)
296+
@Preview(name: 'Attributes preview', size: Size(width: 100.0, height: 100), textScaleFactor: 2, wrapper: testWrapper, theme: theming, brightness: Brightness.dark)
299297
Widget attributesPreview() {
300298
return Text('Attributes');
301299
}

packages/flutter_tools/test/general.shard/widget_preview/preview_code_generator_test.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ void main() {
5757
functionName: 'barPreview3',
5858
isBuilder: true,
5959
name: 'Foo',
60-
width: '123',
61-
height: '456',
60+
size: 'Size(height: 123, width: 456)',
6261
textScaleFactor: '50',
6362
wrapper: 'wrapper',
6463
wrapperLibraryUri: 'wrapper.dart',
@@ -82,7 +81,7 @@ void main() {
8281
// The generated file is unfortunately unformatted.
8382
const String expectedGeneratedPreviewFileContents = '''
8483
// ignore_for_file: no_leading_underscores_for_library_prefixes
85-
import 'widget_preview.dart' as _i1;import 'foo.dart' as _i2;import 'src/bar.dart' as _i3;import 'brightness.dart' as _i4;import 'theme.dart' as _i5;import '' as _i6;import 'wrapper.dart' as _i7;import 'package:flutter/widgets.dart' as _i8;List<_i1.WidgetPreview> previews() => [_i1.WidgetPreview(builder: () => _i2.preview()), _i1.WidgetPreview(builder: () => _i3.barPreview1()), _i1.WidgetPreview(brightness: _i4.brightnessConstant, builder: () => _i3.barPreview2(), ), _i1.WidgetPreview(name: Foo, height: 456.0, width: 123.0, textScaleFactor: 50.0, theme: _i5.myThemeCallback(), brightness: _i6.Brightness.dark, builder: () => _i7.wrapper(_i8.Builder(builder: _i3.barPreview3())), ), ];''';
84+
import 'widget_preview.dart' as _i1;import 'foo.dart' as _i2;import 'src/bar.dart' as _i3;import 'brightness.dart' as _i4;import 'dart:ui' as _i5;import 'theme.dart' as _i6;import '' as _i7;import 'wrapper.dart' as _i8;import 'package:flutter/widgets.dart' as _i9;List<_i1.WidgetPreview> previews() => [_i1.WidgetPreview(builder: () => _i2.preview()), _i1.WidgetPreview(builder: () => _i3.barPreview1()), _i1.WidgetPreview(brightness: _i4.brightnessConstant, builder: () => _i3.barPreview2(), ), _i1.WidgetPreview(name: Foo, size: _i5.Size(height: 123, width: 456), textScaleFactor: 50.0, theme: _i6.myThemeCallback(), brightness: _i7.Brightness.dark, builder: () => _i8.wrapper(_i9.Builder(builder: _i3.barPreview3())), ), ];''';
8685
expect(generatedPreviewFile.readAsStringSync(), expectedGeneratedPreviewFileContents);
8786

8887
// Regenerate the generated file with no previews.

packages/flutter_tools/test/widget_preview_scaffold.shard/widget_preview_scaffold/lib/src/widget_preview.dart

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class WidgetPreview {
2323
const WidgetPreview({
2424
required this.builder,
2525
this.name,
26-
this.width,
27-
this.height,
26+
this.size,
2827
this.textScaleFactor,
2928
this.brightness,
3029
this.theme,
@@ -38,17 +37,14 @@ class WidgetPreview {
3837
/// A callback to build the [Widget] to be rendered in the preview.
3938
final Widget Function() builder;
4039

41-
/// Artificial width constraint to be applied to the [Widget] returned by [builder].
40+
/// Artificial constraints to be applied to the previewed widget.
4241
///
43-
/// If not provided, the previewed widget will attempt to set its own width
44-
/// constraints and may result in an unbounded constraint error.
45-
final double? width;
46-
47-
/// Artificial height constraint to be applied to the [Widget] returned by [builder].
42+
/// If not provided, the previewed widget will attempt to set its own
43+
/// constraints.
4844
///
49-
/// If not provided, the previewed widget will attempt to set its own height
50-
/// constraints and may result in an unbounded constraint error.
51-
final double? height;
45+
/// If a dimension has a value of `double.infinity`, the previewed widget
46+
/// will attempt to set its own constraints in the relevant dimension.
47+
final Size? size;
5248

5349
/// Applies font scaling to text within the [Widget] returned by [builder].
5450
///

packages/flutter_tools/test/widget_preview_scaffold.shard/widget_preview_scaffold/lib/src/widget_preview_rendering.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ class WidgetPreviewWidgetState extends State<WidgetPreviewWidget> {
250250
},
251251
);
252252

253+
final Size? size = widget.preview.size;
254+
253255
// Add support for selecting only previewed widgets via the widget
254256
// inspector.
255257
preview = ValueListenableBuilder(
@@ -274,8 +276,8 @@ class WidgetPreviewWidgetState extends State<WidgetPreviewWidget> {
274276
child: _WidgetPreviewWrapper(
275277
previewerConstraints: maxSizeConstraints,
276278
child: SizedBox(
277-
width: widget.preview.width,
278-
height: widget.preview.height,
279+
width: size?.width == double.infinity ? null : size?.width,
280+
height: size?.height == double.infinity ? null : size?.height,
279281
child: preview,
280282
),
281283
),
@@ -425,11 +427,11 @@ class WidgetPreviewMediaQueryOverride extends StatelessWidget {
425427
}
426428

427429
var size = Size(
428-
preview.width ?? mediaQueryData.size.width,
429-
preview.height ?? mediaQueryData.size.height,
430+
preview.size?.width ?? mediaQueryData.size.width,
431+
preview.size?.height ?? mediaQueryData.size.height,
430432
);
431433

432-
if (preview.width != null || preview.height != null) {
434+
if (preview.size != null) {
433435
mediaQueryData = mediaQueryData.copyWith(size: size);
434436
}
435437

0 commit comments

Comments
 (0)