Skip to content

Support BorderRadiusDirectional for dropdown menu #369

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

Merged
merged 2 commits into from
Apr 3, 2025
Merged
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
2 changes: 2 additions & 0 deletions packages/dropdown_button2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
- Add barrierCoversButton to DropdownButtonFormField2.
- Respect button's borderRadius when barrierCoversButton is false.
- Respect inputDecoration's borderRadius when barrierCoversButton is false.
- Support BorderRadiusDirectional for dropdown menu.
- Fix barrier when using TextDirection.rtl while barrierCoversButton set to false.

## 3.0.0-beta.21

Expand Down
2 changes: 2 additions & 0 deletions packages/dropdown_button2/lib/src/dropdown_button2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>> with WidgetsBin
void _handleTap() {
final NavigatorState navigator = Navigator.of(context,
rootNavigator: _dropdownStyle.isFullScreen ?? _dropdownStyle.useRootNavigator);
final TextDirection? textDirection = Directionality.maybeOf(context);

final items = widget.items!;
final separator = widget.dropdownSeparator;
Expand All @@ -646,6 +647,7 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>> with WidgetsBin
barrierCoversButton: widget.barrierCoversButton,
parentFocusNode: _focusNode,
enableFeedback: widget.enableFeedback ?? true,
textDirection: textDirection,
dropdownStyle: _dropdownStyle,
menuItemStyle: _menuItemStyle,
inputDecorationPadding: _getInputDecorationPadding(),
Expand Down
20 changes: 14 additions & 6 deletions packages/dropdown_button2/lib/src/dropdown_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
resize: _resize,
itemHeight: items[0].height,
dropdownDecoration: dropdownStyle.decoration,
textDirection: widget.textDirection,
),
child: Semantics(
scopesRoute: true,
Expand All @@ -263,12 +264,13 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {

class _DropdownMenuPainter extends CustomPainter {
_DropdownMenuPainter({
this.color,
this.elevation,
this.selectedIndex,
required this.color,
required this.elevation,
required this.selectedIndex,
required this.resize,
required this.itemHeight,
this.dropdownDecoration,
required this.dropdownDecoration,
required this.textDirection,
}) : _painter = dropdownDecoration
?.copyWith(
color: dropdownDecoration.color ?? color,
Expand All @@ -291,6 +293,7 @@ class _DropdownMenuPainter extends CustomPainter {
final Animation<double> resize;
final double itemHeight;
final BoxDecoration? dropdownDecoration;
final TextDirection? textDirection;

final BoxPainter _painter;

Expand All @@ -310,16 +313,21 @@ class _DropdownMenuPainter extends CustomPainter {

final Rect rect = Rect.fromLTRB(0.0, top.evaluate(resize), size.width, bottom.evaluate(resize));

_painter.paint(canvas, rect.topLeft, ImageConfiguration(size: rect.size));
_painter.paint(
canvas,
rect.topLeft,
ImageConfiguration(size: rect.size, textDirection: textDirection),
);
}

@override
bool shouldRepaint(_DropdownMenuPainter oldPainter) {
return oldPainter.color != color ||
oldPainter.elevation != elevation ||
oldPainter.selectedIndex != selectedIndex ||
oldPainter.dropdownDecoration != dropdownDecoration ||
oldPainter.itemHeight != itemHeight ||
oldPainter.dropdownDecoration != dropdownDecoration ||
oldPainter.textDirection != textDirection ||
oldPainter.resize != resize;
}
}
123 changes: 63 additions & 60 deletions packages/dropdown_button2/lib/src/dropdown_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
required this.barrierCoversButton,
required this.parentFocusNode,
required this.enableFeedback,
required this.textDirection,
required this.dropdownStyle,
required this.menuItemStyle,
required this.inputDecorationPadding,
Expand All @@ -34,6 +35,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
final TextStyle style;
final FocusNode parentFocusNode;
final bool enableFeedback;
final TextDirection? textDirection;
final DropdownStyleData dropdownStyle;
final MenuItemStyleData menuItemStyle;
final EdgeInsets? inputDecorationPadding;
Expand Down Expand Up @@ -61,46 +63,49 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {

@override
Widget buildPage(BuildContext context, _, __) {
return FocusScope.withExternalFocusNode(
focusScopeNode: _childNode,
parentNode: parentFocusNode,
child: LayoutBuilder(
builder: (BuildContext ctx, BoxConstraints constraints) {
//Exclude BottomInset from maxHeight to avoid overlapping menu items
//with keyboard when using searchable dropdown.
//This will ensure menu is drawn in the actual available height.
final padding = MediaQuery.paddingOf(context);
final viewInsets = MediaQuery.viewInsetsOf(context);
final BoxConstraints actualConstraints =
constraints.copyWith(maxHeight: constraints.maxHeight - viewInsets.bottom);
final EdgeInsets mediaQueryPadding =
dropdownStyle.useSafeArea ? padding : EdgeInsets.zero;
return ValueListenableBuilder<Rect?>(
valueListenable: buttonRect,
builder: (BuildContext context, Rect? rect, _) {
final routePage = _DropdownRoutePage<T>(
route: this,
constraints: actualConstraints,
mediaQueryPadding: mediaQueryPadding,
buttonRect: rect!,
selectedIndex: selectedIndex,
capturedThemes: capturedThemes,
style: style,
enableFeedback: enableFeedback,
);
return barrierCoversButton
? routePage
: _CustomModalBarrier(
barrierColor: _altBarrierColor,
animation: animation,
barrierCurve: barrierCurve,
buttonRect: rect,
buttonBorderRadius: buttonBorderRadius ?? BorderRadius.zero,
child: routePage,
);
},
);
},
return Directionality(
textDirection: textDirection ?? Directionality.of(context),
child: FocusScope.withExternalFocusNode(
focusScopeNode: _childNode,
parentNode: parentFocusNode,
child: LayoutBuilder(
builder: (BuildContext ctx, BoxConstraints constraints) {
//Exclude BottomInset from maxHeight to avoid overlapping menu items
//with keyboard when using searchable dropdown.
//This will ensure menu is drawn in the actual available height.
final padding = MediaQuery.paddingOf(context);
final viewInsets = MediaQuery.viewInsetsOf(context);
final BoxConstraints actualConstraints =
constraints.copyWith(maxHeight: constraints.maxHeight - viewInsets.bottom);
final EdgeInsets mediaQueryPadding =
dropdownStyle.useSafeArea ? padding : EdgeInsets.zero;
return ValueListenableBuilder<Rect?>(
valueListenable: buttonRect,
builder: (BuildContext context, Rect? rect, _) {
final routePage = _DropdownRoutePage<T>(
route: this,
constraints: actualConstraints,
mediaQueryPadding: mediaQueryPadding,
buttonRect: rect!,
selectedIndex: selectedIndex,
capturedThemes: capturedThemes,
style: style,
enableFeedback: enableFeedback,
);
return barrierCoversButton
? routePage
: _CustomModalBarrier(
barrierColor: _altBarrierColor,
animation: animation,
barrierCurve: barrierCurve,
buttonRect: rect,
buttonBorderRadius: buttonBorderRadius ?? BorderRadius.zero,
child: routePage,
);
},
);
},
),
),
);
}
Expand Down Expand Up @@ -503,18 +508,21 @@ class _CustomModalBarrierState extends State<_CustomModalBarrier> {

return Stack(
children: [
ValueListenableBuilder(
valueListenable: color,
builder: (BuildContext context, Color? value, Widget? child) {
return CustomPaint(
painter: _DropdownBarrierPainter(
barrierColor: value,
buttonRect: widget.buttonRect,
buttonBorderRadius: widget.buttonBorderRadius,
pageSize: size,
),
);
},
IgnorePointer(
child: ValueListenableBuilder(
valueListenable: color,
builder: (BuildContext context, Color? value, Widget? child) {
return CustomPaint(
size: Size(size.width, size.height),
painter: _DropdownBarrierPainter(
barrierColor: value,
buttonRect: widget.buttonRect,
buttonBorderRadius: widget.buttonBorderRadius,
pageSize: size,
),
);
},
),
),
widget.child,
],
Expand All @@ -538,15 +546,10 @@ class _DropdownBarrierPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
if (barrierColor != null) {
final Rect rect = Rect.fromLTRB(
-buttonRect.left,
-buttonRect.top,
pageSize.width,
pageSize.height,
);
final Rect pageRect = Offset.zero & pageSize;

canvas.saveLayer(rect, Paint());
canvas.drawRect(rect, Paint()..color = barrierColor!);
canvas.saveLayer(pageRect, Paint());
canvas.drawRect(pageRect, Paint()..color = barrierColor!);

final RRect buttonRRect = RRect.fromRectAndCorners(
buttonRect,
Expand Down
Loading