This change makes the detection of `@Preview(...)` annotations more
robust by converting the analyzer AST directly to generated code rather
than simply copying and pasting the original source. This allows for
proper handling of the following cases WRT import prefixing:
- The use of `const`
- String interpolation
- Other uses of constant variables in non-trivial contexts
- Etc.
<!-- start_original_pr_link -->
Reverts: flutter/flutter#166698
<!-- end_original_pr_link -->
<!-- start_initiating_author -->
Initiated by: jonahwilliams
<!-- end_initiating_author -->
<!-- start_revert_reason -->
Reason for reverting: tests are timing out in presubmit
<!-- end_revert_reason -->
<!-- start_original_pr_author -->
Original PR Author: bkonyi
<!-- end_original_pr_author -->
<!-- start_reviewers -->
Reviewed By: {jyameo}
<!-- end_reviewers -->
<!-- start_revert_body -->
This change reverts the following previous change:
This will eventually be used as the main communication channel between
the widget preview scaffold, the Flutter tool, and other developer
tooling (e.g., IDEs).
Fixes#166417
<!-- end_revert_body -->
Co-authored-by: auto-submit[bot] <flutter-engprod-team@google.com>
This will eventually be used as the main communication channel between
the widget preview scaffold, the Flutter tool, and other developer
tooling (e.g., IDEs).
Fixes#166417
This change reworks how users define previews in their code, expands the
number of valid 'functions' that can be used to create previews, and
allows for specifying a 'wrapper' function to wrap the previewed widget
with
The `WidgetPreview` class has been removed from the framework, with its
properties being added to the `Preview` annotation class instead to
remove some boilerplate from the preview declaration workflow.
Before:
```dart
@Preview()
List<WidgetPreview> previews() => <WidgetPreview>[
WidgetPreview(
name: 'Top-level preview',
child: Text('Foo'),
),
];
```
After:
```dart
@Preview(name: 'Top-level preview')
Widget previews() => Text('Foo');
```
Previews can now be defined using top-level functions, constructors and
factories which take no arguments, and static methods within classes:
Examples:
```dart
@Preview(name: 'Top-level preview')
Widget previews() => Text('Foo');
class MyWidget extends StatelessWidget {
@Preview(name: 'Constructor preview')
MyWidget.preview();
@Preview(name: 'Factory preview')
factory MyWidget.factoryPreview() => MyWidget.preview();
@Preview(name: 'Static preview')
static Widget previewStatic() => Text('Static');
@override
Widget build(BuildContext context) {
return Text('MyWidget');
}
}
```
Users can also provide a `wrapper` function with the signature `Widget
Function(Widget)` to easily wrap previewed widget with shared
bootstrapping logic.
Example:
```dart
Widget testWrapper(Widget child) {
return Provider<int>.value(
value: 42,
child: child,
);
}
@Preview(name: 'Preview with wrapper', wrapper: testWrapper)
Widget preview() {
return Text('Attributes');
}
```
Which is effectively the same as:
```dart
@Preview(name: 'Preview with wrapper')
Widget preview() {
return Provider<int>.value(
value: 42,
child: Text('Attributes'),
);
}
```
Finally, for situations where a `BuildContext` is needed, users can
return a `WidgetBuilder` from their preview function:
```dart
@Preview('Builder preview')
WidgetBuilder builderPreview() {
return (BuildContext context) {
// TODO: retrieve state from context.
return Text('Foo');
};
}
```
Tests under the `general.shard` have a 2000ms timeout and these tests
write to the real file system and watch directories for changes. This
can be slow on heavily loaded machines and cause flaky failures.