From 91e7678f79d82f3cca96380c815e5d2845697487 Mon Sep 17 00:00:00 2001 From: William Oprandi Date: Thu, 4 Jun 2020 01:13:01 +0200 Subject: [PATCH] Allow null value for CheckboxListTile (#58154) --- .../lib/src/material/checkbox_list_tile.dart | 26 ++++++++++--- .../material/checkbox_list_tile_test.dart | 39 +++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/checkbox_list_tile.dart b/packages/flutter/lib/src/material/checkbox_list_tile.dart index 1a829f5d8c..8dc7c99e3f 100644 --- a/packages/flutter/lib/src/material/checkbox_list_tile.dart +++ b/packages/flutter/lib/src/material/checkbox_list_tile.dart @@ -248,11 +248,12 @@ class CheckboxListTile extends StatelessWidget { /// /// The following arguments are required: /// - /// * [value], which determines whether the checkbox is checked, and must not - /// be null. - /// + /// * [value], which determines whether the checkbox is checked. The [value] + /// can only be null if [tristate] is true. /// * [onChanged], which is called when the value of the checkbox should /// change. It can be set to null to disable the checkbox. + /// + /// The value of [tristate] must not be null. const CheckboxListTile({ Key key, @required this.value, @@ -268,7 +269,9 @@ class CheckboxListTile extends StatelessWidget { this.controlAffinity = ListTileControlAffinity.platform, this.autofocus = false, this.contentPadding, - }) : assert(value != null), + this.tristate = false, + }) : assert(tristate != null), + assert(tristate || value != null), assert(isThreeLine != null), assert(!isThreeLine || subtitle != null), assert(selected != null), @@ -277,8 +280,6 @@ class CheckboxListTile extends StatelessWidget { super(key: key); /// Whether this checkbox is checked. - /// - /// This property must not be null. final bool value; /// Called when the value of the checkbox should change. @@ -365,6 +366,18 @@ class CheckboxListTile extends StatelessWidget { /// When the value is null, the `contentPadding` is `EdgeInsets.symmetric(horizontal: 16.0)`. final EdgeInsetsGeometry contentPadding; + /// If true the checkbox's [value] can be true, false, or null. + /// + /// Checkbox displays a dash when its value is null. + /// + /// When a tri-state checkbox ([tristate] is true) is tapped, its [onChanged] + /// callback will be applied to true if the current value is false, to null if + /// value is true, and to false if value is null (i.e. it cycles through false + /// => true => null => false when tapped). + /// + /// If tristate is false (the default), [value] must not be null. + final bool tristate; + @override Widget build(BuildContext context) { final Widget control = Checkbox( @@ -374,6 +387,7 @@ class CheckboxListTile extends StatelessWidget { checkColor: checkColor, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, autofocus: autofocus, + tristate: tristate, ); Widget leading, trailing; switch (controlAffinity) { diff --git a/packages/flutter/test/material/checkbox_list_tile_test.dart b/packages/flutter/test/material/checkbox_list_tile_test.dart index 5c0eb9b85d..c2a10f1cd6 100644 --- a/packages/flutter/test/material/checkbox_list_tile_test.dart +++ b/packages/flutter/test/material/checkbox_list_tile_test.dart @@ -144,4 +144,43 @@ void main() { expect(paddingRect.top, tallerWidget.top - remainingHeight / 2 - 18); expect(paddingRect.bottom, tallerWidget.bottom + remainingHeight / 2 + 2); }); + + testWidgets('CheckboxListTile tristate test', (WidgetTester tester) async { + bool _value; + + await tester.pumpWidget( + Material( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return wrap( + child: CheckboxListTile( + title: const Text('Title'), + tristate: true, + value: _value, + onChanged: (bool value) { + setState(() { + _value = value; + }); + }, + ), + ); + }, + ), + ), + ); + + expect(tester.widget(find.byType(Checkbox)).value, null); + + await tester.tap(find.byType(Checkbox)); + await tester.pumpAndSettle(); + expect(_value, false); + + await tester.tap(find.byType(Checkbox)); + await tester.pumpAndSettle(); + expect(_value, true); + + await tester.tap(find.byType(Checkbox)); + await tester.pumpAndSettle(); + expect(_value, null); + }); }