diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index d1d545425d..c08a45a0a7 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -668,6 +668,7 @@ class _DropdownMenuItemContainer extends StatelessWidget { /// The [child] argument is required. const _DropdownMenuItemContainer({ Key? key, + this.alignment = AlignmentDirectional.centerStart, required this.child, }) : assert(child != null), super(key: key); @@ -677,11 +678,23 @@ class _DropdownMenuItemContainer extends StatelessWidget { /// Typically a [Text] widget. final Widget child; + /// Defines how the item is positioned within the container. + /// + /// This property must not be null. It defaults to [AlignmentDirectional.centerStart]. + /// + /// See also: + /// + /// * [Alignment], a class with convenient constants typically used to + /// specify an [AlignmentGeometry]. + /// * [AlignmentDirectional], like [Alignment] for specifying alignments + /// relative to text direction. + final AlignmentGeometry alignment; + @override Widget build(BuildContext context) { return Container( constraints: const BoxConstraints(minHeight: _kMenuItemHeight), - alignment: AlignmentDirectional.centerStart, + alignment: alignment, child: child, ); } @@ -700,9 +713,10 @@ class DropdownMenuItem extends _DropdownMenuItemContainer { this.onTap, this.value, this.enabled = true, + AlignmentGeometry alignment = AlignmentDirectional.centerStart, required Widget child, }) : assert(child != null), - super(key: key, child: child); + super(key: key, alignment:alignment, child: child); /// Called when the dropdown menu item is tapped. final VoidCallback? onTap; @@ -866,6 +880,7 @@ class DropdownButton extends StatefulWidget { this.dropdownColor, this.menuMaxHeight, this.enableFeedback, + this.alignment = AlignmentDirectional.centerStart, // When adding new arguments, consider adding similar arguments to // DropdownButtonFormField. }) : assert(items == null || items.isEmpty || value == null || @@ -1138,6 +1153,18 @@ class DropdownButton extends StatefulWidget { /// * [Feedback] for providing platform-specific feedback to certain actions. final bool? enableFeedback; + /// Defines how the hint or the selected item is positioned within the button. + /// + /// This property must not be null. It defaults to [AlignmentDirectional.centerStart]. + /// + /// See also: + /// + /// * [Alignment], a class with convenient constants typically used to + /// specify an [AlignmentGeometry]. + /// * [AlignmentDirectional], like [Alignment] for specifying alignments + /// relative to text direction. + final AlignmentGeometry alignment; + @override _DropdownButtonState createState() => _DropdownButtonState(); } @@ -1407,7 +1434,7 @@ class _DropdownButtonState extends State> with WidgetsBindi } else { innerItemsWidget = IndexedStack( index: _selectedIndex ?? hintIndex, - alignment: AlignmentDirectional.centerStart, + alignment: widget.alignment, children: widget.isDense ? items : items.map((Widget item) { return widget.itemHeight != null ? SizedBox(height: widget.itemHeight, child: item) @@ -1537,6 +1564,8 @@ class DropdownButtonFormField extends FormField { bool autovalidate = false, AutovalidateMode? autovalidateMode, double? menuMaxHeight, + bool? enableFeedback, + AlignmentGeometry alignment = AlignmentDirectional.centerStart, }) : assert(items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) { return item.value == value; @@ -1606,6 +1635,8 @@ class DropdownButtonFormField extends FormField { autofocus: autofocus, dropdownColor: dropdownColor, menuMaxHeight: menuMaxHeight, + enableFeedback: enableFeedback, + alignment: alignment, ), ), ); diff --git a/packages/flutter/test/material/dropdown_form_field_test.dart b/packages/flutter/test/material/dropdown_form_field_test.dart index 7c2babb9b3..cca6f1bfa2 100644 --- a/packages/flutter/test/material/dropdown_form_field_test.dart +++ b/packages/flutter/test/material/dropdown_form_field_test.dart @@ -43,6 +43,7 @@ Widget buildFormFrame({ List? items = menuItems, Alignment alignment = Alignment.center, TextDirection textDirection = TextDirection.ltr, + AlignmentGeometry buttonAlignment = AlignmentDirectional.centerStart, }) { return TestApp( textDirection: textDirection, @@ -72,6 +73,7 @@ Widget buildFormFrame({ child: Text(item, key: ValueKey(item + 'Text')), ); }).toList(), + alignment: buttonAlignment, ), ), ), @@ -815,4 +817,20 @@ void main() { expect(() => builder(), throwsAssertionError); }); + testWidgets('DropdownButtonFormField - Custom button alignment', (WidgetTester tester) async { + await tester.pumpWidget(buildFormFrame( + buttonAlignment: AlignmentDirectional.center, + items: ['one'], + value: 'one', + )); + + final RenderBox buttonBox = tester.renderObject(find.byType(IndexedStack)); + final RenderBox selectedItemBox = tester.renderObject(find.text('one')); + + // Should be center-center aligned. + expect( + buttonBox.localToGlobal(Offset(buttonBox.size.width / 2.0, buttonBox.size.height / 2.0)), + selectedItemBox.localToGlobal(Offset(selectedItemBox.size.width / 2.0, selectedItemBox.size.height / 2.0)), + ); + }); } diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index 5818bd5d62..2eda72f4b2 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -3253,6 +3253,77 @@ void main() { expect(Focus.maybeOf(disabledItem), null, reason: 'Disabled menu item should not be able to request focus'); }); + testWidgets('alignment test', (WidgetTester tester) async { + final Key buttonKey = UniqueKey(); + Widget buildFrame({AlignmentGeometry? buttonAlignment, AlignmentGeometry? menuAlignment}) { + return MaterialApp( + home: Scaffold( + body: Center( + child: DropdownButton( + key: buttonKey, + alignment: buttonAlignment ?? AlignmentDirectional.centerStart, + value: 'enabled', + onChanged: onChanged, + items: >[ + DropdownMenuItem( + alignment: buttonAlignment ?? AlignmentDirectional.centerStart, + enabled: false, + child: const Text('disabled'), + ), + DropdownMenuItem( + alignment: buttonAlignment ?? AlignmentDirectional.centerStart, + value: 'enabled', + child: const Text('enabled'), + ) + ], + ), + ), + ), + ); + } + + await tester.pumpWidget(buildFrame()); + + final RenderBox buttonBox = tester.renderObject(find.byKey(buttonKey)); + RenderBox selectedItemBox = tester.renderObject(find.text('enabled')); + // Default to center-start aligned. + expect( + buttonBox.localToGlobal(Offset(0.0, buttonBox.size.height / 2.0)), + selectedItemBox.localToGlobal(Offset(0.0, selectedItemBox.size.height / 2.0)), + ); + + await tester.pumpWidget(buildFrame( + buttonAlignment: AlignmentDirectional.center, + menuAlignment: AlignmentDirectional.center, + )); + + selectedItemBox = tester.renderObject(find.text('enabled')); + // Should be center-center aligned, the icon size is 24.0 pixels. + expect( + buttonBox.localToGlobal(Offset((buttonBox.size.width -24.0) / 2.0, buttonBox.size.height / 2.0)), + selectedItemBox.localToGlobal(Offset(selectedItemBox.size.width / 2.0, selectedItemBox.size.height / 2.0)), + ); + + // Open dropdown. + await tester.tap(find.text('enabled').hitTestable()); + await tester.pumpAndSettle(); + + final RenderBox selectedItemBoxInMenu = tester.renderObjectList(find.text('enabled')).toList()[1]; + final Finder menu = find.byWidgetPredicate((Widget widget) { + return widget.runtimeType.toString().startsWith('_DropdownMenu<'); + }); + final Rect menuRect = tester.getRect(menu); + final Offset center = selectedItemBoxInMenu.localToGlobal( + Offset(selectedItemBoxInMenu.size.width / 2.0, selectedItemBoxInMenu.size.height / 2.0) + ); + + expect(center.dx, menuRect.topCenter.dx,); + expect( + center.dy, + selectedItemBox.localToGlobal(Offset(selectedItemBox.size.width / 2.0, selectedItemBox.size.height / 2.0)).dy, + ); + }); + group('feedback', () { late FeedbackTester feedback;