diff --git a/packages/flutter/lib/src/cupertino/segmented_control.dart b/packages/flutter/lib/src/cupertino/segmented_control.dart index 3506c0ad50..82cb6dbd9b 100644 --- a/packages/flutter/lib/src/cupertino/segmented_control.dart +++ b/packages/flutter/lib/src/cupertino/segmented_control.dart @@ -382,6 +382,7 @@ class _SegmentedControlState extends State> ); child = GestureDetector( + behavior: HitTestBehavior.opaque, onTapDown: (TapDownDetails event) { _onTapDown(currentKey); }, diff --git a/packages/flutter/test/cupertino/segmented_control_test.dart b/packages/flutter/test/cupertino/segmented_control_test.dart index 86384a1816..bdf1927a57 100644 --- a/packages/flutter/test/cupertino/segmented_control_test.dart +++ b/packages/flutter/test/cupertino/segmented_control_test.dart @@ -926,6 +926,44 @@ void main() { expect(sharedValue, 0); }); + testWidgets( + 'Segment still hittable with a child that has no hitbox', + (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/57326. + final Map children = {}; + children[0] = const Text('Child 1'); + children[1] = const SizedBox(); + int sharedValue = 0; + + await tester.pumpWidget( + StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return boilerplate( + child: CupertinoSegmentedControl( + key: const ValueKey('Segmented Control'), + children: children, + onValueChanged: (int newValue) { + setState(() { + sharedValue = newValue; + }); + }, + groupValue: sharedValue, + ), + ); + }, + ), + ); + + expect(sharedValue, 0); + + final Offset centerOfTwo = tester.getCenter(find.byWidget(children[1])); + // Tap within the bounds of children[1], but not at the center. + // children[1] is a SizedBox thus not hittable by itself. + await tester.tapAt(centerOfTwo + const Offset(10, 0)); + + expect(sharedValue, 1); + }); + testWidgets('Animation is correct when the selected segment changes', (WidgetTester tester) async { await tester.pumpWidget(setupSimpleSegmentedControl()); diff --git a/packages/flutter/test/cupertino/sliding_segmented_control_test.dart b/packages/flutter/test/cupertino/sliding_segmented_control_test.dart index bd92bd244c..1c13302162 100644 --- a/packages/flutter/test/cupertino/sliding_segmented_control_test.dart +++ b/packages/flutter/test/cupertino/sliding_segmented_control_test.dart @@ -776,7 +776,8 @@ void main() { expect(groupValue, 0); final Offset centerOfTwo = tester.getCenter(find.byWidget(children[1])); - // Tap just inside segment bounds + // Tap within the bounds of children[1], but not at the center. + // children[1] is a SizedBox thus not hittable by itself. await tester.tapAt(centerOfTwo + const Offset(10, 0)); expect(groupValue, 1);