Unverified Commit 368537b9 authored by David Skelly's avatar David Skelly Committed by GitHub

Add padding to DropdownButton (#115806)

* add padding param to DropdownButton

* improve padding comment

* update test

* Add more context to documentation

* update padding documentation with more detail

---------
Co-authored-by: 's avatarKate Lovett <katelovett@google.com>
parent f85a75db
...@@ -886,6 +886,7 @@ class DropdownButton<T> extends StatefulWidget { ...@@ -886,6 +886,7 @@ class DropdownButton<T> extends StatefulWidget {
this.enableFeedback, this.enableFeedback,
this.alignment = AlignmentDirectional.centerStart, this.alignment = AlignmentDirectional.centerStart,
this.borderRadius, this.borderRadius,
this.padding,
// When adding new arguments, consider adding similar arguments to // When adding new arguments, consider adding similar arguments to
// DropdownButtonFormField. // DropdownButtonFormField.
}) : assert(items == null || items.isEmpty || value == null || }) : assert(items == null || items.isEmpty || value == null ||
...@@ -929,6 +930,7 @@ class DropdownButton<T> extends StatefulWidget { ...@@ -929,6 +930,7 @@ class DropdownButton<T> extends StatefulWidget {
this.enableFeedback, this.enableFeedback,
this.alignment = AlignmentDirectional.centerStart, this.alignment = AlignmentDirectional.centerStart,
this.borderRadius, this.borderRadius,
this.padding,
required InputDecoration inputDecoration, required InputDecoration inputDecoration,
required bool isEmpty, required bool isEmpty,
required bool isFocused, required bool isFocused,
...@@ -1115,6 +1117,17 @@ class DropdownButton<T> extends StatefulWidget { ...@@ -1115,6 +1117,17 @@ class DropdownButton<T> extends StatefulWidget {
/// instead. /// instead.
final Color? dropdownColor; final Color? dropdownColor;
/// Padding around the visible portion of the dropdown widget.
///
/// As the padding increases, the size of the [DropdownButton] will also
/// increase. The padding is included in the clickable area of the dropdown
/// widget, so this can make the widget easier to click.
///
/// Padding can be useful when used with a custom border. The clickable
/// area will stay flush with the border, as opposed to an external [Padding]
/// widget which will leave a non-clickable gap.
final EdgeInsetsGeometry? padding;
/// The maximum height of the menu. /// The maximum height of the menu.
/// ///
/// The maximum height of the menu must be at least one row shorter than /// The maximum height of the menu must be at least one row shorter than
...@@ -1505,7 +1518,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi ...@@ -1505,7 +1518,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
autofocus: widget.autofocus, autofocus: widget.autofocus,
focusColor: widget.focusColor ?? Theme.of(context).focusColor, focusColor: widget.focusColor ?? Theme.of(context).focusColor,
enableFeedback: false, enableFeedback: false,
child: result, child: widget.padding == null ? result : Padding(padding: widget.padding!, child: result),
), ),
), ),
); );
...@@ -1566,6 +1579,7 @@ class DropdownButtonFormField<T> extends FormField<T> { ...@@ -1566,6 +1579,7 @@ class DropdownButtonFormField<T> extends FormField<T> {
bool? enableFeedback, bool? enableFeedback,
AlignmentGeometry alignment = AlignmentDirectional.centerStart, AlignmentGeometry alignment = AlignmentDirectional.centerStart,
BorderRadius? borderRadius, BorderRadius? borderRadius,
EdgeInsetsGeometry? padding,
// When adding new arguments, consider adding similar arguments to // When adding new arguments, consider adding similar arguments to
// DropdownButton. // DropdownButton.
}) : assert(items == null || items.isEmpty || value == null || }) : assert(items == null || items.isEmpty || value == null ||
...@@ -1635,6 +1649,7 @@ class DropdownButtonFormField<T> extends FormField<T> { ...@@ -1635,6 +1649,7 @@ class DropdownButtonFormField<T> extends FormField<T> {
inputDecoration: effectiveDecoration.copyWith(errorText: field.errorText), inputDecoration: effectiveDecoration.copyWith(errorText: field.errorText),
isEmpty: isEmpty, isEmpty: isEmpty,
isFocused: Focus.of(context).hasFocus, isFocused: Focus.of(context).hasFocus,
padding: padding,
), ),
); );
}), }),
......
...@@ -67,6 +67,7 @@ Widget buildDropdown({ ...@@ -67,6 +67,7 @@ Widget buildDropdown({
Color? focusColor, Color? focusColor,
Color? dropdownColor, Color? dropdownColor,
double? menuMaxHeight, double? menuMaxHeight,
EdgeInsetsGeometry? padding,
}) { }) {
final List<DropdownMenuItem<String>>? listItems = items?.map<DropdownMenuItem<String>>((String item) { final List<DropdownMenuItem<String>>? listItems = items?.map<DropdownMenuItem<String>>((String item) {
return DropdownMenuItem<String>( return DropdownMenuItem<String>(
...@@ -101,6 +102,7 @@ Widget buildDropdown({ ...@@ -101,6 +102,7 @@ Widget buildDropdown({
itemHeight: itemHeight, itemHeight: itemHeight,
alignment: alignment, alignment: alignment,
menuMaxHeight: menuMaxHeight, menuMaxHeight: menuMaxHeight,
padding: padding,
), ),
); );
} }
...@@ -127,6 +129,7 @@ Widget buildDropdown({ ...@@ -127,6 +129,7 @@ Widget buildDropdown({
itemHeight: itemHeight, itemHeight: itemHeight,
alignment: alignment, alignment: alignment,
menuMaxHeight: menuMaxHeight, menuMaxHeight: menuMaxHeight,
padding: padding,
); );
} }
...@@ -156,6 +159,7 @@ Widget buildFrame({ ...@@ -156,6 +159,7 @@ Widget buildFrame({
Color? dropdownColor, Color? dropdownColor,
bool isFormField = false, bool isFormField = false,
double? menuMaxHeight, double? menuMaxHeight,
EdgeInsetsGeometry? padding,
Alignment dropdownAlignment = Alignment.center, Alignment dropdownAlignment = Alignment.center,
}) { }) {
return TestApp( return TestApp(
...@@ -189,6 +193,7 @@ Widget buildFrame({ ...@@ -189,6 +193,7 @@ Widget buildFrame({
itemHeight: itemHeight, itemHeight: itemHeight,
alignment: alignment, alignment: alignment,
menuMaxHeight: menuMaxHeight, menuMaxHeight: menuMaxHeight,
padding: padding,
), ),
), ),
), ),
...@@ -3933,4 +3938,28 @@ void main() { ...@@ -3933,4 +3938,28 @@ void main() {
final RenderClipRRect renderClip = tester.allRenderObjects.whereType<RenderClipRRect>().first; final RenderClipRRect renderClip = tester.allRenderObjects.whereType<RenderClipRRect>().first;
expect(renderClip.borderRadius, BorderRadius.circular(radius)); expect(renderClip.borderRadius, BorderRadius.circular(radius));
}); });
testWidgets('Size of DropdownButton with padding', (WidgetTester tester) async {
const double padVertical = 5;
const double padHorizontal = 10;
final Key buttonKey = UniqueKey();
EdgeInsets? padding;
Widget build() => buildFrame(buttonKey: buttonKey, onChanged: onChanged, padding: padding);
await tester.pumpWidget(build());
final RenderBox buttonBoxNoPadding = tester.renderObject<RenderBox>(find.byKey(buttonKey));
assert(buttonBoxNoPadding.attached);
final Size noPaddingSize = Size.copy(buttonBoxNoPadding.size);
padding = const EdgeInsets.symmetric(vertical: padVertical, horizontal: padHorizontal);
await tester.pumpWidget(build());
final RenderBox buttonBoxPadded = tester.renderObject<RenderBox>(find.byKey(buttonKey));
assert(buttonBoxPadded.attached);
final Size paddedSize = Size.copy(buttonBoxPadded.size);
// dropdowns with padding should be that much larger than with no padding
expect(noPaddingSize.height, equals(paddedSize.height - padVertical * 2));
expect(noPaddingSize.width, equals(paddedSize.width - padHorizontal * 2));
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment