From 4d1da9afa5f60b6e85cc21557c9af523552e313a Mon Sep 17 00:00:00 2001 From: Chinmoy Date: Wed, 23 Jun 2021 02:45:54 +0530 Subject: [PATCH] Adds borderRadius property to DropdownButton (#84298) --- .../flutter/lib/src/material/dropdown.dart | 38 ++++++++- .../flutter/test/material/dropdown_test.dart | 77 +++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 862e654374..edf34fb4d8 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -43,6 +43,7 @@ class _DropdownMenuPainter extends CustomPainter { this.color, this.elevation, this.selectedIndex, + this.borderRadius, required this.resize, required this.getSelectedItemOffset, }) : _painter = BoxDecoration( @@ -50,7 +51,7 @@ class _DropdownMenuPainter extends CustomPainter { // configuration in the paint() function and you must provide some sort // of onChanged callback here. color: color, - borderRadius: BorderRadius.circular(2.0), + borderRadius: borderRadius ?? const BorderRadius.all(Radius.circular(2.0)), boxShadow: kElevationToShadow[elevation], ).createBoxPainter(), super(repaint: resize); @@ -58,6 +59,7 @@ class _DropdownMenuPainter extends CustomPainter { final Color? color; final int? elevation; final int? selectedIndex; + final BorderRadius? borderRadius; final Animation resize; final ValueGetter getSelectedItemOffset; final BoxPainter _painter; @@ -85,6 +87,7 @@ class _DropdownMenuPainter extends CustomPainter { return oldPainter.color != color || oldPainter.elevation != elevation || oldPainter.selectedIndex != selectedIndex + || oldPainter.borderRadius != borderRadius || oldPainter.resize != resize; } } @@ -94,6 +97,7 @@ class _DropdownMenuItemButton extends StatefulWidget { const _DropdownMenuItemButton({ Key? key, this.padding, + required this.borderRadius, required this.route, required this.buttonRect, required this.constraints, @@ -107,6 +111,7 @@ class _DropdownMenuItemButton extends StatefulWidget { final BoxConstraints constraints; final int itemIndex; final bool enableFeedback; + final BorderRadius borderRadius; @override _DropdownMenuItemButtonState createState() => _DropdownMenuItemButtonState(); @@ -172,6 +177,16 @@ class _DropdownMenuItemButtonState extends State<_DropdownMenuItemButton> padding: widget.padding, child: widget.route.items[widget.itemIndex], ); + final BorderRadius itemBorderRadius; + if (widget.route.items.length == 1) { + itemBorderRadius = widget.borderRadius; + } else if (widget.itemIndex == 0) { + itemBorderRadius = BorderRadius.only(topLeft: widget.borderRadius.topLeft, topRight: widget.borderRadius.topRight); + } else if (widget.itemIndex == widget.route.items.length - 1) { + itemBorderRadius = BorderRadius.only(bottomLeft: widget.borderRadius.bottomLeft, bottomRight: widget.borderRadius.bottomRight); + } else { + itemBorderRadius = BorderRadius.zero; + } // An [InkWell] is added to the item only if it is enabled if (dropdownMenuItem.enabled) { child = InkWell( @@ -180,6 +195,7 @@ class _DropdownMenuItemButtonState extends State<_DropdownMenuItemButton> onTap: _handleOnTap, onFocusChange: _handleFocusChange, child: child, + borderRadius: itemBorderRadius, ); } child = FadeTransition(opacity: opacity, child: child); @@ -202,6 +218,7 @@ class _DropdownMenu extends StatefulWidget { required this.constraints, this.dropdownColor, required this.enableFeedback, + this.borderRadius, }) : super(key: key); final _DropdownRoute route; @@ -210,6 +227,7 @@ class _DropdownMenu extends StatefulWidget { final BoxConstraints constraints; final Color? dropdownColor; final bool enableFeedback; + final BorderRadius? borderRadius; @override _DropdownMenuState createState() => _DropdownMenuState(); @@ -260,6 +278,7 @@ class _DropdownMenuState extends State<_DropdownMenu> { constraints: widget.constraints, itemIndex: itemIndex, enableFeedback: widget.enableFeedback, + borderRadius: widget.borderRadius ?? BorderRadius.zero, ), ]; @@ -271,6 +290,7 @@ class _DropdownMenuState extends State<_DropdownMenu> { elevation: route.elevation, selectedIndex: route.selectedIndex, resize: _resize, + borderRadius: widget.borderRadius, // This offset is passed as a callback, not a value, because it must // be retrieved at paint time (after layout), not at build time. getSelectedItemOffset: () => route.getItemOffset(route.selectedIndex), @@ -420,6 +440,7 @@ class _DropdownRoute extends PopupRoute<_DropdownRouteResult> { this.dropdownColor, this.menuMaxHeight, required this.enableFeedback, + this.borderRadius, }) : assert(style != null), itemHeights = List.filled(items.length, itemHeight ?? kMinInteractiveDimension); @@ -434,6 +455,7 @@ class _DropdownRoute extends PopupRoute<_DropdownRouteResult> { final Color? dropdownColor; final double? menuMaxHeight; final bool enableFeedback; + final BorderRadius? borderRadius; final List itemHeights; ScrollController? scrollController; @@ -466,6 +488,7 @@ class _DropdownRoute extends PopupRoute<_DropdownRouteResult> { style: style, dropdownColor: dropdownColor, enableFeedback: enableFeedback, + borderRadius: borderRadius, ); }, ); @@ -562,6 +585,7 @@ class _DropdownRoutePage extends StatelessWidget { this.style, required this.dropdownColor, required this.enableFeedback, + this.borderRadius, }) : super(key: key); final _DropdownRoute route; @@ -575,6 +599,7 @@ class _DropdownRoutePage extends StatelessWidget { final TextStyle? style; final Color? dropdownColor; final bool enableFeedback; + final BorderRadius? borderRadius; @override Widget build(BuildContext context) { @@ -599,6 +624,7 @@ class _DropdownRoutePage extends StatelessWidget { constraints: constraints, dropdownColor: dropdownColor, enableFeedback: enableFeedback, + borderRadius: borderRadius, ); return MediaQuery.removePadding( @@ -883,6 +909,7 @@ class DropdownButton extends StatefulWidget { this.menuMaxHeight, this.enableFeedback, this.alignment = AlignmentDirectional.centerStart, + this.borderRadius, // When adding new arguments, consider adding similar arguments to // DropdownButtonFormField. }) : assert(items == null || items.isEmpty || value == null || @@ -1167,6 +1194,14 @@ class DropdownButton extends StatefulWidget { /// relative to text direction. final AlignmentGeometry alignment; + /// Defines the corner radii of the menu's rounded rectangle shape. + /// + /// The radii of the first menu item's top left and right corners are + /// defined by the corresponding properties of the [borderRadius]. + /// Similarly, the radii of the last menu item's bottom and right corners + /// are defined by the corresponding properties of the [borderRadius]. + final BorderRadius? borderRadius; + @override State> createState() => _DropdownButtonState(); } @@ -1320,6 +1355,7 @@ class _DropdownButtonState extends State> with WidgetsBindi dropdownColor: widget.dropdownColor, menuMaxHeight: widget.menuMaxHeight, enableFeedback: widget.enableFeedback ?? true, + borderRadius: widget.borderRadius, ); navigator.push(_dropdownRoute!).then((_DropdownRouteResult? newValue) { diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index 76b3f9671d..45a6254cae 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -3472,4 +3472,81 @@ void main() { expect(find.byType(RawScrollbar), findsNothing); }, variant: TargetPlatformVariant.all()); + + testWidgets('borderRadius property works properly', (WidgetTester tester) async { + const double radius = 20.0; + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: DropdownButton( + borderRadius: BorderRadius.circular(radius), + value: 'One', + items: ['One', 'Two', 'Three', 'Four'] + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(), + onChanged: (_) { }, + ), + ), + ), + ), + ); + + await tester.tap(find.text('One')); + await tester.pumpAndSettle(); + + expect( + find.ancestor( + of: find.text('One').last, + matching: find.byType(CustomPaint), + ).at(2), + paints + ..save() + ..rrect() + ..rrect() + ..rrect() + ..rrect(rrect: const RRect.fromLTRBXY(0.0, 0.0, 144.0, 208.0, radius, radius)), + ); + + final InkWell firstItem = tester.widget(find.widgetWithText(InkWell, 'One')); + final InkWell lastItem = tester.widget(find.widgetWithText(InkWell, 'Four')); + + expect(firstItem.borderRadius, const BorderRadius.vertical(top: Radius.circular(radius))); + expect(lastItem.borderRadius, const BorderRadius.vertical(bottom: Radius.circular(radius))); + }); + + testWidgets('borderRadius is properly applied to InkWell when there is only one item', (WidgetTester tester) async { + const BorderRadius borderRadius = BorderRadius.all(Radius.circular(5.0)); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: DropdownButton( + borderRadius: borderRadius, + value: 'One', + items: const >[ + DropdownMenuItem( + child: Text('One'), value: 'One' + ), + ], + onChanged: (_) { }, + ), + ), + ), + ), + ); + + await tester.tap(find.text('One')); + await tester.pumpAndSettle(); + + final InkWell menuItem = tester.widget(find.widgetWithText(InkWell, 'One')); + + expect(menuItem.borderRadius, borderRadius); + }); }