@@ -7,6 +7,7 @@ import 'package:analysis_server/src/services/correction/selection_analyzer.dart'
77import 'package:analysis_server_plugin/edit/dart/correction_producer.dart' ;
88import 'package:analyzer/dart/ast/ast.dart' ;
99import 'package:analyzer/dart/element/element2.dart' ;
10+ import 'package:analyzer/dart/element/type.dart' ;
1011import 'package:analyzer/source/source_range.dart' ;
1112import 'package:analyzer/src/dart/ast/extensions.dart' ;
1213import 'package:analyzer/src/utilities/extensions/flutter.dart' ;
@@ -30,6 +31,14 @@ class FlutterWrap extends MultiCorrectionProducer {
3031 if (! widgetType.isExactWidgetTypeContainer) {
3132 producers.add (_FlutterWrapContainer (widgetExpr, context: context));
3233 }
34+ if (! widgetType.isExactWidgetTypeExpanded &&
35+ (widgetExpr.isParentFlexWidget || ! widgetExpr.isParentWidget)) {
36+ producers.add (_FlutterWrapExpanded (widgetExpr, context: context));
37+ }
38+ if (! widgetType.isExactWidgetTypeFlexible &&
39+ (widgetExpr.isParentFlexWidget || ! widgetExpr.isParentWidget)) {
40+ producers.add (_FlutterWrapFlexible (widgetExpr, context: context));
41+ }
3342 if (! widgetType.isExactWidgetTypePadding) {
3443 producers.add (_FlutterWrapPadding (widgetExpr, context: context));
3544 }
@@ -138,6 +147,36 @@ class _FlutterWrapContainer extends _WrapSingleWidget {
138147 String get _parentLibraryUri => widgetsUri;
139148}
140149
150+ /// A correction processor that can make one of the possible changes computed by
151+ /// the [FlutterWrap] producer.
152+ class _FlutterWrapExpanded extends _WrapSingleWidget {
153+ _FlutterWrapExpanded (super .widgetExpr, {required super .context});
154+
155+ @override
156+ AssistKind get assistKind => DartAssistKind .FLUTTER_WRAP_EXPANDED ;
157+
158+ @override
159+ String get _parentClassName => 'Expanded' ;
160+
161+ @override
162+ String get _parentLibraryUri => widgetsUri;
163+ }
164+
165+ /// A correction processor that can make one of the possible changes computed by
166+ /// the [FlutterWrap] producer.
167+ class _FlutterWrapFlexible extends _WrapSingleWidget {
168+ _FlutterWrapFlexible (super .widgetExpr, {required super .context});
169+
170+ @override
171+ AssistKind get assistKind => DartAssistKind .FLUTTER_WRAP_FLEXIBLE ;
172+
173+ @override
174+ String get _parentClassName => 'Flexible' ;
175+
176+ @override
177+ String get _parentLibraryUri => widgetsUri;
178+ }
179+
141180/// A correction processor that can make one of the possible changes computed by
142181/// the [FlutterWrap] producer.
143182class _FlutterWrapGeneric extends _WrapSingleWidget {
@@ -350,3 +389,53 @@ abstract class _WrapSingleWidget extends ResolvedCorrectionProducer {
350389 });
351390 }
352391}
392+
393+ extension on Expression {
394+ /// Return `true` if the parent is a `Flex` widget creation.
395+ ///
396+ /// This is used to determine if the widget is wrapped in a `Row` , `Column` ,
397+ /// or `Flex` widget.
398+ bool get isParentFlexWidget {
399+ var parent = _getParentInstanceCreationExpression ();
400+ if (parent == null || ! parent.isWidgetCreation) {
401+ return false ;
402+ }
403+ return parent.staticType.isWidgetFlexType;
404+ }
405+
406+ /// Return `true` if the parent is a widget creation.
407+ ///
408+ /// This tells if this is a direct child of a widget creation.
409+ /// It will return `false` if we are assigning this to a variable or
410+ /// returning it from a function or other similar cases.
411+ bool get isParentWidget {
412+ var parent = _getParentInstanceCreationExpression ();
413+ return parent != null && parent.isWidgetCreation;
414+ }
415+
416+ /// Return the parent `InstanceCreationExpression` if it exists.
417+ ///
418+ /// This is used to find the parent widget creation if it exists.
419+ InstanceCreationExpression ? _getParentInstanceCreationExpression () {
420+ var self = this ;
421+ NamedExpression ? namedExpression;
422+ if (self.parent case ListLiteral listLiteral) {
423+ if (listLiteral.parent case NamedExpression parent) {
424+ namedExpression = parent;
425+ }
426+ }
427+ // NamedExpression (child:), ArgumentList, InstanceCreationExpression
428+ if ((namedExpression ?? self.parent)? .parent? .parent
429+ case InstanceCreationExpression parent? ) {
430+ return parent;
431+ }
432+ return null ;
433+ }
434+ }
435+
436+ extension on DartType ? {
437+ bool get isWidgetFlexType {
438+ var self = this ;
439+ return self is InterfaceType && self.element3.isFlexWidget;
440+ }
441+ }
0 commit comments