diff --git a/examples/api/lib/cupertino/switch/cupertino_switch.0.dart b/examples/api/lib/cupertino/switch/cupertino_switch.0.dart index 9f40bf03d7..5e3d2aac7b 100644 --- a/examples/api/lib/cupertino/switch/cupertino_switch.0.dart +++ b/examples/api/lib/cupertino/switch/cupertino_switch.0.dart @@ -40,7 +40,7 @@ class _CupertinoSwitchExampleState extends State { child: CupertinoSwitch( // This bool value toggles the switch. value: switchValue, - activeColor: CupertinoColors.activeBlue, + activeTrackColor: CupertinoColors.activeBlue, onChanged: (bool? value) { // This is called when the user toggles the switch. setState(() { diff --git a/packages/flutter/lib/fix_data/fix_cupertino.yaml b/packages/flutter/lib/fix_data/fix_cupertino.yaml index 3654bb75a0..1a4563dfac 100644 --- a/packages/flutter/lib/fix_data/fix_cupertino.yaml +++ b/packages/flutter/lib/fix_data/fix_cupertino.yaml @@ -295,4 +295,50 @@ transforms: - kind: 'rename' newName: 'buildOverscrollIndicator' + # Changes made in https://github.com/flutter/flutter/pull/151367 + - title: "Rename to 'activeTrackColor'" + date: 2024-07-15 + element: + uris: [ 'cupertino.dart' ] + getter: 'activeColor' + inClass: 'CupertinoSwitch' + changes: + - kind: 'rename' + newName: 'activeTrackColor' + + # Changes made in https://github.com/flutter/flutter/pull/151367 + - title: "Migrate to 'activeTrackColor'" + date: 2024-07-15 + element: + uris: [ 'cupertino.dart' ] + constructor: '' + inClass: 'CupertinoSwitch' + changes: + - kind: 'renameParameter' + oldName: 'activeColor' + newName: 'activeTrackColor' + + # Changes made in https://github.com/flutter/flutter/pull/151367 + - title: "Rename to 'inactiveTrackColor'" + date: 2024-07-15 + element: + uris: [ 'cupertino.dart' ] + getter: 'trackColor' + inClass: 'CupertinoSwitch' + changes: + - kind: 'rename' + newName: 'inactiveTrackColor' + + # Changes made in https://github.com/flutter/flutter/pull/151367 + - title: "Migrate to 'inactiveTrackColor'" + date: 2024-07-15 + element: + uris: [ 'cupertino.dart' ] + constructor: '' + inClass: 'CupertinoSwitch' + changes: + - kind: 'renameParameter' + oldName: 'trackColor' + newName: 'inactiveTrackColor' + # Before adding a new fix: read instructions at the top of this file. diff --git a/packages/flutter/lib/src/cupertino/switch.dart b/packages/flutter/lib/src/cupertino/switch.dart index c354acdb84..2642c62d3b 100644 --- a/packages/flutter/lib/src/cupertino/switch.dart +++ b/packages/flutter/lib/src/cupertino/switch.dart @@ -120,8 +120,18 @@ class CupertinoSwitch extends StatefulWidget { super.key, required this.value, required this.onChanged, - this.activeColor, - this.trackColor, + @Deprecated( + 'Use activeTrackColor instead. ' + 'This feature was deprecated after v3.24.0-0.2.pre.' + ) + Color? activeColor, + @Deprecated( + 'Use inactiveTrackColor instead. ' + 'This feature was deprecated after v3.24.0-0.2.pre.' + ) + Color? trackColor, + Color? activeTrackColor, + Color? inactiveTrackColor, this.thumbColor, this.inactiveThumbColor, this.applyTheme, @@ -141,7 +151,11 @@ class CupertinoSwitch extends StatefulWidget { this.autofocus = false, this.dragStartBehavior = DragStartBehavior.start, }) : assert(activeThumbImage != null || onActiveThumbImageError == null), - assert(inactiveThumbImage != null || onInactiveThumbImageError == null); + assert(inactiveThumbImage != null || onInactiveThumbImageError == null), + assert(activeTrackColor == null || activeColor == null), + assert(inactiveTrackColor == null || trackColor == null), + activeTrackColor = activeTrackColor ?? activeColor, + inactiveTrackColor = inactiveTrackColor ?? trackColor; /// Whether this switch is on or off. final bool value; @@ -178,8 +192,23 @@ class CupertinoSwitch extends StatefulWidget { /// /// See also: /// - /// * [trackColor], the color to use for the track when the switch is off. - final Color? activeColor; + /// * [inactiveTrackColor], the color to use for the track when the switch is off. + @Deprecated( + 'Use activeTrackColor instead. ' + 'This feature was deprecated after v3.24.0-0.2.pre.' + ) + Color? get activeColor => activeTrackColor; + + /// The color to use for the track when the switch is on. + /// + /// If null and [applyTheme] is false, defaults to [CupertinoColors.systemGreen] + /// in accordance to native iOS behavior. Otherwise, defaults to + /// [CupertinoThemeData.primaryColor]. + /// + /// See also: + /// + /// * [inactiveTrackColor], the color to use for the track when the switch is off. + final Color? activeTrackColor; /// The color to use for the track when the switch is off. /// @@ -187,8 +216,21 @@ class CupertinoSwitch extends StatefulWidget { /// /// See also: /// - /// * [activeColor], the color to use for the track when the switch is on. - final Color? trackColor; + /// * [inactiveTrackColor], the color to use for the track when the switch is off. + @Deprecated( + 'Use inactiveTrackColor instead. ' + 'This feature was deprecated after v3.24.0-0.2.pre.' + ) + Color? get trackColor => inactiveTrackColor; + + /// The color to use for the track when the switch is off. + /// + /// Defaults to [CupertinoColors.secondarySystemFill] when null. + /// + /// See also: + /// + /// * [activeTrackColor], the color to use for the track when the switch is on. + final Color? inactiveTrackColor; /// The color to use for the thumb when the switch is on. /// @@ -483,9 +525,9 @@ class _CupertinoSwitchState extends State with TickerProviderSt WidgetStateProperty get _widgetTrackColor { return WidgetStateProperty.resolveWith((Set states) { if (states.contains(WidgetState.selected)) { - return widget.activeColor; + return widget.activeTrackColor; } - return widget.trackColor; + return widget.inactiveTrackColor; }); } @@ -584,7 +626,7 @@ class _CupertinoSwitchState extends State with TickerProviderSt final CupertinoThemeData theme = CupertinoTheme.of(context); final Color activeColor = CupertinoDynamicColor.resolve( - widget.activeColor + widget.activeTrackColor ?? ((widget.applyTheme ?? theme.applyThemeToAll) ? theme.primaryColor : null) ?? CupertinoColors.systemGreen, context, diff --git a/packages/flutter/test/cupertino/switch_test.dart b/packages/flutter/test/cupertino/switch_test.dart index 0ff292a0aa..2d955d2482 100644 --- a/packages/flutter/test/cupertino/switch_test.dart +++ b/packages/flutter/test/cupertino/switch_test.dart @@ -1867,6 +1867,50 @@ void main() { imageProvider2.complete(); expect(tester.takeException(), isNull); }); + + testWidgets('Switch uses inactive track color when set', (WidgetTester tester) async { + const Color inactiveTrackColor = Color(0xFF00FF00); + + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: CupertinoSwitch( + value: false, + inactiveTrackColor: inactiveTrackColor, + dragStartBehavior: DragStartBehavior.down, + onChanged: null, + ), + ), + ), + ); + + expect(find.byType(CupertinoSwitch), findsOneWidget); + expect(tester.widget(find.byType(CupertinoSwitch)).inactiveTrackColor, inactiveTrackColor); + expect(find.byType(CupertinoSwitch), paints..rrect(color: inactiveTrackColor)); + }); + + testWidgets('Switch uses active track color when set', (WidgetTester tester) async { + const Color activeTrackColor = Color(0xFF00FF00); + + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: CupertinoSwitch( + value: true, + activeTrackColor: activeTrackColor, + dragStartBehavior: DragStartBehavior.down, + onChanged: null, + ), + ), + ), + ); + + expect(find.byType(CupertinoSwitch), findsOneWidget); + expect(tester.widget(find.byType(CupertinoSwitch)).activeTrackColor, activeTrackColor); + expect(find.byType(CupertinoSwitch), paints..rrect(color: activeTrackColor)); + }); }); } diff --git a/packages/flutter/test_fixes/cupertino/cupertino.dart b/packages/flutter/test_fixes/cupertino/cupertino.dart index 6e313d4dc0..e418c5cb9c 100644 --- a/packages/flutter/test_fixes/cupertino/cupertino.dart +++ b/packages/flutter/test_fixes/cupertino/cupertino.dart @@ -248,4 +248,20 @@ void main() { final PlatformMenuBar platformMenuBar = PlatformMenuBar(menus: [], body: const SizedBox()); final Widget bodyValue = platformMenuBar.body; + + // Changes made in https://github.com/flutter/flutter/pull/151367 + final cupertinoSwitch = CupertinoSwitch( + value: value, + onChanged: onChanged, + activeColor: Colors.red, + ); + Color? activeTrackColor = cupertinoSwitch.activeColor; + + // Changes made in https://github.com/flutter/flutter/pull/151367 + final cupertinoSwitch = CupertinoSwitch( + value: value, + onChanged: onChanged, + trackColor: Colors.red, + ); + Color? inactiveTrackColor = cupertinoSwitch.trackColor; } diff --git a/packages/flutter/test_fixes/cupertino/cupertino.dart.expect b/packages/flutter/test_fixes/cupertino/cupertino.dart.expect index b0fc578e91..e6ab0deeb4 100644 --- a/packages/flutter/test_fixes/cupertino/cupertino.dart.expect +++ b/packages/flutter/test_fixes/cupertino/cupertino.dart.expect @@ -248,4 +248,20 @@ void main() { final PlatformMenuBar platformMenuBar = PlatformMenuBar(menus: [], child: const SizedBox()); final Widget bodyValue = platformMenuBar.child; + + // Changes made in https://github.com/flutter/flutter/pull/151367 + final cupertinoSwitch = CupertinoSwitch( + value: value, + onChanged: onChanged, + activeTrackColor: Colors.red, + ); + Color? activeTrackColor = cupertinoSwitch.activeTrackColor; + + // Changes made in https://github.com/flutter/flutter/pull/151367 + final cupertinoSwitch = CupertinoSwitch( + value: value, + onChanged: onChanged, + inactiveTrackColor: Colors.red, + ); + Color? inactiveTrackColor = cupertinoSwitch.inactiveTrackColor; }