Add Checkbox checkIcon color parameter #26808
The new parameter defines the color of the checkbox's check mark. Default is (still) white.
diff --git a/packages/flutter/lib/src/material/checkbox.dart b/packages/flutter/lib/src/material/checkbox.dart
index 849ebd0..00a879a 100644
--- a/packages/flutter/lib/src/material/checkbox.dart
+++ b/packages/flutter/lib/src/material/checkbox.dart
@@ -59,6 +59,7 @@
this.tristate = false,
@required this.onChanged,
this.activeColor,
+ this.checkColor,
this.materialTapTargetSize,
}) : assert(tristate != null),
assert(tristate || value != null),
@@ -103,6 +104,11 @@
/// Defaults to [ThemeData.toggleableActiveColor].
final Color activeColor;
+ /// The color to use for the check icon when this checkbox is checked
+ ///
+ /// Defaults to Color(0xFFFFFFFF)
+ final Color checkColor;
+
/// If true the checkbox's [value] can be true, false, or null.
///
/// Checkbox displays a dash when its value is null.
@@ -150,6 +156,7 @@
value: widget.value,
tristate: widget.tristate,
activeColor: widget.activeColor ?? themeData.toggleableActiveColor,
+ checkColor: widget.checkColor ?? const Color(0xFFFFFFFF),
inactiveColor: widget.onChanged != null ? themeData.unselectedWidgetColor : themeData.disabledColor,
onChanged: widget.onChanged,
additionalConstraints: additionalConstraints,
@@ -164,6 +171,7 @@
@required this.value,
@required this.tristate,
@required this.activeColor,
+ @required this.checkColor,
@required this.inactiveColor,
@required this.onChanged,
@required this.vsync,
@@ -178,6 +186,7 @@
final bool value;
final bool tristate;
final Color activeColor;
+ final Color checkColor;
final Color inactiveColor;
final ValueChanged<bool> onChanged;
final TickerProvider vsync;
@@ -188,6 +197,7 @@
value: value,
tristate: tristate,
activeColor: activeColor,
+ checkColor: checkColor,
inactiveColor: inactiveColor,
onChanged: onChanged,
vsync: vsync,
@@ -200,6 +210,7 @@
..value = value
..tristate = tristate
..activeColor = activeColor
+ ..checkColor = checkColor
..inactiveColor = inactiveColor
..onChanged = onChanged
..additionalConstraints = additionalConstraints
@@ -216,6 +227,7 @@
bool value,
bool tristate,
Color activeColor,
+ this.checkColor,
Color inactiveColor,
BoxConstraints additionalConstraints,
ValueChanged<bool> onChanged,
@@ -232,6 +244,7 @@
);
bool _oldValue;
+ Color checkColor;
@override
set value(bool newValue) {
@@ -270,7 +283,7 @@
// White stroke used to paint the check and dash.
void _initStrokePaint(Paint paint) {
paint
- ..color = const Color(0xFFFFFFFF)
+ ..color = checkColor
..style = PaintingStyle.stroke
..strokeWidth = _kStrokeWidth;
}
diff --git a/packages/flutter/test/material/checkbox_test.dart b/packages/flutter/test/material/checkbox_test.dart
index 58945d1..6429fb6 100644
--- a/packages/flutter/test/material/checkbox_test.dart
+++ b/packages/flutter/test/material/checkbox_test.dart
@@ -336,4 +336,32 @@
expect(getCheckboxRenderer(), paints..line()); // null is rendered as a line (a "dash")
});
+ testWidgets('CheckBox color rendering', (WidgetTester tester) async {
+ Widget buildFrame(Color color) {
+ return Material(
+ child: StatefulBuilder(
+ builder: (BuildContext context, StateSetter setState) {
+ return Checkbox(
+ value: true,
+ checkColor: color,
+ onChanged: (bool value) { },
+ );
+ },
+ ),
+ );
+ }
+
+ RenderToggleable getCheckboxRenderer() {
+ return tester.renderObject<RenderToggleable>(find.byType(Checkbox));
+ }
+
+ await tester.pumpWidget(buildFrame(null));
+ await tester.pumpAndSettle();
+ expect(getCheckboxRenderer(), paints..path(color: const Color(0xFFFFFFFF))); // paints's color is 0xFFFFFFFF (default color)
+
+ await tester.pumpWidget(buildFrame(const Color(0xFF000000)));
+ await tester.pumpAndSettle();
+ expect(getCheckboxRenderer(), paints..path(color: const Color(0xFF000000))); // paints's color is 0xFF000000 (params)
+ });
+
}