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 {
@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<CupertinoSwitch> 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<bool> 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
......
......@@ -335,6 +335,7 @@ class _SwitchState extends State<Switch> with TickerProviderStateMixin {
value: widget.value,
onChanged: widget.onChanged,
activeColor: widget.activeColor,
trackColor: widget.inactiveTrackColor
),
),
);
......
......@@ -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<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 {
await tester.pumpWidget(
Directionality(
......
......@@ -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);
......
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