diff --git a/packages/flutter/lib/src/cupertino/switch.dart b/packages/flutter/lib/src/cupertino/switch.dart index f779d6828c..25ccb04802 100644 --- a/packages/flutter/lib/src/cupertino/switch.dart +++ b/packages/flutter/lib/src/cupertino/switch.dart @@ -60,6 +60,7 @@ class CupertinoSwitch extends StatefulWidget { @required this.value, @required this.onChanged, this.activeColor, + this.trackColor, this.dragStartBehavior = DragStartBehavior.start, }) : assert(value != null), assert(dragStartBehavior != null), @@ -100,6 +101,11 @@ class CupertinoSwitch extends StatefulWidget { /// the [CupertinoTheme] in accordance to native iOS behavior. final Color activeColor; + /// The color to use for the background when the switch is off. + /// + /// Defaults to [CupertinoColors.secondarySystemFill] when null. + final Color trackColor; + /// {@template flutter.cupertino.switch.dragStartBehavior} /// Determines the way that drag start behavior is handled. /// @@ -144,6 +150,7 @@ class _CupertinoSwitchState extends State with TickerProviderSt widget.activeColor ?? CupertinoColors.systemGreen, context, ), + trackColor: CupertinoDynamicColor.resolve(widget.trackColor ?? CupertinoColors.secondarySystemFill, context), onChanged: widget.onChanged, vsync: this, dragStartBehavior: widget.dragStartBehavior, @@ -157,6 +164,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget { Key key, this.value, this.activeColor, + this.trackColor, this.onChanged, this.vsync, this.dragStartBehavior = DragStartBehavior.start, @@ -164,6 +172,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget { final bool value; final Color activeColor; + final Color trackColor; final ValueChanged onChanged; final TickerProvider vsync; final DragStartBehavior dragStartBehavior; @@ -173,7 +182,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget { return _RenderCupertinoSwitch( value: value, activeColor: activeColor, - trackColor: CupertinoDynamicColor.resolve(CupertinoColors.secondarySystemFill, context), + trackColor: trackColor, onChanged: onChanged, textDirection: Directionality.of(context), vsync: vsync, @@ -186,7 +195,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget { renderObject ..value = value ..activeColor = activeColor - ..trackColor = CupertinoDynamicColor.resolve(CupertinoColors.secondarySystemFill, context) + ..trackColor = trackColor ..onChanged = onChanged ..textDirection = Directionality.of(context) ..vsync = vsync diff --git a/packages/flutter/lib/src/material/switch.dart b/packages/flutter/lib/src/material/switch.dart index 296c99414a..d5e1cd2a7a 100644 --- a/packages/flutter/lib/src/material/switch.dart +++ b/packages/flutter/lib/src/material/switch.dart @@ -335,6 +335,7 @@ class _SwitchState extends State with TickerProviderStateMixin { value: widget.value, onChanged: widget.onChanged, activeColor: widget.activeColor, + trackColor: widget.inactiveTrackColor ), ), ); diff --git a/packages/flutter/test/cupertino/switch_test.dart b/packages/flutter/test/cupertino/switch_test.dart index 805b3c2600..2935cecf93 100644 --- a/packages/flutter/test/cupertino/switch_test.dart +++ b/packages/flutter/test/cupertino/switch_test.dart @@ -7,6 +7,9 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/services.dart'; import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; + +import '../rendering/mock_canvas.dart'; void main() { testWidgets('Switch can toggle on tap', (WidgetTester tester) async { @@ -433,6 +436,28 @@ void main() { expect(tester.widget(find.byType(Opacity).first).opacity, 0.5); }); + testWidgets('Switch is using track color when set', (WidgetTester tester) async { + const Color trackColor = Color(0xFF00FF00); + + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: CupertinoSwitch( + value: false, + trackColor: trackColor, + dragStartBehavior: DragStartBehavior.down, + onChanged: null, + ), + ), + ), + ); + + expect(find.byType(CupertinoSwitch), findsOneWidget); + expect(tester.widget(find.byType(CupertinoSwitch)).trackColor, trackColor); + expect(find.byType(CupertinoSwitch), paints..rrect(color: trackColor)); + }); + testWidgets('Switch is opaque when enabled', (WidgetTester tester) async { await tester.pumpWidget( Directionality( diff --git a/packages/flutter/test/material/switch_test.dart b/packages/flutter/test/material/switch_test.dart index 35b82e99d5..6aa8e9b5a2 100644 --- a/packages/flutter/test/material/switch_test.dart +++ b/packages/flutter/test/material/switch_test.dart @@ -585,6 +585,7 @@ void main() { testWidgets('Switch.adaptive', (WidgetTester tester) async { bool value = false; + const Color inactiveTrackColor = Colors.pink; Widget buildFrame(TargetPlatform platform) { return MaterialApp( @@ -595,6 +596,7 @@ void main() { child: Center( child: Switch.adaptive( value: value, + inactiveTrackColor: inactiveTrackColor, onChanged: (bool newValue) { setState(() { value = newValue; @@ -611,6 +613,9 @@ void main() { await tester.pumpWidget(buildFrame(TargetPlatform.iOS)); expect(find.byType(CupertinoSwitch), findsOneWidget); + final CupertinoSwitch adaptiveSwitch = tester.widget(find.byType(CupertinoSwitch)); + expect(adaptiveSwitch.trackColor, inactiveTrackColor); + expect(value, isFalse); await tester.tap(find.byType(Switch)); expect(value, isTrue);