diff --git a/packages/flutter/lib/src/material/checkbox_list_tile.dart b/packages/flutter/lib/src/material/checkbox_list_tile.dart index 4ee9a2c06b..3c91c128ae 100644 --- a/packages/flutter/lib/src/material/checkbox_list_tile.dart +++ b/packages/flutter/lib/src/material/checkbox_list_tile.dart @@ -127,6 +127,7 @@ class CheckboxListTile extends StatelessWidget { required this.onChanged, this.activeColor, this.checkColor, + this.enabled, this.tileColor, this.title, this.subtitle, @@ -295,6 +296,13 @@ class CheckboxListTile extends StatelessWidget { /// * [Feedback] for providing platform-specific feedback to certain actions. final bool? enableFeedback; + /// Whether the CheckboxListTile is interactive. + /// + /// If false, this list tile is styled with the disabled color from the + /// current [Theme] and the [ListTile.onTap] callback is + /// inoperative. + final bool? enabled; + void _handleValueChange() { assert(onChanged != null); switch (value) { @@ -314,7 +322,7 @@ class CheckboxListTile extends StatelessWidget { Widget build(BuildContext context) { final Widget control = Checkbox( value: value, - onChanged: onChanged, + onChanged: enabled ?? true ? onChanged : null , activeColor: activeColor, checkColor: checkColor, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, @@ -345,7 +353,7 @@ class CheckboxListTile extends StatelessWidget { trailing: trailing, isThreeLine: isThreeLine, dense: dense, - enabled: onChanged != null, + enabled: enabled ?? onChanged != null, onTap: onChanged != null ? _handleValueChange : null, selected: selected, autofocus: autofocus, diff --git a/packages/flutter/test/material/checkbox_list_tile_test.dart b/packages/flutter/test/material/checkbox_list_tile_test.dart index b4a1ca29dd..ee161a06fd 100644 --- a/packages/flutter/test/material/checkbox_list_tile_test.dart +++ b/packages/flutter/test/material/checkbox_list_tile_test.dart @@ -421,6 +421,47 @@ void main() { expect(tileNode.hasPrimaryFocus, isTrue); }); + testWidgets('CheckboxListTile can be disabled', (WidgetTester tester) async { + bool? value = false; + bool enabled = true; + + await tester.pumpWidget( + Material( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return wrap( + child: CheckboxListTile( + title: const Text('Title'), + enabled: enabled, + value: value, + onChanged: (bool? v) { + setState(() { + value = v; + enabled = !enabled; + }); + }, + ), + ); + }, + ), + ), + ); + + final Finder checkbox = find.byType(Checkbox); + // verify initial values + expect(tester.widget(checkbox).value, false); + expect(enabled, true); + + // Tap the checkbox to disable CheckboxListTile + await tester.tap(checkbox); + await tester.pumpAndSettle(); + expect(tester.widget(checkbox).value, true); + expect(enabled, false); + await tester.tap(checkbox); + await tester.pumpAndSettle(); + expect(tester.widget(checkbox).value, true); + }); + group('feedback', () { late FeedbackTester feedback;