-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Original issue: flutter/flutter#112468
Extensions on Widget allow for a code style where, instead of nesting widgets by passing them as arguments to constructor calls of other widgets, we call extension methods. This style has the advantage of avoiding the left to right drift of nesting, requires fewer lines and is, in general, more readable, IMHO.
// Traditional style
Center(
child: Padding(
padding: EdgeInsets.all(8),
child: Text('Hello World'),
),
);
// Extension method style
Text('Hello World')
.padding(EdgeInsets.all(8))
.center();
extension WidgetExtensions on Widget {
Widget center() => Center(child: this);
Widget padding(EdgeInsetsGeometry padding) =>
Padding(padding: padding, child: this);
}The reason why I don't currently use this style is that it leads to a bad debugging experience. Widget instances record the location in source code where they are constructed, and dev tools use this information. When using the described extension methods to build widget trees, the extension method is recorded as the creation location for returned widget instances, which is not helpful to developers.
I would prefer that the call site of the extension method is recorded as the creation location for widgets returned from these types of extension methods.
One way this could be solved is by allowing extension authors to annotate methods:
@widgetFactory
Widget center() => Center(child: this);