Style: Rename CupertinoSwitch activeColor and trackColor to activeTrackColor and InactiveTrackColor (#151367)

Style: Rename CupertinoSwitch `activeColor` and `trackColor` to `activeTrackColor` and `InactiveTrackColor`

Resolves #151256
This commit is contained in:
Kishan Rathore 2024-08-07 23:12:57 +05:30 committed by GitHub
parent d595e98d85
commit 0f7bceb9c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 175 additions and 11 deletions

View File

@ -40,7 +40,7 @@ class _CupertinoSwitchExampleState extends State<CupertinoSwitchExample> {
child: CupertinoSwitch( child: CupertinoSwitch(
// This bool value toggles the switch. // This bool value toggles the switch.
value: switchValue, value: switchValue,
activeColor: CupertinoColors.activeBlue, activeTrackColor: CupertinoColors.activeBlue,
onChanged: (bool? value) { onChanged: (bool? value) {
// This is called when the user toggles the switch. // This is called when the user toggles the switch.
setState(() { setState(() {

View File

@ -295,4 +295,50 @@ transforms:
- kind: 'rename' - kind: 'rename'
newName: 'buildOverscrollIndicator' 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. # Before adding a new fix: read instructions at the top of this file.

View File

@ -120,8 +120,18 @@ class CupertinoSwitch extends StatefulWidget {
super.key, super.key,
required this.value, required this.value,
required this.onChanged, required this.onChanged,
this.activeColor, @Deprecated(
this.trackColor, '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.thumbColor,
this.inactiveThumbColor, this.inactiveThumbColor,
this.applyTheme, this.applyTheme,
@ -141,7 +151,11 @@ class CupertinoSwitch extends StatefulWidget {
this.autofocus = false, this.autofocus = false,
this.dragStartBehavior = DragStartBehavior.start, this.dragStartBehavior = DragStartBehavior.start,
}) : assert(activeThumbImage != null || onActiveThumbImageError == null), }) : 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. /// Whether this switch is on or off.
final bool value; final bool value;
@ -178,8 +192,23 @@ class CupertinoSwitch extends StatefulWidget {
/// ///
/// See also: /// See also:
/// ///
/// * [trackColor], the color to use for the track when the switch is off. /// * [inactiveTrackColor], the color to use for the track when the switch is off.
final Color? activeColor; @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. /// The color to use for the track when the switch is off.
/// ///
@ -187,8 +216,21 @@ class CupertinoSwitch extends StatefulWidget {
/// ///
/// See also: /// See also:
/// ///
/// * [activeColor], the color to use for the track when the switch is on. /// * [inactiveTrackColor], the color to use for the track when the switch is off.
final Color? trackColor; @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. /// The color to use for the thumb when the switch is on.
/// ///
@ -483,9 +525,9 @@ class _CupertinoSwitchState extends State<CupertinoSwitch> with TickerProviderSt
WidgetStateProperty<Color?> get _widgetTrackColor { WidgetStateProperty<Color?> get _widgetTrackColor {
return WidgetStateProperty.resolveWith((Set<WidgetState> states) { return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
if (states.contains(WidgetState.selected)) { 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<CupertinoSwitch> with TickerProviderSt
final CupertinoThemeData theme = CupertinoTheme.of(context); final CupertinoThemeData theme = CupertinoTheme.of(context);
final Color activeColor = CupertinoDynamicColor.resolve( final Color activeColor = CupertinoDynamicColor.resolve(
widget.activeColor widget.activeTrackColor
?? ((widget.applyTheme ?? theme.applyThemeToAll) ? theme.primaryColor : null) ?? ((widget.applyTheme ?? theme.applyThemeToAll) ? theme.primaryColor : null)
?? CupertinoColors.systemGreen, ?? CupertinoColors.systemGreen,
context, context,

View File

@ -1867,6 +1867,50 @@ void main() {
imageProvider2.complete(); imageProvider2.complete();
expect(tester.takeException(), isNull); 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<CupertinoSwitch>(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<CupertinoSwitch>(find.byType(CupertinoSwitch)).activeTrackColor, activeTrackColor);
expect(find.byType(CupertinoSwitch), paints..rrect(color: activeTrackColor));
});
}); });
} }

View File

@ -248,4 +248,20 @@ void main() {
final PlatformMenuBar platformMenuBar = PlatformMenuBar(menus: <PlatformMenuItem>[], body: const SizedBox()); final PlatformMenuBar platformMenuBar = PlatformMenuBar(menus: <PlatformMenuItem>[], body: const SizedBox());
final Widget bodyValue = platformMenuBar.body; 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;
} }

View File

@ -248,4 +248,20 @@ void main() {
final PlatformMenuBar platformMenuBar = PlatformMenuBar(menus: <PlatformMenuItem>[], child: const SizedBox()); final PlatformMenuBar platformMenuBar = PlatformMenuBar(menus: <PlatformMenuItem>[], child: const SizedBox());
final Widget bodyValue = platformMenuBar.child; 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;
} }