Commit 8a441ae7 authored by jiamingc's avatar jiamingc Committed by Ian Hickson

Make switch widget accept 3 other colors. (#12118)

* Make switch widget accept 3 other colors: inactiveThumbColor, inactiveTrackColor, activeTrackColor.

* Make switch widget accept 3 other colors.

* Make switch widget accept 3 other colors.
parent 7a35db1f
...@@ -51,6 +51,9 @@ class Switch extends StatefulWidget { ...@@ -51,6 +51,9 @@ class Switch extends StatefulWidget {
@required this.value, @required this.value,
@required this.onChanged, @required this.onChanged,
this.activeColor, this.activeColor,
this.activeTrackColor,
this.inactiveThumbColor,
this.inactiveTrackColor,
this.activeThumbImage, this.activeThumbImage,
this.inactiveThumbImage this.inactiveThumbImage
}) : super(key: key); }) : super(key: key);
...@@ -89,6 +92,21 @@ class Switch extends StatefulWidget { ...@@ -89,6 +92,21 @@ class Switch extends StatefulWidget {
/// Defaults to accent color of the current [Theme]. /// Defaults to accent color of the current [Theme].
final Color activeColor; final Color activeColor;
/// The color to use on the track when this switch is on.
///
/// Defaults to accent color of the current [Theme] with the opacity set at 50%.
final Color activeTrackColor;
/// The color to use on the thumb when this switch is off.
///
/// Defaults to the colors described in the Material design specification.
final Color inactiveThumbColor;
/// The color to use on the track when this switch is off.
///
/// Defaults to the colors described in the Material design specification.
final Color inactiveTrackColor;
/// An image to use on the thumb of this switch when the switch is on. /// An image to use on the thumb of this switch when the switch is on.
final ImageProvider activeThumbImage; final ImageProvider activeThumbImage;
...@@ -114,16 +132,16 @@ class _SwitchState extends State<Switch> with TickerProviderStateMixin { ...@@ -114,16 +132,16 @@ class _SwitchState extends State<Switch> with TickerProviderStateMixin {
final bool isDark = themeData.brightness == Brightness.dark; final bool isDark = themeData.brightness == Brightness.dark;
final Color activeThumbColor = widget.activeColor ?? themeData.accentColor; final Color activeThumbColor = widget.activeColor ?? themeData.accentColor;
final Color activeTrackColor = activeThumbColor.withAlpha(0x80); final Color activeTrackColor = widget.activeTrackColor ?? activeThumbColor.withAlpha(0x80);
Color inactiveThumbColor; Color inactiveThumbColor;
Color inactiveTrackColor; Color inactiveTrackColor;
if (widget.onChanged != null) { if (widget.onChanged != null) {
inactiveThumbColor = isDark ? Colors.grey.shade400 : Colors.grey.shade50; inactiveThumbColor = widget.inactiveThumbColor ?? (isDark ? Colors.grey.shade400 : Colors.grey.shade50);
inactiveTrackColor = isDark ? Colors.white30 : Colors.black26; inactiveTrackColor = widget.inactiveTrackColor ?? (isDark ? Colors.white30 : Colors.black26);
} else { } else {
inactiveThumbColor = isDark ? Colors.grey.shade800 : Colors.grey.shade400; inactiveThumbColor = widget.inactiveThumbColor ?? (isDark ? Colors.grey.shade800 : Colors.grey.shade400);
inactiveTrackColor = isDark ? Colors.white10 : Colors.black12; inactiveTrackColor = widget.inactiveTrackColor ?? (isDark ? Colors.white10 : Colors.black12);
} }
return new _SwitchRenderObjectWidget( return new _SwitchRenderObjectWidget(
......
...@@ -6,6 +6,8 @@ import 'package:flutter/material.dart'; ...@@ -6,6 +6,8 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.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 {
final Key switchKey = new UniqueKey(); final Key switchKey = new UniqueKey();
...@@ -110,8 +112,6 @@ void main() { ...@@ -110,8 +112,6 @@ void main() {
), ),
); );
expect(value, isFalse);
await tester.drag(find.byType(Switch), const Offset(30.0, 0.0)); await tester.drag(find.byType(Switch), const Offset(30.0, 0.0));
expect(value, isFalse); expect(value, isFalse);
...@@ -130,4 +130,59 @@ void main() { ...@@ -130,4 +130,59 @@ void main() {
expect(value, isFalse); expect(value, isFalse);
}); });
testWidgets('Switch can be set color', (WidgetTester tester) async {
bool value = false;
await tester.pumpWidget(
new Directionality(
textDirection: TextDirection.rtl,
child: new StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return new Material(
child: new Center(
child: new Switch(
value: value,
onChanged: (bool newValue) {
setState(() {
value = newValue;
});
},
activeColor: Colors.red[500],
activeTrackColor: Colors.green[500],
inactiveThumbColor: Colors.yellow[500],
inactiveTrackColor: Colors.blue[500],
),
),
);
},
),
),
);
expect(
Material.of(tester.element(find.byType(Switch))),
paints
..rrect(
color: Colors.blue[500],
rrect: new RRect.fromLTRBR(
383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
..circle(color: const Color(0x33000000))
..circle(color: const Color(0x24000000))
..circle(color: const Color(0x1f000000))
..circle(color: Colors.yellow[500]));
await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));
await tester.pump();
expect(
Material.of(tester.element(find.byType(Switch))),
paints
..rrect(
color: Colors.green[500],
rrect: new RRect.fromLTRBR(
383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
..circle(color: const Color(0x33000000))
..circle(color: const Color(0x24000000))
..circle(color: const Color(0x1f000000))
..circle(color: Colors.red[500]));
});
} }
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