diff --git a/packages/flutter/lib/src/cupertino/sliding_segmented_control.dart b/packages/flutter/lib/src/cupertino/sliding_segmented_control.dart index c871b819cb..7c314810d4 100644 --- a/packages/flutter/lib/src/cupertino/sliding_segmented_control.dart +++ b/packages/flutter/lib/src/cupertino/sliding_segmented_control.dart @@ -94,56 +94,12 @@ class _FontWeightTween extends Tween { /// argument must be an ordered [Map] such as a [LinkedHashMap], the ordering of /// the keys will determine the order of the widgets in the segmented control. /// -/// When the state of the segmented control changes, the widget changes the -/// [controller]'s value to the map key associated with the newly selected widget, -/// causing all of its listeners to be notified. -/// -/// {@tool dartpad --template=stateful_widget_material} -/// -/// This sample shows two [CupertinoSlidingSegmentedControl]s that mirror each other. -/// -/// ```dart -/// final Map children = const { -/// 0: Text('Child 1'), -/// 1: Text('Child 2'), -/// 2: Text('Child 3'), -/// }; -/// -/// // No segment is initially selected because the controller's value is null. -/// final ValueNotifier controller = ValueNotifier(null); -/// -/// @override -/// void initState() { -/// super.initState(); -/// // Prints a message whenever the currently selected widget changes. -/// controller.addListener(() { print('selected: ${controller.value}'); }); -/// } -/// -/// @override -/// Widget build(BuildContext context) { -/// return Center( -/// child: Column( -/// children: [ -/// CupertinoSlidingSegmentedControl( -/// children: children, -/// controller: controller, -/// ), -/// CupertinoSlidingSegmentedControl( -/// children: children, -/// controller: controller, -/// ), -/// ], -/// ), -/// ); -/// } -/// -/// @override -/// void dispose() { -/// controller.dispose(); -/// super.dispose(); -/// } -/// ``` -/// {@end-tool} +/// When the state of the segmented control changes, the widget calls the +/// [onValueChanged] callback. The map key associated with the newly selected +/// widget is returned in the [onValueChanged] callback. Typically, widgets +/// that use a segmented control will listen for the [onValueChanged] callback +/// and rebuild the segmented control with a new [groupValue] to update which +/// option is currently selected. /// /// The [children] will be displayed in the order of the keys in the [Map]. /// The height of the segmented control is determined by the height of the @@ -166,33 +122,34 @@ class _FontWeightTween extends Tween { class CupertinoSlidingSegmentedControl extends StatefulWidget { /// Creates an iOS-style segmented control bar. /// - /// The [children] and [controller] arguments must not be null. The [children] - /// argument must be an ordered [Map] such as a [LinkedHashMap]. Further, the - /// length of the [children] list must be greater than one. + /// The [children] and [onValueChanged] arguments must not be null. The + /// [children] argument must be an ordered [Map] such as a [LinkedHashMap]. + /// Further, the length of the [children] list must be greater than one. /// - /// Each widget value in the map of [children] must have an associated [Map] key - /// of type [T] that uniquely identifies this widget. This key will become the - /// [controller]'s new value, when the corresponding child widget from the - /// [children] map is selected. + /// Each widget value in the map of [children] must have an associated key + /// that uniquely identifies this widget. This key is what will be returned + /// in the [onValueChanged] callback when a new value from the [children] map + /// is selected. /// - /// The [controller]'s [ValueNotifier.value] is the currently selected value for - /// the segmented control. If it is null, no widget will appear as selected. The - /// [controller]'s value must be either null or one of the keys in the [children] - /// map. + /// The [groupValue] is the currently selected value for the segmented control. + /// If no [groupValue] is provided, or the [groupValue] is null, no widget will + /// appear as selected. The [groupValue] must be either null or one of the keys + /// in the [children] map. CupertinoSlidingSegmentedControl({ Key key, @required this.children, - @required this.controller, + @required this.onValueChanged, + this.groupValue, this.thumbColor = _kThumbColor, this.padding = _kHorizontalItemPadding, this.backgroundColor = CupertinoColors.tertiarySystemFill, }) : assert(children != null), assert(children.length >= 2), assert(padding != null), - assert(controller != null), + assert(onValueChanged != null), assert( - controller.value == null || children.keys.any((T child) => child == controller.value), - "The controller's value must be either null or one of the keys in the children map.", + groupValue == null || children.keys.contains(groupValue), + 'The groupValue must be either null or one of the keys in the children map.', ), super(key: key); @@ -203,16 +160,58 @@ class CupertinoSlidingSegmentedControl extends StatefulWidget { /// This attribute must be an ordered [Map] such as a [LinkedHashMap]. final Map children; - /// A [ValueNotifier]<[T]> that controls the currently selected child. + /// The identifier of the widget that is currently selected. /// - /// Its value must be one of the keys in the [Map] of [children], or null, in - /// which case no widget will be selected. + /// This must be one of the keys in the [Map] of [children]. + /// If this attribute is null, no widget will be initially selected. + final T groupValue; + + /// The callback that is called when a new option is tapped. /// - /// The [controller]'s value changes when the user drags the thumb to a different - /// child widget, or taps on a different child widget. Its value can also be - /// changed programmatically, in which case all sliding animations will play as - /// if the new selected child widget was tapped on. - final ValueNotifier controller; + /// This attribute must not be null. + /// + /// The segmented control passes the newly selected widget's associated key + /// to the callback but does not actually change state until the parent + /// widget rebuilds the segmented control with the new [groupValue]. + /// + /// The callback provided to [onValueChanged] should update the state of + /// the parent [StatefulWidget] using the [State.setState] method, so that + /// the parent gets rebuilt; for example: + /// + /// {@tool sample} + /// + /// ```dart + /// class SegmentedControlExample extends StatefulWidget { + /// @override + /// State createState() => SegmentedControlExampleState(); + /// } + /// + /// class SegmentedControlExampleState extends State { + /// final Map children = const { + /// 0: Text('Child 1'), + /// 1: Text('Child 2'), + /// }; + /// + /// int currentValue; + /// + /// @override + /// Widget build(BuildContext context) { + /// return Container( + /// child: CupertinoSlidingSegmentedControl( + /// children: children, + /// onValueChanged: (int newValue) { + /// setState(() { + /// currentValue = newValue; + /// }); + /// }, + /// groupValue: currentValue, + /// ), + /// ); + /// } + /// } + /// ``` + /// {@end-tool} + final ValueChanged onValueChanged; /// The color used to paint the rounded rect behind the [children] and the separators. /// @@ -240,7 +239,7 @@ class _SegmentedControlState extends State> { final Map _highlightControllers = {}; - final Tween _highlightTween = _FontWeightTween(begin: FontWeight.normal, end: FontWeight.w600); + final Tween _highlightTween = _FontWeightTween(begin: FontWeight.normal, end: FontWeight.w500); final Map _pressControllers = {}; final Tween _pressTween = Tween(begin: 1, end: 0.2); @@ -255,8 +254,6 @@ class _SegmentedControlState extends State controller; - AnimationController _createHighlightAnimationController({ bool isCompleted = false }) { return AnimationController( duration: _kHighlightAnimationDuration, @@ -284,9 +281,7 @@ class _SegmentedControlState extends State extends State extends State extends State extends State index == null ? null : keys[index]; @@ -447,9 +422,9 @@ class _SegmentedControlState extends State extends State( children: children, diff --git a/packages/flutter/test/cupertino/sliding_segmented_control_test.dart b/packages/flutter/test/cupertino/sliding_segmented_control_test.dart index 9bce490f0a..a0600308f7 100644 --- a/packages/flutter/test/cupertino/sliding_segmented_control_test.dart +++ b/packages/flutter/test/cupertino/sliding_segmented_control_test.dart @@ -36,32 +36,51 @@ Widget setupSimpleSegmentedControl() { 0: Text('Child 1'), 1: Text('Child 2'), }; - final ValueNotifier controller = ValueNotifier(0); return boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: controller, + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, + ); +} + +StateSetter setState; +int groupValue = 0; +void defaultCallback(int newValue) { + setState(() { groupValue = newValue; }); +} + +Widget boilerplate({ WidgetBuilder builder }) { + return Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: StatefulBuilder(builder: (BuildContext context, StateSetter setter) { + setState = setter; + return builder(context); + }), ), ); } -Widget boilerplate({ Widget child }) { - return Directionality( - textDirection: TextDirection.ltr, - child: Center(child: child), - ); -} - void main() { - testWidgets('Children and controller and padding arguments can not be null', (WidgetTester tester) async { + + setUp(() { + setState = null; + groupValue = 0; + }); + + testWidgets('Children and onValueChanged and padding arguments can not be null', (WidgetTester tester) async { + groupValue = null; try { await tester.pumpWidget( - boilerplate( - child: CupertinoSlidingSegmentedControl( - children: null, - controller: ValueNotifier(null), - ), + CupertinoSlidingSegmentedControl( + children: null, + groupValue: groupValue, + onValueChanged: defaultCallback, ), ); fail('Should not be possible to create segmented control with null children'); @@ -76,26 +95,24 @@ void main() { try { await tester.pumpWidget( - boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: null, - ), + CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: null, ), ); - fail('Should not be possible to create segmented control without a controller'); + fail('Should not be possible to create segmented control without an onValueChanged'); } on AssertionError catch (e) { - expect(e.toString(), contains('controller')); + expect(e.toString(), contains('onValueChanged')); } try { await tester.pumpWidget( - boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: ValueNotifier(null), - padding: null, - ), + CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + padding: null, ), ); fail('Should not be possible to create segmented control with null padding'); @@ -106,13 +123,13 @@ void main() { testWidgets('Need at least 2 children', (WidgetTester tester) async { final Map children = {}; + groupValue = null; try { await tester.pumpWidget( - boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: ValueNotifier(null), - ), + CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, ), ); fail('Should not be possible to create a segmented control with no children'); @@ -123,11 +140,10 @@ void main() { children[0] = const Text('Child 1'); await tester.pumpWidget( - boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: ValueNotifier(null), - ), + CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, ), ); fail('Should not be possible to create a segmented control with just one child'); @@ -135,20 +151,20 @@ void main() { expect(e.toString(), contains('children.length')); } + groupValue = -1; try { children[1] = const Text('Child 2'); children[2] = const Text('Child 3'); await tester.pumpWidget( - boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: ValueNotifier(-1), - ), + CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, ), ); - fail('Should not be possible to create a segmented control with a controller pointing to a non-existent child'); + fail('Should not be possible to create a segmented control with a groupValue pointing to a non-existent child'); } on AssertionError catch (e) { - expect(e.toString(), contains('value must be either null or one of the keys in the children map')); + expect(e.toString(), contains('groupValue must be either null or one of the keys in the children map')); } }); @@ -185,11 +201,14 @@ void main() { await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - key: key, - children: children, - controller: ValueNotifier(null), - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + key: key, + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); @@ -204,12 +223,15 @@ void main() { await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - key: key, - padding: const EdgeInsets.fromLTRB(1, 3, 5, 7), - children: children, - controller: ValueNotifier(null), - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + key: key, + padding: const EdgeInsets.fromLTRB(1, 3, 5, 7), + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); @@ -230,119 +252,29 @@ void main() { 2: Text('Child 3'), }; - final ValueNotifier controller = ValueNotifier(0); - await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), - ), - ); - - expect(controller.value, 0); - - await tester.tap(find.text('Child 2')); - - expect(controller.value, 1); - - // Tapping the currently selected item should not change controller's value. - bool valueChanged = false; - controller.addListener(() { valueChanged = true; }); - - await tester.tap(find.text('Child 2')); - - expect(valueChanged, isFalse); - expect(controller.value, 1); - }); - - testWidgets('Changing controller works', (WidgetTester tester) async { - const Map children = { - 0: Text('Child 1'), - 1: Text('Child 2'), - 2: Text('Child 3'), - }; - - final ValueNotifier controller = ValueNotifier(0); - final ValueNotifier newControlelr = ValueNotifier(null); - - await tester.pumpWidget( - boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), - ), - ); - - expect( - currentUnscaledThumbRect(tester, useGlobalCoordinate: true).center, - offsetMoreOrLessEquals(tester.getCenter(find.text('Child 1'))), - ); - - await tester.pumpWidget( - boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: newControlelr, - ), - ), - ); - - expect( - currentUnscaledThumbRect(tester, useGlobalCoordinate: true), - isNull, - ); - }); - - testWidgets('Can change controller value in build method', (WidgetTester tester) async { - const Map children = { - 0: Text('Child 1'), - 1: Text('Child 2'), - 2: Text('Child 3'), - }; - - int currentIndex = 0; - StateSetter setState; - final ValueNotifier controller = ValueNotifier(currentIndex); - - await tester.pumpWidget( - StatefulBuilder( - builder: (BuildContext context, StateSetter setter) { - setState = setter; - if (controller.value != currentIndex) - controller.value = currentIndex; - return boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + key: const ValueKey('Segmented Control'), + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, ); }, ), ); - expect( - currentUnscaledThumbRect(tester, useGlobalCoordinate: true).center, - offsetMoreOrLessEquals(tester.getCenter(find.text('Child 1'))), - ); + expect(groupValue, 0); - setState(() { - currentIndex = 2; - }); + await tester.tap(find.text('Child 2')); - await tester.pump(); - await tester.pumpAndSettle(); + expect(groupValue, 1); - expect( - currentUnscaledThumbRect(tester, useGlobalCoordinate: true).center, - offsetMoreOrLessEquals(tester.getCenter(find.text('Child 3')), epsilon: 0.01), - ); + // Tapping the currently selected item should not change groupValue. + await tester.tap(find.text('Child 2')); + + expect(groupValue, 1); }); testWidgets( @@ -353,16 +285,15 @@ void main() { 1: Icon(IconData(1)), }; - final ValueNotifier controller = ValueNotifier(0); - await tester.pumpWidget( CupertinoApp( theme: const CupertinoThemeData(brightness: Brightness.dark), - home: StatefulBuilder( - builder: (BuildContext context, StateSetter setState) { + home: boilerplate( + builder: (BuildContext context) { return CupertinoSlidingSegmentedControl( children: children, - controller: controller, + groupValue: groupValue, + onValueChanged: defaultCallback, ); }, ), @@ -371,7 +302,7 @@ void main() { DefaultTextStyle textStyle = tester.widget(find.widgetWithText(DefaultTextStyle, 'Child 1').first); - expect(textStyle.style.fontWeight, FontWeight.w600); + expect(textStyle.style.fontWeight, FontWeight.w500); await tester.tap(find.byIcon(const IconData(1))); await tester.pump(); @@ -379,6 +310,7 @@ void main() { textStyle = tester.widget(find.widgetWithText(DefaultTextStyle, 'Child 1').first); + expect(groupValue, 1); expect(textStyle.style.fontWeight, FontWeight.normal); }, ); @@ -389,7 +321,6 @@ void main() { 1: Icon(IconData(1)), }; - final ValueNotifier controller = ValueNotifier(0); Brightness brightness = Brightness.light; StateSetter setState; @@ -400,12 +331,15 @@ void main() { return MediaQuery( data: MediaQueryData(platformBrightness: brightness), child: boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: controller, - thumbColor: CupertinoColors.systemGreen, - backgroundColor: CupertinoColors.systemRed, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + thumbColor: CupertinoColors.systemGreen, + backgroundColor: CupertinoColors.systemRed, + ); + }, ), ); }, @@ -443,14 +377,15 @@ void main() { 2: Placeholder(), }; - final ValueNotifier controller = ValueNotifier(0); - await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); }, @@ -468,16 +403,18 @@ void main() { 1: Text('Child 2'), }; - final ValueNotifier controller = ValueNotifier(null); - + groupValue = null; await tester.pumpWidget( StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ); }, ), @@ -496,14 +433,17 @@ void main() { }; // Child 3 is intially selected. - final ValueNotifier controller = ValueNotifier(2); + groupValue = 2; await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); @@ -571,15 +511,16 @@ void main() { 2: Container(constraints: const BoxConstraints.tightFor(height: 200.0)), }; - final ValueNotifier controller = ValueNotifier(null); - await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + key: const ValueKey('Segmented Control'), + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); @@ -600,14 +541,16 @@ void main() { 2: Container(constraints: const BoxConstraints.tightFor(width: 200.0)), }; - final ValueNotifier controller = ValueNotifier(null); await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + key: const ValueKey('Segmented Control'), + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); @@ -628,19 +571,20 @@ void main() { 1: SizedBox(width: 70), }; - final ValueNotifier controller = ValueNotifier(null); - await tester.pumpWidget( boilerplate( - child: Row( - children: [ - CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), - ], - ), + builder: (BuildContext context) { + return Row( + children: [ + CupertinoSlidingSegmentedControl( + key: const ValueKey('Segmented Control'), + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ), + ], + ); + }, ), ); @@ -660,14 +604,19 @@ void main() { 1: Text('Child 2'), }; - final ValueNotifier controller = ValueNotifier(null); await tester.pumpWidget( Directionality( textDirection: TextDirection.rtl, child: Center( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: controller, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setter) { + setState = setter; + return CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ), ), @@ -682,16 +631,19 @@ void main() { 0: Text('Child 1'), 1: Text('Child 2'), }; - - final ValueNotifier controller = ValueNotifier(0); - await tester.pumpWidget( Directionality( textDirection: TextDirection.rtl, child: Center( - child: CupertinoSlidingSegmentedControl( - children: children, - controller: controller, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setter) { + setState = setter; + return CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ), ), @@ -718,17 +670,15 @@ void main() { 1: Text('Child 2'), }; - final ValueNotifier controller = ValueNotifier(0); - await tester.pumpWidget( - Directionality( - textDirection: TextDirection.ltr, - child: Center( - child: CupertinoSlidingSegmentedControl( + boilerplate( + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( children: children, - controller: controller, - ), - ), + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); @@ -810,25 +760,26 @@ void main() { children[0] = const Text('Child 1'); children[1] = const SizedBox(); - final ValueNotifier controller = ValueNotifier(0); - await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + key: const ValueKey('Segmented Control'), + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); - expect(controller.value, 0); + expect(groupValue, 0); final Offset centerOfTwo = tester.getCenter(find.byWidget(children[1])); // Tap just inside segment bounds await tester.tapAt(centerOfTwo + const Offset(10, 0)); - expect(controller.value, 1); + expect(groupValue, 1); }); testWidgets('Thumb animation is correct when the selected segment changes', (WidgetTester tester) async { @@ -917,20 +868,23 @@ void main() { 1: Text('B'), 2: Text('C'), }; - final ValueNotifier controller = ValueNotifier(0); await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + key: const ValueKey('Segmented Control'), + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); await tester.tap(find.text('B')); await tester.pump(); + await tester.pump(); await tester.pump(const Duration(milliseconds: 40)); // Between A and B. @@ -965,15 +919,16 @@ void main() { children[2] = const Text('C'); children[3] = const Text('D'); - final ValueNotifier controller = ValueNotifier(0); - await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + key: const ValueKey('Segmented Control'), + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); @@ -984,11 +939,14 @@ void main() { children[1] = const Text('B'); await tester.pumpWidget( boilerplate( - child: CupertinoSlidingSegmentedControl( - key: const ValueKey('Segmented Control'), - children: children, - controller: controller, - ), + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + key: const ValueKey('Segmented Control'), + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), ); @@ -1005,7 +963,6 @@ void main() { 0: Text('Child 1'), 1: Text('Child 2'), }; - final ValueNotifier controller = ValueNotifier(0); final ScrollController scrollController = ScrollController(); await tester.pumpWidget( @@ -1015,9 +972,14 @@ void main() { controller: scrollController, children: [ const SizedBox(height: 100), - CupertinoSlidingSegmentedControl( - children: children, - controller: controller, + boilerplate( + builder: (BuildContext context) { + return CupertinoSlidingSegmentedControl( + children: children, + groupValue: groupValue, + onValueChanged: defaultCallback, + ); + }, ), const SizedBox(height: 1000), ], @@ -1029,7 +991,7 @@ void main() { await tester.tap(find.text('Child 2')); await tester.pump(); - expect(controller.value, 1); + expect(groupValue, 1); // Vertical drag works for the scroll view. final TestGesture gesture = await tester.startGesture(tester.getCenter(find.text('Child 1'))); @@ -1043,14 +1005,14 @@ void main() { expect(scrollController.offset, 100); // Does not affect the segmented control. - expect(controller.value, 1); + expect(groupValue, 1); await gesture.moveBy(const Offset(0, 100)); await gesture.up(); await tester.pump(); expect(scrollController.offset, 0); - expect(controller.value, 1); + expect(groupValue, 1); // Long press vertical drag is recognized by the segmented control. await gesture.down(tester.getCenter(find.text('Child 1'))); @@ -1061,7 +1023,7 @@ void main() { // Should not scroll. expect(scrollController.offset, 0); - expect(controller.value, 1); + expect(groupValue, 1); await gesture.moveBy(const Offset(0, 100)); await gesture.moveBy(const Offset(0, 100)); @@ -1069,7 +1031,7 @@ void main() { await tester.pump(); expect(scrollController.offset, 0); - expect(controller.value, 0); + expect(groupValue, 0); // Horizontal drag is recognized by the segmentedControl. await gesture.down(tester.getCenter(find.text('Child 1'))); @@ -1079,6 +1041,6 @@ void main() { await tester.pump(); expect(scrollController.offset, 0); - expect(controller.value, 1); + expect(groupValue, 1); }); }