Added customizable padding for the segmented controll (#34555)
diff --git a/packages/flutter/lib/src/cupertino/segmented_control.dart b/packages/flutter/lib/src/cupertino/segmented_control.dart
index 4069004..14fba1d 100644
--- a/packages/flutter/lib/src/cupertino/segmented_control.dart
+++ b/packages/flutter/lib/src/cupertino/segmented_control.dart
@@ -11,9 +11,9 @@
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 @@
this.selectedColor,
this.borderColor,
this.pressedColor,
+ this.padding,
}) : assert(children != null),
assert(children.length >= 2),
assert(onValueChanged != null),
@@ -179,6 +180,11 @@
/// 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<T> createState() => _SegmentedControlState<T>();
}
@@ -407,7 +413,7 @@
);
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 9ee5689..09bb53e 100644
--- a/packages/flutter/test/cupertino/segmented_control_test.dart
+++ b/packages/flutter/test/cupertino/segmented_control_test.dart
@@ -117,6 +117,93 @@
}
});
+ testWidgets('Padding works', (WidgetTester tester) async {
+ const Key key = Key('Container');
+
+ final Map<int, Widget> children = <int, Widget>{};
+ children[0] = const SizedBox(
+ height: double.infinity,
+ child: Text('Child 1'),
+ ) ;
+ children[1] = const SizedBox(
+ height: double.infinity,
+ child: Text('Child 2'),
+ ) ;
+
+ Future<void> 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<int>(
+ 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<int>(
+ 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<int, Widget> children = <int, Widget>{};
children[0] = const Text('Child 1');