Skip to content

Commit

Permalink
[DropdownButton]: Fix alignmentparameter doesn't work for hint wh…
Browse files Browse the repository at this point in the history
…en `isExpanded: true` (#102752)
  • Loading branch information
TahaTesser authored May 6, 2022
1 parent 6778084 commit 13d76b2
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/material/dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
if (widget.hint != null || (!_enabled && widget.disabledHint != null)) {
Widget displayedHint = _enabled ? widget.hint! : widget.disabledHint ?? widget.hint!;
if (widget.selectedItemBuilder == null)
displayedHint = _DropdownMenuItemContainer(child: displayedHint);
displayedHint = _DropdownMenuItemContainer(alignment: widget.alignment, child: displayedHint);

hintIndex = items.length;
items.add(DefaultTextStyle(
Expand Down
180 changes: 169 additions & 11 deletions packages/flutter/test/material/dropdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Widget buildDropdown({
List<String>? items = menuItems,
List<Widget> Function(BuildContext)? selectedItemBuilder,
double? itemHeight = kMinInteractiveDimension,
Alignment alignment = Alignment.center,
AlignmentDirectional alignment = AlignmentDirectional.centerStart,
TextDirection textDirection = TextDirection.ltr,
Size? mediaSize,
FocusNode? focusNode,
Expand Down Expand Up @@ -98,6 +98,7 @@ Widget buildDropdown({
items: listItems,
selectedItemBuilder: selectedItemBuilder,
itemHeight: itemHeight,
alignment: alignment,
menuMaxHeight: menuMaxHeight,
),
);
Expand All @@ -123,6 +124,7 @@ Widget buildDropdown({
items: listItems,
selectedItemBuilder: selectedItemBuilder,
itemHeight: itemHeight,
alignment: alignment,
menuMaxHeight: menuMaxHeight,
);
}
Expand All @@ -144,7 +146,7 @@ Widget buildFrame({
List<String>? items = menuItems,
List<Widget> Function(BuildContext)? selectedItemBuilder,
double? itemHeight = kMinInteractiveDimension,
Alignment alignment = Alignment.center,
AlignmentDirectional alignment = AlignmentDirectional.centerStart,
TextDirection textDirection = TextDirection.ltr,
Size? mediaSize,
FocusNode? focusNode,
Expand All @@ -153,13 +155,14 @@ Widget buildFrame({
Color? dropdownColor,
bool isFormField = false,
double? menuMaxHeight,
Alignment dropdownAlignment = Alignment.center,
}) {
return TestApp(
textDirection: textDirection,
mediaSize: mediaSize,
child: Material(
child: Align(
alignment: alignment,
alignment: dropdownAlignment,
child: RepaintBoundary(
child: buildDropdown(
isFormField: isFormField,
Expand All @@ -183,6 +186,7 @@ Widget buildFrame({
items: items,
selectedItemBuilder: selectedItemBuilder,
itemHeight: itemHeight,
alignment: alignment,
menuMaxHeight: menuMaxHeight,
),
),
Expand Down Expand Up @@ -1180,19 +1184,19 @@ void main() {
// so that it fits within the frame.

await popUpAndDown(
buildFrame(alignment: Alignment.topLeft, value: menuItems.last, onChanged: onChanged),
buildFrame(dropdownAlignment: Alignment.topLeft, value: menuItems.last, onChanged: onChanged),
);
expect(menuRect.topLeft, Offset.zero);
expect(menuRect.topRight, Offset(menuRect.width, 0.0));

await popUpAndDown(
buildFrame(alignment: Alignment.topCenter, value: menuItems.last, onChanged: onChanged),
buildFrame(dropdownAlignment: Alignment.topCenter, value: menuItems.last, onChanged: onChanged),
);
expect(menuRect.topLeft, Offset(buttonRect.left, 0.0));
expect(menuRect.topRight, Offset(buttonRect.right, 0.0));

await popUpAndDown(
buildFrame(alignment: Alignment.topRight, value: menuItems.last, onChanged: onChanged),
buildFrame(dropdownAlignment: Alignment.topRight, value: menuItems.last, onChanged: onChanged),
);
expect(menuRect.topLeft, Offset(800.0 - menuRect.width, 0.0));
expect(menuRect.topRight, const Offset(800.0, 0.0));
Expand All @@ -1202,7 +1206,7 @@ void main() {
// is selected) and shifted horizontally so that it fits within the frame.

await popUpAndDown(
buildFrame(alignment: Alignment.centerLeft, value: menuItems.first, onChanged: onChanged),
buildFrame(dropdownAlignment: Alignment.centerLeft, value: menuItems.first, onChanged: onChanged),
);
expect(menuRect.topLeft, Offset(0.0, buttonRect.top));
expect(menuRect.topRight, Offset(menuRect.width, buttonRect.top));
Expand All @@ -1214,7 +1218,7 @@ void main() {
expect(menuRect.topRight, buttonRect.topRight);

await popUpAndDown(
buildFrame(alignment: Alignment.centerRight, value: menuItems.first, onChanged: onChanged),
buildFrame(dropdownAlignment: Alignment.centerRight, value: menuItems.first, onChanged: onChanged),
);
expect(menuRect.topLeft, Offset(800.0 - menuRect.width, buttonRect.top));
expect(menuRect.topRight, Offset(800.0, buttonRect.top));
Expand All @@ -1224,19 +1228,19 @@ void main() {
// so that it fits within the frame.

await popUpAndDown(
buildFrame(alignment: Alignment.bottomLeft, value: menuItems.first, onChanged: onChanged),
buildFrame(dropdownAlignment: Alignment.bottomLeft, value: menuItems.first, onChanged: onChanged),
);
expect(menuRect.bottomLeft, const Offset(0.0, 600.0));
expect(menuRect.bottomRight, Offset(menuRect.width, 600.0));

await popUpAndDown(
buildFrame(alignment: Alignment.bottomCenter, value: menuItems.first, onChanged: onChanged),
buildFrame(dropdownAlignment: Alignment.bottomCenter, value: menuItems.first, onChanged: onChanged),
);
expect(menuRect.bottomLeft, Offset(buttonRect.left, 600.0));
expect(menuRect.bottomRight, Offset(buttonRect.right, 600.0));

await popUpAndDown(
buildFrame(alignment: Alignment.bottomRight, value: menuItems.first, onChanged: onChanged),
buildFrame(dropdownAlignment: Alignment.bottomRight, value: menuItems.first, onChanged: onChanged),
);
expect(menuRect.bottomLeft, Offset(800.0 - menuRect.width, 600.0));
expect(menuRect.bottomRight, const Offset(800.0, 600.0));
Expand Down Expand Up @@ -3681,4 +3685,158 @@ void main() {
..rrect(rrect: const RRect.fromLTRBXY(0.0, 0.0, 800.0, 208.0, radius, radius)),
);
});

testWidgets('DropdownButton hint alignment', (WidgetTester tester) async {
final Key buttonKey = UniqueKey();
const String hintText = 'hint';

// DropdownButton with `isExpanded: false` (default)
// AlignmentDirectional.centerStart (default)
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
itemHeight: 100.0,
hint: const Text(hintText)),
);
expect(tester.getTopLeft(find.text(hintText)).dx, 348.0);
expect(tester.getTopLeft(find.text(hintText)).dy, 292.0);
// AlignmentDirectional.topStart
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
alignment: AlignmentDirectional.topStart,
itemHeight: 100.0,
hint: const Text(hintText)),
);
expect(tester.getTopLeft(find.text(hintText)).dx, 348.0);
expect(tester.getTopLeft(find.text(hintText)).dy, 250.0);
// AlignmentDirectional.bottomStart
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
alignment: AlignmentDirectional.bottomStart,
itemHeight: 100.0,
hint: const Text(hintText)),
);
expect(tester.getBottomLeft(find.text(hintText)).dx, 348.0);
expect(tester.getBottomLeft(find.text(hintText)).dy, 350.0);
// AlignmentDirectional.center
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
alignment: AlignmentDirectional.center,
itemHeight: 100.0,
hint: const Text(hintText)),
);
expect(tester.getCenter(find.text(hintText)).dx, 388.0);
expect(tester.getCenter(find.text(hintText)).dy, 300.0);
// AlignmentDirectional.topEnd
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
alignment: AlignmentDirectional.topEnd,
itemHeight: 100.0,
hint: const Text(hintText)),
);
expect(tester.getTopRight(find.text(hintText)).dx, 428.0);
expect(tester.getTopRight(find.text(hintText)).dy, 250.0);
// AlignmentDirectional.centerEnd
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
alignment: AlignmentDirectional.centerEnd,
itemHeight: 100.0,
hint: const Text(hintText)),
);
expect(tester.getTopRight(find.text(hintText)).dx, 428.0);
expect(tester.getTopRight(find.text(hintText)).dy, 292.0);
// AlignmentDirectional.topEnd
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
alignment: AlignmentDirectional.bottomEnd,
itemHeight: 100.0,
hint: const Text(hintText)),
);
expect(tester.getTopRight(find.text(hintText)).dx, 428.0);
expect(tester.getTopRight(find.text(hintText)).dy, 334.0);

// DropdownButton with `isExpanded: true`
// AlignmentDirectional.centerStart (default)
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
itemHeight: 100.0,
isExpanded: true,
hint: const Text(hintText)),
);
expect(tester.getTopLeft(find.text(hintText)).dx, 0.0);
expect(tester.getTopLeft(find.text(hintText)).dy, 292.0);
// AlignmentDirectional.topStart
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
itemHeight: 100.0,
isExpanded: true,
alignment: AlignmentDirectional.topStart,
hint: const Text(hintText)),
);
expect(tester.getTopLeft(find.text(hintText)).dx, 0.0);
expect(tester.getTopLeft(find.text(hintText)).dy, 250.0);
// AlignmentDirectional.bottomStart
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
itemHeight: 100.0,
isExpanded: true,
alignment: AlignmentDirectional.bottomStart,
hint: const Text(hintText)),
);
expect(tester.getBottomLeft(find.text(hintText)).dx, 0.0);
expect(tester.getBottomLeft(find.text(hintText)).dy, 350.0);
// AlignmentDirectional.center
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
itemHeight: 100.0,
isExpanded: true,
alignment: AlignmentDirectional.center,
hint: const Text(hintText)),
);
expect(tester.getCenter(find.text(hintText)).dx, 388.0);
expect(tester.getCenter(find.text(hintText)).dy, 300.0);
// AlignmentDirectional.topEnd
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
itemHeight: 100.0,
isExpanded: true,
alignment: AlignmentDirectional.topEnd,
hint: const Text(hintText)),
);
expect(tester.getTopRight(find.text(hintText)).dx, 776.0);
expect(tester.getTopRight(find.text(hintText)).dy, 250.0);
// AlignmentDirectional.centerEnd
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
itemHeight: 100.0,
isExpanded: true,
alignment: AlignmentDirectional.centerEnd,
hint: const Text(hintText)),
);
expect(tester.getTopRight(find.text(hintText)).dx, 776.0);
expect(tester.getTopRight(find.text(hintText)).dy, 292.0);
// AlignmentDirectional.bottomEnd
await tester.pumpWidget(buildFrame(
buttonKey: buttonKey,
mediaSize: const Size(800, 600),
itemHeight: 100.0,
isExpanded: true,
alignment: AlignmentDirectional.bottomEnd,
hint: const Text(hintText)),
);
expect(tester.getBottomRight(find.text(hintText)).dx, 776.0);
expect(tester.getBottomRight(find.text(hintText)).dy, 350.0);
});
}

0 comments on commit 13d76b2

Please sign in to comment.