Unverified Commit f44f4335 authored by xubaolin's avatar xubaolin Committed by GitHub

Fix bug when tapping ListTitle with CheckboxListTile tristate enable (#63925)

* fix bug when tap ListTitle when tristate enable #63846

* fix bug when tap ListTitle when tristate enable #63846

* code style

* improve the unit test case

* bow to convention
parent 41e553bb
...@@ -382,6 +382,21 @@ class CheckboxListTile extends StatelessWidget { ...@@ -382,6 +382,21 @@ class CheckboxListTile extends StatelessWidget {
/// If tristate is false (the default), [value] must not be null. /// If tristate is false (the default), [value] must not be null.
final bool tristate; final bool tristate;
void _handleValueChange() {
assert(onChanged != null);
switch (value) {
case false:
onChanged(true);
break;
case true:
onChanged(tristate ? null : false);
break;
default: // case null:
onChanged(false);
break;
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final Widget control = Checkbox( final Widget control = Checkbox(
...@@ -416,7 +431,7 @@ class CheckboxListTile extends StatelessWidget { ...@@ -416,7 +431,7 @@ class CheckboxListTile extends StatelessWidget {
isThreeLine: isThreeLine, isThreeLine: isThreeLine,
dense: dense, dense: dense,
enabled: onChanged != null, enabled: onChanged != null,
onTap: onChanged != null ? () { onChanged(!value); } : null, onTap: onChanged != null ? _handleValueChange : null,
selected: selected, selected: selected,
autofocus: autofocus, autofocus: autofocus,
contentPadding: contentPadding, contentPadding: contentPadding,
......
...@@ -148,7 +148,8 @@ void main() { ...@@ -148,7 +148,8 @@ void main() {
}); });
testWidgets('CheckboxListTile tristate test', (WidgetTester tester) async { testWidgets('CheckboxListTile tristate test', (WidgetTester tester) async {
bool _value; bool _value = false;
bool _tristate = false;
await tester.pumpWidget( await tester.pumpWidget(
Material( Material(
...@@ -157,7 +158,7 @@ void main() { ...@@ -157,7 +158,7 @@ void main() {
return wrap( return wrap(
child: CheckboxListTile( child: CheckboxListTile(
title: const Text('Title'), title: const Text('Title'),
tristate: true, tristate: _tristate,
value: _value, value: _value,
onChanged: (bool value) { onChanged: (bool value) {
setState(() { setState(() {
...@@ -171,12 +172,33 @@ void main() { ...@@ -171,12 +172,33 @@ void main() {
), ),
); );
expect(tester.widget<Checkbox>(find.byType(Checkbox)).value, null); expect(tester.widget<Checkbox>(find.byType(Checkbox)).value, false);
// Tap the checkbox when tristate is disabled.
await tester.tap(find.byType(Checkbox));
await tester.pumpAndSettle();
expect(_value, true);
await tester.tap(find.byType(Checkbox)); await tester.tap(find.byType(Checkbox));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(_value, false); expect(_value, false);
// Tap the listTile when tristate is disabled.
await tester.tap(find.byType(ListTile));
await tester.pumpAndSettle();
expect(_value, true);
await tester.tap(find.byType(ListTile));
await tester.pumpAndSettle();
expect(_value, false);
// Enable tristate
_tristate = true;
await tester.pumpAndSettle();
expect(tester.widget<Checkbox>(find.byType(Checkbox)).value, false);
// Tap the checkbox when tristate is enabled.
await tester.tap(find.byType(Checkbox)); await tester.tap(find.byType(Checkbox));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(_value, true); expect(_value, true);
...@@ -184,5 +206,22 @@ void main() { ...@@ -184,5 +206,22 @@ void main() {
await tester.tap(find.byType(Checkbox)); await tester.tap(find.byType(Checkbox));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(_value, null); expect(_value, null);
await tester.tap(find.byType(Checkbox));
await tester.pumpAndSettle();
expect(_value, false);
// Tap the listTile when tristate is enabled.
await tester.tap(find.byType(ListTile));
await tester.pumpAndSettle();
expect(_value, true);
await tester.tap(find.byType(ListTile));
await tester.pumpAndSettle();
expect(_value, null);
await tester.tap(find.byType(ListTile));
await tester.pumpAndSettle();
expect(_value, false);
}); });
} }
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