Skip to content

Allow to put dropdowns with dynamic width into the horizontal list #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions packages/dropdown_button2/lib/src/dropdown_button2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -802,23 +802,28 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>> with WidgetsBin
// which enhances the performance when dealing with big items list.
// Note: Both buttonHeight & buttonWidth must be specified to avoid changing
// button's size when selecting different items, which is a bad UX.
return buttonHeight != null && _buttonStyle?.width != null
return buttonHeight != null
? Align(
alignment: widget.alignment,
child: item,
)
: IndexedStack(
index: _selectedIndex ?? hintIndex,
alignment: widget.alignment,
children: buttonHeight != null
? buttonItems
: buttonItems.map((item) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[item],
);
}).toList(),
);
: _buttonStyle?.width != null
? IndexedStack(
index: _selectedIndex ?? hintIndex,
alignment: widget.alignment,
children: buttonHeight != null
? buttonItems
: buttonItems.map((item) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[item],
);
}).toList(),
)
: SizedBox(
height: buttonHeight,
child: item,
);
},
);

Expand Down Expand Up @@ -849,7 +854,10 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>> with WidgetsBin
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
if (widget.isExpanded) Expanded(child: innerItemsWidget) else innerItemsWidget,
if (widget.isExpanded && _buttonStyle?.width != null)
Expanded(child: innerItemsWidget)
else
innerItemsWidget,
IconTheme(
data: IconThemeData(
color: _iconColor,
Expand Down
20 changes: 10 additions & 10 deletions packages/dropdown_button2/lib/src/dropdown_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -409,29 +409,29 @@ class _DropdownMenuRouteLayout<T> extends SingleChildLayoutDelegate {
DropdownDirection.textDirection => switch (textDirection!) {
TextDirection.rtl => clampDouble(
buttonRect.right - childSize.width + offset.dx,
0.0,
size.width - childSize.width,
route.dropdownStyle.margin?.right ?? 0.00,
size.width - childSize.width - (route.dropdownStyle.margin?.left ?? 0),
),
TextDirection.ltr => clampDouble(
buttonRect.left + offset.dx,
0.0,
size.width - childSize.width,
route.dropdownStyle.margin?.left ?? 0.00,
size.width - childSize.width - (route.dropdownStyle.margin?.right ?? 0),
),
},
DropdownDirection.right => clampDouble(
buttonRect.left + offset.dx,
0.0,
size.width - childSize.width,
route.dropdownStyle.margin?.right ?? 0.00,
size.width - childSize.width - (route.dropdownStyle.margin?.left ?? 0),
),
DropdownDirection.left => clampDouble(
buttonRect.right - childSize.width + offset.dx,
0.0,
size.width - childSize.width,
route.dropdownStyle.margin?.right ?? 0.00,
size.width - childSize.width - (route.dropdownStyle.margin?.right ?? 0),
),
DropdownDirection.center => clampDouble(
(size.width - childSize.width) / 2 + offset.dx,
0.0,
size.width - childSize.width,
route.dropdownStyle.margin?.left ?? 0.00,
size.width - childSize.width - (route.dropdownStyle.margin?.right ?? 0),
),
};

Expand Down
6 changes: 6 additions & 0 deletions packages/dropdown_button2/lib/src/dropdown_style_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ part of 'dropdown_button2.dart';
class DropdownStyleData {
/// Creates a DropdownStyleData.
const DropdownStyleData({
this.margin,
this.maxHeight,
this.width,
this.padding,
Expand All @@ -21,6 +22,9 @@ class DropdownStyleData {
this.dropdownBuilder,
});

/// The outer margin of the dropdown menu
final EdgeInsets? margin;

/// The maximum height of the dropdown menu
///
/// The maximum height of the menu must be at least one row shorter than
Expand Down Expand Up @@ -112,6 +116,7 @@ class DropdownStyleData {
/// Create a clone of the current [DropdownStyleData] but with the provided
/// parameters overridden.
DropdownStyleData copyWith({
EdgeInsets? margin,
double? maxHeight,
double? width,
EdgeInsetsGeometry? padding,
Expand All @@ -129,6 +134,7 @@ class DropdownStyleData {
DropdownBuilder? dropdownBuilder,
}) {
return DropdownStyleData(
margin: margin ?? this.margin,
maxHeight: maxHeight ?? this.maxHeight,
width: width ?? this.width,
padding: padding ?? this.padding,
Expand Down