Skip to content

Commit 59fd06a

Browse files
authored
Merge pull request #351 from AhmedLSayed9/avoid_container_objects
Avoid Container objects when possible for better performance [Flutter…
2 parents 5130c57 + 10e745d commit 59fd06a

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed

packages/dropdown_button2/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Form fields onChange callback should be called on reset [Flutter core].
77
- Implement switch expressions.
88
- Fix memory leak in CurvedAnimation [Flutter core].
9+
- Avoid Container objects when possible for better performance [Flutter core].
910

1011
## 3.0.0-beta.21
1112

packages/dropdown_button2/lib/src/dropdown_button2.dart

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>>
785785
? _textStyle!
786786
: _textStyle!.copyWith(color: Theme.of(context).disabledColor),
787787
child: widget.customButton ??
788-
Container(
788+
_ConditionalDecoratedBox(
789789
decoration: _buttonStyle?.decoration?.copyWith(
790790
boxShadow: _buttonStyle!.decoration!.boxShadow ??
791791
kElevationToShadow[_buttonStyle!.elevation ?? 0],
@@ -794,45 +794,47 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>>
794794
boxShadow: _buttonStyle!.foregroundDecoration!.boxShadow ??
795795
kElevationToShadow[_buttonStyle!.elevation ?? 0],
796796
),
797-
padding: (_buttonStyle?.padding ??
798-
padding.resolve(Directionality.of(context)))
799-
.add(
800-
// When buttonWidth & dropdownWidth is null, their width will be calculated
801-
// from the maximum width of menu items or the hint text (width of IndexedStack).
802-
// We need to add MenuHorizontalPadding so menu width adapts to max items width with padding properly
803-
_buttonStyle?.width == null && _dropdownStyle.width == null
804-
? _getMenuPadding()
805-
.resolve(Directionality.of(context))
806-
.copyWith(top: 0, bottom: 0)
807-
: EdgeInsets.zero,
808-
),
809797
height: buttonHeight,
810798
width: _buttonStyle?.width,
811-
child: Row(
812-
mainAxisAlignment: MainAxisAlignment.spaceBetween,
813-
mainAxisSize: MainAxisSize.min,
814-
children: <Widget>[
815-
if (widget.isExpanded)
816-
Expanded(child: innerItemsWidget)
817-
else
818-
innerItemsWidget,
819-
IconTheme(
820-
data: IconThemeData(
821-
color: _iconColor,
822-
size: _iconStyle.iconSize,
823-
),
824-
child: ValueListenableBuilder<bool>(
825-
valueListenable: _isMenuOpen,
826-
builder: (BuildContext context, bool isOpen, _) {
827-
return _iconStyle.openMenuIcon != null
828-
? isOpen
829-
? _iconStyle.openMenuIcon!
830-
: _iconStyle.icon
831-
: _iconStyle.icon;
832-
},
799+
child: Padding(
800+
padding: (_buttonStyle?.padding ??
801+
padding.resolve(Directionality.of(context)))
802+
.add(
803+
// When buttonWidth & dropdownWidth is null, their width will be calculated
804+
// from the maximum width of menu items or the hint text (width of IndexedStack).
805+
// We need to add MenuHorizontalPadding so menu width adapts to max items width with padding properly
806+
_buttonStyle?.width == null && _dropdownStyle.width == null
807+
? _getMenuPadding()
808+
.resolve(Directionality.of(context))
809+
.copyWith(top: 0, bottom: 0)
810+
: EdgeInsets.zero,
811+
),
812+
child: Row(
813+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
814+
mainAxisSize: MainAxisSize.min,
815+
children: <Widget>[
816+
if (widget.isExpanded)
817+
Expanded(child: innerItemsWidget)
818+
else
819+
innerItemsWidget,
820+
IconTheme(
821+
data: IconThemeData(
822+
color: _iconColor,
823+
size: _iconStyle.iconSize,
824+
),
825+
child: ValueListenableBuilder<bool>(
826+
valueListenable: _isMenuOpen,
827+
builder: (BuildContext context, bool isOpen, _) {
828+
return _iconStyle.openMenuIcon != null
829+
? isOpen
830+
? _iconStyle.openMenuIcon!
831+
: _iconStyle.icon
832+
: _iconStyle.icon;
833+
},
834+
),
833835
),
834-
),
835-
],
836+
],
837+
),
836838
),
837839
),
838840
);

packages/dropdown_button2/lib/src/dropdown_menu_item.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,9 @@ class _DropdownMenuItemContainer extends StatelessWidget {
111111

112112
@override
113113
Widget build(BuildContext context) {
114-
return Container(
114+
return SizedBox(
115115
height: intrinsicHeight ? null : height,
116-
alignment: alignment,
117-
child: child,
116+
child: Align(alignment: alignment, child: child),
118117
);
119118
}
120119
}
@@ -239,7 +238,7 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
239238
Widget build(BuildContext context) {
240239
final DropdownItem<T> dropdownItem = widget.route.items[widget.itemIndex];
241240

242-
Widget child = Container(
241+
Widget child = Padding(
243242
padding: (menuItemStyle.padding ?? _kMenuItemPadding)
244243
.resolve(widget.textDirection),
245244
child: dropdownItem,

packages/dropdown_button2/lib/src/utils.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,35 @@ void _uniqueValueAssert<T>(
3636
);
3737
}
3838

39+
class _ConditionalDecoratedBox extends StatelessWidget {
40+
const _ConditionalDecoratedBox({
41+
required this.child,
42+
this.height,
43+
this.width,
44+
this.decoration,
45+
this.foregroundDecoration,
46+
});
47+
48+
final double? height;
49+
final double? width;
50+
final Decoration? decoration;
51+
final Decoration? foregroundDecoration;
52+
final Widget child;
53+
54+
@override
55+
Widget build(BuildContext context) {
56+
return decoration != null || foregroundDecoration != null
57+
? Container(
58+
height: height,
59+
width: width,
60+
decoration: decoration,
61+
foregroundDecoration: foregroundDecoration,
62+
child: child,
63+
)
64+
: SizedBox(height: height, width: width, child: child);
65+
}
66+
}
67+
3968
extension _InputDecorationExtension on InputDecoration {
4069
InputDecoration updateSurroundingElements({
4170
required Widget? error,

0 commit comments

Comments
 (0)