From 60d2735e1a2a14a5a969d709af7571d972d88460 Mon Sep 17 00:00:00 2001 From: Van Looveren Koen Date: Sun, 16 Jun 2019 23:58:04 +0200 Subject: [PATCH] Added customizable padding for the segmented controll (#34555) --- .../lib/src/cupertino/segmented_control.dart | 12 ++- .../cupertino/segmented_control_test.dart | 87 +++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/segmented_control.dart b/packages/flutter/lib/src/cupertino/segmented_control.dart index 4069004897..14fba1dd62 100644 --- a/packages/flutter/lib/src/cupertino/segmented_control.dart +++ b/packages/flutter/lib/src/cupertino/segmented_control.dart @@ -11,9 +11,9 @@ import 'package:flutter/widgets.dart'; import 'theme.dart'; -// Minimum padding from horizontal edges of segmented control to edges of +// Minimum padding from edges of the segmented control to edges of // encompassing widget. -const EdgeInsets _kHorizontalItemPadding = EdgeInsets.symmetric(horizontal: 16.0); +const EdgeInsetsGeometry _kHorizontalItemPadding = EdgeInsets.symmetric(horizontal: 16.0); // Minimum height of the segmented control. const double _kMinSegmentedControlHeight = 28.0; @@ -87,6 +87,7 @@ class CupertinoSegmentedControl extends StatefulWidget { this.selectedColor, this.borderColor, this.pressedColor, + this.padding, }) : assert(children != null), assert(children.length >= 2), assert(onValueChanged != null), @@ -179,6 +180,11 @@ class CupertinoSegmentedControl extends StatefulWidget { /// Defaults to the selectedColor at 20% opacity if null. final Color pressedColor; + /// The CupertinoSegmentedControl will be placed inside this padding + /// + /// Defaults to EdgeInsets.symmetric(horizontal: 16.0) + final EdgeInsetsGeometry padding; + @override _SegmentedControlState createState() => _SegmentedControlState(); } @@ -407,7 +413,7 @@ class _SegmentedControlState extends State> ); return Padding( - padding: _kHorizontalItemPadding.resolve(Directionality.of(context)), + padding: widget.padding ?? _kHorizontalItemPadding, child: UnconstrainedBox( constrainedAxis: Axis.horizontal, child: box, diff --git a/packages/flutter/test/cupertino/segmented_control_test.dart b/packages/flutter/test/cupertino/segmented_control_test.dart index 9ee56899a5..09bb53ef79 100644 --- a/packages/flutter/test/cupertino/segmented_control_test.dart +++ b/packages/flutter/test/cupertino/segmented_control_test.dart @@ -117,6 +117,93 @@ void main() { } }); + testWidgets('Padding works', (WidgetTester tester) async { + const Key key = Key('Container'); + + final Map children = {}; + children[0] = const SizedBox( + height: double.infinity, + child: Text('Child 1'), + ) ; + children[1] = const SizedBox( + height: double.infinity, + child: Text('Child 2'), + ) ; + + Future verifyPadding({ EdgeInsets padding }) async { + final EdgeInsets effectivePadding = padding ?? const EdgeInsets.symmetric(horizontal: 16); + final Rect segmentedControlRect = tester.getRect(find.byKey(key)); + expect( + tester.getTopLeft(find.byWidget(children[0])), + segmentedControlRect.topLeft.translate( + effectivePadding.topLeft.dx, + effectivePadding.topLeft.dy, + ) + ); + expect( + tester.getBottomLeft(find.byWidget(children[0])), + segmentedControlRect.bottomLeft.translate( + effectivePadding.bottomLeft.dx, + effectivePadding.bottomLeft.dy, + ), + ); + + expect( + tester.getTopRight(find.byWidget(children[1])), + segmentedControlRect.topRight.translate( + effectivePadding.topRight.dx, + effectivePadding.topRight.dy, + ), + ); + expect( + tester.getBottomRight(find.byWidget(children[1])), + segmentedControlRect.bottomRight.translate( + effectivePadding.bottomRight.dx, + effectivePadding.bottomRight.dy, + ), + ); + } + + await tester.pumpWidget( + boilerplate( + child: CupertinoSegmentedControl( + key: key, + children: children, + onValueChanged: (int newValue) { }, + ), + ) + ); + + // Default padding works. + await verifyPadding(); + + // Switch to Child 2 padding should remain the same. + await tester.tap(find.text('Child 2')); + await tester.pumpAndSettle(); + + await verifyPadding(); + + await tester.pumpWidget( + boilerplate( + child: CupertinoSegmentedControl( + key: key, + padding: const EdgeInsets.fromLTRB(1, 3, 5, 7), + children: children, + onValueChanged: (int newValue) { }, + ), + ) + ); + + // Custom padding works. + await verifyPadding(padding: const EdgeInsets.fromLTRB(1, 3, 5, 7)); + + // Switch back to Child 1 padding should remain the same. + await tester.tap(find.text('Child 1')); + await tester.pumpAndSettle(); + + await verifyPadding(padding: const EdgeInsets.fromLTRB(1, 3, 5, 7)); + }); + testWidgets('Value attribute must be the key of one of the children widgets', (WidgetTester tester) async { final Map children = {}; children[0] = const Text('Child 1');