Skip to content

Commit 72f1525

Browse files
committed
Respect inputDecoration's borderRadius when barrierCoversButton is false
1 parent 34e2650 commit 72f1525

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

packages/dropdown_button2/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Fix DropdownButtonFormField padding when ButtonTheme.alignedDropdown is true [Flutter core].
1717
- Add barrierCoversButton to DropdownButtonFormField2.
1818
- Respect button's borderRadius when barrierCoversButton is false.
19+
- Respect inputDecoration's borderRadius when barrierCoversButton is false.
1920

2021
## 3.0.0-beta.21
2122

packages/dropdown_button2/lib/src/dropdown_button2.dart

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ class DropdownButton2<T> extends StatefulWidget {
127127
'Only one of valueListenable or multiValueListenable can be used.',
128128
),
129129
_inputDecoration = null,
130-
_isEmpty = false;
130+
_isEmpty = false,
131+
_hasError = false;
131132

132133
const DropdownButton2._formField({
133134
super.key,
@@ -161,9 +162,11 @@ class DropdownButton2<T> extends StatefulWidget {
161162
required this.openDropdownListenable,
162163
required InputDecoration inputDecoration,
163164
required bool isEmpty,
165+
required bool hasError,
164166
}) : underline = null,
165167
_inputDecoration = inputDecoration,
166-
_isEmpty = isEmpty;
168+
_isEmpty = isEmpty,
169+
_hasError = hasError;
167170

168171
/// The list of items the user can select.
169172
///
@@ -382,6 +385,8 @@ class DropdownButton2<T> extends StatefulWidget {
382385

383386
final bool _isEmpty;
384387

388+
final bool _hasError;
389+
385390
@override
386391
State<DropdownButton2<T>> createState() => _DropdownButton2State<T>();
387392
}
@@ -572,6 +577,30 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>> with WidgetsBin
572577
}
573578
}
574579

580+
Set<WidgetState> _materialState(InputDecoration decoration) => <WidgetState>{
581+
if (!decoration.enabled) WidgetState.disabled,
582+
if (_isFocused) WidgetState.focused,
583+
if (_isHovering) WidgetState.hovered,
584+
if (widget._hasError) WidgetState.error,
585+
};
586+
587+
BorderRadius? _getInputDecorationBorderRadius(InputDecoration decoration) {
588+
InputBorder? border;
589+
if (!decoration.enabled) {
590+
border = widget._hasError ? decoration.errorBorder : decoration.disabledBorder;
591+
} else if (_isFocused) {
592+
border = widget._hasError ? decoration.focusedErrorBorder : decoration.focusedBorder;
593+
} else {
594+
border = widget._hasError ? decoration.errorBorder : decoration.enabledBorder;
595+
}
596+
border ??= WidgetStateProperty.resolveAs(decoration.border, _materialState(decoration));
597+
598+
if (border is OutlineInputBorder) {
599+
return border.borderRadius;
600+
}
601+
return null;
602+
}
603+
575604
EdgeInsetsGeometry _buttonAdditionalHPadding() {
576605
final TextDirection? textDirection = Directionality.maybeOf(context);
577606

@@ -602,7 +631,9 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>> with WidgetsBin
602631
_dropdownRoute = _DropdownRoute<T>(
603632
items: items,
604633
buttonRect: _buttonRect,
605-
buttonBorderRadius: _getButtonBorderRadius(context) ?? BorderRadius.zero,
634+
buttonBorderRadius: widget._inputDecoration != null
635+
? _getInputDecorationBorderRadius(widget._inputDecoration!)
636+
: _getButtonBorderRadius(context),
606637
selectedIndex: _selectedIndex ?? 0,
607638
isNoSelectedItem: _selectedIndex == null,
608639
onChanged: widget.onChanged,
@@ -1027,6 +1058,10 @@ class DropdownButtonFormField2<T> extends FormField<T> {
10271058
: effectiveHint != null || effectiveDisabledHint != null;
10281059
final bool isEmpty = !showSelectedItem && !isHintOrDisabledHintAvailable;
10291060

1061+
final bool hasError = field.hasError ||
1062+
effectiveDecoration.errorText != null ||
1063+
effectiveDecoration.error != null;
1064+
10301065
// An unFocusable Focus widget so that this widget can detect if its
10311066
// descendants have focus or not.
10321067
return Focus(
@@ -1068,6 +1103,7 @@ class DropdownButtonFormField2<T> extends FormField<T> {
10681103
hintText: effectiveDecoration.hintText != null ? '' : null,
10691104
),
10701105
isEmpty: isEmpty,
1106+
hasError: hasError,
10711107
),
10721108
),
10731109
);

packages/dropdown_button2/lib/src/dropdown_route.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
2626

2727
final List<DropdownItem<T>> items;
2828
final ValueNotifier<Rect?> buttonRect;
29-
final BorderRadius buttonBorderRadius;
29+
final BorderRadius? buttonBorderRadius;
3030
final int selectedIndex;
3131
final bool isNoSelectedItem;
3232
final ValueChanged<T?>? onChanged;
@@ -95,7 +95,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
9595
animation: animation,
9696
barrierCurve: barrierCurve,
9797
buttonRect: rect,
98-
buttonBorderRadius: buttonBorderRadius,
98+
buttonBorderRadius: buttonBorderRadius ?? BorderRadius.zero,
9999
child: routePage,
100100
);
101101
},
@@ -563,8 +563,8 @@ class _DropdownBarrierPainter extends CustomPainter {
563563

564564
@override
565565
bool shouldRepaint(_DropdownBarrierPainter oldPainter) {
566-
return oldPainter.barrierColor != barrierColor ||
567-
oldPainter.buttonRect != buttonRect ||
566+
return oldPainter.buttonRect != buttonRect ||
567+
oldPainter.barrierColor != barrierColor ||
568568
oldPainter.buttonBorderRadius != buttonBorderRadius;
569569
}
570570
}

0 commit comments

Comments
 (0)