Commit 417db34d authored by Sebastian Döll's avatar Sebastian Döll Committed by LongCatIsLooong

Set track color in Cupertino Switch and Adaptive Switch (#45074)

parent 50532f38
...@@ -60,6 +60,7 @@ class CupertinoSwitch extends StatefulWidget { ...@@ -60,6 +60,7 @@ class CupertinoSwitch extends StatefulWidget {
@required this.value, @required this.value,
@required this.onChanged, @required this.onChanged,
this.activeColor, this.activeColor,
this.trackColor,
this.dragStartBehavior = DragStartBehavior.start, this.dragStartBehavior = DragStartBehavior.start,
}) : assert(value != null), }) : assert(value != null),
assert(dragStartBehavior != null), assert(dragStartBehavior != null),
...@@ -100,6 +101,11 @@ class CupertinoSwitch extends StatefulWidget { ...@@ -100,6 +101,11 @@ class CupertinoSwitch extends StatefulWidget {
/// the [CupertinoTheme] in accordance to native iOS behavior. /// the [CupertinoTheme] in accordance to native iOS behavior.
final Color activeColor; 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} /// {@template flutter.cupertino.switch.dragStartBehavior}
/// Determines the way that drag start behavior is handled. /// Determines the way that drag start behavior is handled.
/// ///
...@@ -144,6 +150,7 @@ class _CupertinoSwitchState extends State<CupertinoSwitch> with TickerProviderSt ...@@ -144,6 +150,7 @@ class _CupertinoSwitchState extends State<CupertinoSwitch> with TickerProviderSt
widget.activeColor ?? CupertinoColors.systemGreen, widget.activeColor ?? CupertinoColors.systemGreen,
context, context,
), ),
trackColor: CupertinoDynamicColor.resolve(widget.trackColor ?? CupertinoColors.secondarySystemFill, context),
onChanged: widget.onChanged, onChanged: widget.onChanged,
vsync: this, vsync: this,
dragStartBehavior: widget.dragStartBehavior, dragStartBehavior: widget.dragStartBehavior,
...@@ -157,6 +164,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -157,6 +164,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget {
Key key, Key key,
this.value, this.value,
this.activeColor, this.activeColor,
this.trackColor,
this.onChanged, this.onChanged,
this.vsync, this.vsync,
this.dragStartBehavior = DragStartBehavior.start, this.dragStartBehavior = DragStartBehavior.start,
...@@ -164,6 +172,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -164,6 +172,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget {
final bool value; final bool value;
final Color activeColor; final Color activeColor;
final Color trackColor;
final ValueChanged<bool> onChanged; final ValueChanged<bool> onChanged;
final TickerProvider vsync; final TickerProvider vsync;
final DragStartBehavior dragStartBehavior; final DragStartBehavior dragStartBehavior;
...@@ -173,7 +182,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -173,7 +182,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget {
return _RenderCupertinoSwitch( return _RenderCupertinoSwitch(
value: value, value: value,
activeColor: activeColor, activeColor: activeColor,
trackColor: CupertinoDynamicColor.resolve(CupertinoColors.secondarySystemFill, context), trackColor: trackColor,
onChanged: onChanged, onChanged: onChanged,
textDirection: Directionality.of(context), textDirection: Directionality.of(context),
vsync: vsync, vsync: vsync,
...@@ -186,7 +195,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -186,7 +195,7 @@ class _CupertinoSwitchRenderObjectWidget extends LeafRenderObjectWidget {
renderObject renderObject
..value = value ..value = value
..activeColor = activeColor ..activeColor = activeColor
..trackColor = CupertinoDynamicColor.resolve(CupertinoColors.secondarySystemFill, context) ..trackColor = trackColor
..onChanged = onChanged ..onChanged = onChanged
..textDirection = Directionality.of(context) ..textDirection = Directionality.of(context)
..vsync = vsync ..vsync = vsync
......
...@@ -335,6 +335,7 @@ class _SwitchState extends State<Switch> with TickerProviderStateMixin { ...@@ -335,6 +335,7 @@ class _SwitchState extends State<Switch> with TickerProviderStateMixin {
value: widget.value, value: widget.value,
onChanged: widget.onChanged, onChanged: widget.onChanged,
activeColor: widget.activeColor, activeColor: widget.activeColor,
trackColor: widget.inactiveTrackColor
), ),
), ),
); );
......
...@@ -7,6 +7,9 @@ import 'package:flutter_test/flutter_test.dart'; ...@@ -7,6 +7,9 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../rendering/mock_canvas.dart';
void main() { void main() {
testWidgets('Switch can toggle on tap', (WidgetTester tester) async { testWidgets('Switch can toggle on tap', (WidgetTester tester) async {
...@@ -433,6 +436,28 @@ void main() { ...@@ -433,6 +436,28 @@ void main() {
expect(tester.widget<Opacity>(find.byType(Opacity).first).opacity, 0.5); expect(tester.widget<Opacity>(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<CupertinoSwitch>(find.byType(CupertinoSwitch)).trackColor, trackColor);
expect(find.byType(CupertinoSwitch), paints..rrect(color: trackColor));
});
testWidgets('Switch is opaque when enabled', (WidgetTester tester) async { testWidgets('Switch is opaque when enabled', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
Directionality( Directionality(
......
...@@ -585,6 +585,7 @@ void main() { ...@@ -585,6 +585,7 @@ void main() {
testWidgets('Switch.adaptive', (WidgetTester tester) async { testWidgets('Switch.adaptive', (WidgetTester tester) async {
bool value = false; bool value = false;
const Color inactiveTrackColor = Colors.pink;
Widget buildFrame(TargetPlatform platform) { Widget buildFrame(TargetPlatform platform) {
return MaterialApp( return MaterialApp(
...@@ -595,6 +596,7 @@ void main() { ...@@ -595,6 +596,7 @@ void main() {
child: Center( child: Center(
child: Switch.adaptive( child: Switch.adaptive(
value: value, value: value,
inactiveTrackColor: inactiveTrackColor,
onChanged: (bool newValue) { onChanged: (bool newValue) {
setState(() { setState(() {
value = newValue; value = newValue;
...@@ -611,6 +613,9 @@ void main() { ...@@ -611,6 +613,9 @@ void main() {
await tester.pumpWidget(buildFrame(TargetPlatform.iOS)); await tester.pumpWidget(buildFrame(TargetPlatform.iOS));
expect(find.byType(CupertinoSwitch), findsOneWidget); expect(find.byType(CupertinoSwitch), findsOneWidget);
final CupertinoSwitch adaptiveSwitch = tester.widget(find.byType(CupertinoSwitch));
expect(adaptiveSwitch.trackColor, inactiveTrackColor);
expect(value, isFalse); expect(value, isFalse);
await tester.tap(find.byType(Switch)); await tester.tap(find.byType(Switch));
expect(value, isTrue); expect(value, isTrue);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment