flutter / flutter-intellij

Flutter Plugin for IntelliJ
https://flutter.dev/using-ide
BSD 3-Clause "New" or "Revised" License
1.97k stars 316 forks source link

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

Open HansMuller opened 7 years ago

HansMuller commented 7 years ago

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: https://github.com/flutter/flutter/issues/4581.

ThinkDigitalSoftware commented 5 years ago

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 commented 4 years ago

This is a feature I've need consistently for the past 2 years. I am happy to implement it. I have found the locations of where the current StreamBuilder wrapper is.

@pq Would you be able to assist me with the approach to running and testing my edits locally?

pq commented 4 years ago

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

MrBirb commented 3 years ago

Looks like #45656 added the Builder widget.