Commit Graph

15 Commits

Author SHA1 Message Date
Ben Konyi
4baf4b4d1c
[ Widget Preview ] Refactor @Preview() detection and code generation (#168307)
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.
2025-05-16 19:59:25 +00:00
Ben Konyi
ac6ab205b5
Reland "[ Widget Preview ] Add initial support for communications over the Dart Tooling Daemon (DTD) (#166698)" (#166877)
This reverts commit 059326d49d and updates
the `widget-preview` command to always spawn the previewer using a local
canvaskit binary.

Fixes https://github.com/flutter/flutter/issues/166865
2025-04-11 14:55:29 +00:00
auto-submit[bot]
059326d49d
Reverts "[ Widget Preview ] Add initial support for communications over the Dart Tooling Daemon (DTD) (#166698)" (#166866)
<!-- 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>
2025-04-09 18:11:18 +00:00
Ben Konyi
30e53b0d9c
[ Widget Preview ] Add initial support for communications over the Dart Tooling Daemon (DTD) (#166698)
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
2025-04-08 18:10:50 +00:00
Ben Konyi
3efb8cc359
[ Widget Preview ] Display an error widget when an exception is thrown while defining the widget tree (#166005)
Example exception thrown trying to invoke `Directory.current` in a
widget constructor:


![image](https://github.com/user-attachments/assets/f9038fa0-9e6e-4037-bc33-a304861c6a22)
2025-03-27 18:47:51 +00:00
Ben Konyi
41c427c6de
[ Widget Preview ] Remove WidgetPreview in favor of using annotation properties (#165500)
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');
  };
}
```
2025-03-22 01:35:36 +00:00
Ben Konyi
e7e5480a57
[ Widget Preview ] Update generated scaffold project to include early preview rendering (#162847)
With this change, `flutter widget-preview start` will launch a working
widget preview environment that can render previews from a target
project.

Also fixes an issue where `--offline` wasn't being respected by some pub
operations.
2025-02-11 16:41:28 +00:00
Ben Konyi
0889cca518
[ Widget Preview ] Add support for building and launching the widget preview scaffold (#162326)
This also wires up the preview detector to trigger hot reloads when new
previews are detected or previews are removed.

Note: while this change results in lib/generated_preview.dart being
generated and updated, it's not currently referenced by lib/main.dart
and the preview environment will render a black screen.

---------

Co-authored-by: Andrew Kolos <andrewrkolos@gmail.com>
2025-02-05 20:44:56 +00:00
Ben Konyi
de872dd864
[ Tool ] Remove use of globals from widget-preview commands (#162522) 2025-01-31 19:17:45 +00:00
Ben Konyi
2e4b40bdfa
Reland "[ Widget Previews ] Add support for detecting previews and generating code (#161911)"" (#162337)
Reverts flutter/flutter#162327
2025-01-29 02:05:53 +00:00
auto-submit[bot]
1807704009
Reverts "[ Widget Previews ] Add support for detecting previews and generating code (#161911)" (#162327)
<!-- start_original_pr_link -->
Reverts: flutter/flutter#161911
<!-- end_original_pr_link -->
<!-- start_initiating_author -->
Initiated by: matanlurey
<!-- end_initiating_author -->
<!-- start_revert_reason -->
Reason for reverting: Mid-air collision with another Flutter tool
update.
<!-- end_revert_reason -->
<!-- start_original_pr_author -->
Original PR Author: bkonyi
<!-- end_original_pr_author -->

<!-- start_reviewers -->
Reviewed By: {andrewkolos}
<!-- end_reviewers -->

<!-- start_revert_body -->
This change reverts the following previous change:
`flutter widget-preview start` will now look for functions annotated
with `@Preview()` within the developer's project. These functions will
be used to generate
`.dart_tool/widget_preview_scaffold/lib/generated_preview.dart`, which
inserts the returned value from each preview function into a
`List<WidgetPreview>` returned from a `previews()` method that is
invoked by the widget preview scaffold root.

**Example generated_preview.dart:**

```dart
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:foo/foo.dart' as _i1;
import 'package:foo/src/bar.dart' as _i2;
import 'package:widget_preview/widget_preview.dart';

List<WidgetPreview> previews() => [_i1.preview(), _i2.barPreview1(), _i2.barPreview2()];
```
<!-- end_revert_body -->

Co-authored-by: auto-submit[bot] <flutter-engprod-team@google.com>
2025-01-28 19:14:26 +00:00
Ben Konyi
f25b4b77ff
[ Widget Previews ] Add support for detecting previews and generating code (#161911)
`flutter widget-preview start` will now look for functions annotated
with `@Preview()` within the developer's project. These functions will
be used to generate
`.dart_tool/widget_preview_scaffold/lib/generated_preview.dart`, which
inserts the returned value from each preview function into a
`List<WidgetPreview>` returned from a `previews()` method that is
invoked by the widget preview scaffold root.

**Example generated_preview.dart:**

```dart
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:foo/foo.dart' as _i1;
import 'package:foo/src/bar.dart' as _i2;
import 'package:widget_preview/widget_preview.dart';

List<WidgetPreview> previews() => [_i1.preview(), _i2.barPreview1(), _i2.barPreview2()];
```
2025-01-28 17:59:57 +00:00
Andrew Kolos
a9c50335c7
remove dependency on Usage from Pub class (#162279)
Towards https://github.com/flutter/flutter/issues/150575

Removes more usage of `Usage`. This PR is scoped to removing all
references to it in `pub`-related code.

<details>

<summary> Pre-launch checklist </summary> 


- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

</details>


If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-01-28 17:32:27 +00:00
Michael Goderbauer
5491c8c146
Auto-format Framework (#160545)
This auto-formats all *.dart files in the repository outside of the
`engine` subdirectory and enforces that these files stay formatted with
a presubmit check.

**Reviewers:** Please carefully review all the commits except for the
one titled "formatted". The "formatted" commit was auto-generated by
running `dev/tools/format.sh -a -f`. The other commits were hand-crafted
to prepare the repo for the formatting change. I recommend reviewing the
commits one-by-one via the "Commits" tab and avoiding Github's "Files
changed" tab as it will likely slow down your browser because of the
size of this PR.

---------

Co-authored-by: Kate Lovett <katelovett@google.com>
Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
2024-12-19 20:06:21 +00:00
Ben Konyi
74669e4bf1
Add flutter widget-preview {start, clean} commands (#159510)
This is the initial tooling work for Flutter Widget Previews, adding two
commands: `flutter widget-preview start` and `flutter widget-preview
clean`.

The `start` command currently only checks to see if
`.dart_tool/widget_preview_scaffold/` exists and creates a new Flutter
project using the widget_preview_scaffold template if one isn't found.
The widget_preview_scaffold template currently only contains some
placeholder files and will be updated to include additional code
required by the scaffold.

The `clean` command simply deletes `.dart_tool/widget_preview_scaffold/`
if it's found.

This change also includes some refactoring of the `create` command in
order to share some project creation logic without requiring `flutter
widget-preview start` to spawn a new process simply to run `flutter
create -t widget_preview .dart_tool/widget_preview_scaffold`.

Related issue: https://github.com/flutter/flutter/issues/115704

---------

Co-authored-by: Andrew Kolos <andrewrkolos@gmail.com>
2024-12-04 21:51:08 +00:00