Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter IDE Wish: wrap with a builder, a stateful builder, or a layout builder #814

Open
HansMuller opened this issue Mar 13, 2017 · 4 comments

Comments

@HansMuller
Copy link

HansMuller commented Mar 13, 2017

Another useful variation on the wrap this new widget expression with another new widget idiom: inserting a new expression for one of the common widget builder classes, Builder, StatefulBuilder, LayoutBuilder.

There are some idomatic scenarios where a Builder widget is needed. For example.

If you write something like the following and then try pressing the button, the Scaffold.of(context) expression will fail because the context doesn't actually include the Scaffold.

@override
Widget build(BuildContext context) {
  return new Scaffold(
    ...
    body: new FlatButton(
      child: new Text('SHOW SNACKBAR'),
      onPressed: () {
        Scaffold.of(context).showSnackBar(new SnackBar(
          content: ...
        ));
      },
    ),
  );
}

Wrapping the button in a builder corrects the problem because the Scaffold is within the parent of the Builder's BuildContext.

@override
Widget build(BuildContext context) {
  return new Scaffold(
    ...
    body: new Builder(
      builder: (BuildContext scaffoldContext) {
        return new FlatButton(
          child: new Text('SHOW SNACKBAR'),
          onPressed: () {
            Scaffold.of(scaffoldContext).showSnackBar(new SnackBar(
              content: ...
            ));
          },
        );
      },
    ),
  );
}

I've called the Builder function's BuildContext parameter scaffoldContext for clarity. I don't think that sort of rename would be possible for the IDE to generate.

It would be really impressive if the analyzer could detect the origin Scaffold.of(context) and the IDE could suggest this remedy.

Just FTR, the potential difficulty of referring to inherited widgets defined in the same build function is also noted here: flutter/flutter#4581.

@ThinkDigitalSoftware
Copy link

Adding support for this request. It would be good to add all the different variations of what could go there, I.E.
Widgets where the selected widget is a Child or one of the children like we already have, and widgets where the selected widget is returned from a method, as in above. I can't think of any more currently, 😄

@slightfoot
Copy link
Member

@pq
Copy link
Contributor

pq commented Oct 30, 2019

Cool! @slightfoot: in IDEA, you should be able to simply run (and debug) flutter_wrap_stream_builder_test. That's the place to start!

dart-bot pushed a commit to dart-lang/sdk that referenced this issue Apr 12, 2021
Fixes issues such as

* flutter/flutter-intellij#814
* #33957 .

Essentially, in Flutter, it's often useful to create a new context in the UI/widget tree as a place to begin a search towards the trunk of the tree for various state structures attached to lower levels of the tree. The basic `Builder` widget is the official means for creating such a context/entry-point.

The patches in this PR are essentially a copy-paste-tweak from two types of existing, similar assists. The main origin was taken from `FlutterWrapStreamBuilder` which wraps client code with a constructor for the StreamBuilder sub-class. The new assist, provided here, is a little more basic/general than the StreamBuilder so it was able to also adopt assertion and test code from some of the other assists for basic widgets.

Closes #45656
#45656

GitOrigin-RevId: 00a5043
Change-Id: I5f93837af571b4974e35da131112f82cd9359697
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194941
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
@MrBirb
Copy link

MrBirb commented Apr 14, 2021

Looks like #45656 added the Builder widget.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants