Add Checkbox checkIcon color parameter #26808
The new parameter defines the color of the checkbox's check mark. Default is (still) white.
This commit is contained in:
parent
d5b6480648
commit
4e235c0cd8
@ -59,6 +59,7 @@ class Checkbox extends StatefulWidget {
|
||||
this.tristate = false,
|
||||
@required this.onChanged,
|
||||
this.activeColor,
|
||||
this.checkColor,
|
||||
this.materialTapTargetSize,
|
||||
}) : assert(tristate != null),
|
||||
assert(tristate || value != null),
|
||||
@ -103,6 +104,11 @@ class Checkbox extends StatefulWidget {
|
||||
/// 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 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin {
|
||||
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 @@ class _CheckboxRenderObjectWidget extends LeafRenderObjectWidget {
|
||||
@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 @@ class _CheckboxRenderObjectWidget extends LeafRenderObjectWidget {
|
||||
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 @@ class _CheckboxRenderObjectWidget extends LeafRenderObjectWidget {
|
||||
value: value,
|
||||
tristate: tristate,
|
||||
activeColor: activeColor,
|
||||
checkColor: checkColor,
|
||||
inactiveColor: inactiveColor,
|
||||
onChanged: onChanged,
|
||||
vsync: vsync,
|
||||
@ -200,6 +210,7 @@ class _CheckboxRenderObjectWidget extends LeafRenderObjectWidget {
|
||||
..value = value
|
||||
..tristate = tristate
|
||||
..activeColor = activeColor
|
||||
..checkColor = checkColor
|
||||
..inactiveColor = inactiveColor
|
||||
..onChanged = onChanged
|
||||
..additionalConstraints = additionalConstraints
|
||||
@ -216,6 +227,7 @@ class _RenderCheckbox extends RenderToggleable {
|
||||
bool value,
|
||||
bool tristate,
|
||||
Color activeColor,
|
||||
this.checkColor,
|
||||
Color inactiveColor,
|
||||
BoxConstraints additionalConstraints,
|
||||
ValueChanged<bool> onChanged,
|
||||
@ -232,6 +244,7 @@ class _RenderCheckbox extends RenderToggleable {
|
||||
);
|
||||
|
||||
bool _oldValue;
|
||||
Color checkColor;
|
||||
|
||||
@override
|
||||
set value(bool newValue) {
|
||||
@ -270,7 +283,7 @@ class _RenderCheckbox extends RenderToggleable {
|
||||
// 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;
|
||||
}
|
||||
|
@ -336,4 +336,32 @@ void main() {
|
||||
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)
|
||||
});
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user