flutter / flutter/flutter-intellij
Flutter IDE Wish: wrap with a builder, a stateful builder, or a layout builder
- Dominant language
- Java
- Stars
- 2k
- Forks
- 356
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 27
Description
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.
Contributor guide
Assessment
This issue has not been assessed yet.