Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit 7db80db

Browse files
Googlernshahan
authored andcommitted
Prevent material_checkbox from marking itself as dirty when it receives an initial form value.
PiperOrigin-RevId: 193134551
1 parent a6d5e3d commit 7db80db

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/material_checkbox/material_checkbox.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class MaterialCheckboxComponent implements ControlValueAccessor, Focusable {
111111
writeValue(newValue) {
112112
// Need to ignore the null on init.
113113
if (newValue == null) return;
114-
checked = newValue as bool;
114+
_setStates(checked: (newValue as bool), emitEvent: false);
115115
}
116116

117117
@override
@@ -194,8 +194,9 @@ class MaterialCheckboxComponent implements ControlValueAccessor, Focusable {
194194

195195
/// Actually update the state variables. If both parameters are provided, then
196196
/// set them as presented, otherwise we will clear the other one if necessary.
197-
/// Events are only fired if there was a change.
198-
void _setStates({bool checked: false, bool indeterminate: false}) {
197+
/// Events are only fired if there was a change and [emitEvent] is true.
198+
void _setStates(
199+
{bool checked: false, bool indeterminate: false, bool emitEvent = true}) {
199200
// At most one can be true.
200201
assert(!checked || !indeterminate);
201202

@@ -214,11 +215,11 @@ class MaterialCheckboxComponent implements ControlValueAccessor, Focusable {
214215
? indeterminateIcon
215216
: _checked ? checkedIcon : uncheckedIcon;
216217

217-
if (_checked != prevChecked) {
218+
if (emitEvent && _checked != prevChecked) {
218219
_onChecked.add(_checked);
219220
}
220221

221-
if (_indeterminate != prevIndeterminate) {
222+
if (emitEvent && _indeterminate != prevIndeterminate) {
222223
_onIndeterminate.add(_indeterminate);
223224
}
224225

0 commit comments

Comments
 (0)