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.
diff --git a/packages/flutter/lib/src/material/switch.dart b/packages/flutter/lib/src/material/switch.dart index bc4953d..d685ae2 100644 --- a/packages/flutter/lib/src/material/switch.dart +++ b/packages/flutter/lib/src/material/switch.dart
@@ -51,6 +51,9 @@ @required this.value, @required this.onChanged, this.activeColor, + this.activeTrackColor, + this.inactiveThumbColor, + this.inactiveTrackColor, this.activeThumbImage, this.inactiveThumbImage }) : super(key: key); @@ -89,6 +92,21 @@ /// Defaults to accent color of the current [Theme]. 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. final ImageProvider activeThumbImage; @@ -114,16 +132,16 @@ final bool isDark = themeData.brightness == Brightness.dark; final Color activeThumbColor = widget.activeColor ?? themeData.accentColor; - final Color activeTrackColor = activeThumbColor.withAlpha(0x80); + final Color activeTrackColor = widget.activeTrackColor ?? activeThumbColor.withAlpha(0x80); Color inactiveThumbColor; Color inactiveTrackColor; if (widget.onChanged != null) { - inactiveThumbColor = isDark ? Colors.grey.shade400 : Colors.grey.shade50; - inactiveTrackColor = isDark ? Colors.white30 : Colors.black26; + inactiveThumbColor = widget.inactiveThumbColor ?? (isDark ? Colors.grey.shade400 : Colors.grey.shade50); + inactiveTrackColor = widget.inactiveTrackColor ?? (isDark ? Colors.white30 : Colors.black26); } else { - inactiveThumbColor = isDark ? Colors.grey.shade800 : Colors.grey.shade400; - inactiveTrackColor = isDark ? Colors.white10 : Colors.black12; + inactiveThumbColor = widget.inactiveThumbColor ?? (isDark ? Colors.grey.shade800 : Colors.grey.shade400); + inactiveTrackColor = widget.inactiveTrackColor ?? (isDark ? Colors.white10 : Colors.black12); } return new _SwitchRenderObjectWidget(
diff --git a/packages/flutter/test/material/switch_test.dart b/packages/flutter/test/material/switch_test.dart index c05e0df..78c41bf 100644 --- a/packages/flutter/test/material/switch_test.dart +++ b/packages/flutter/test/material/switch_test.dart
@@ -6,6 +6,8 @@ import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; +import '../rendering/mock_canvas.dart'; + void main() { testWidgets('Switch can toggle on tap', (WidgetTester tester) async { final Key switchKey = new UniqueKey(); @@ -110,8 +112,6 @@ ), ); - expect(value, isFalse); - await tester.drag(find.byType(Switch), const Offset(30.0, 0.0)); expect(value, isFalse); @@ -130,4 +130,59 @@ 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])); + }); }