Skip to content

Commit 38df107

Browse files
authored
Add enabled property to CheckboxlistTile (#102314)
1 parent d31c250 commit 38df107

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

packages/flutter/lib/src/material/checkbox_list_tile.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class CheckboxListTile extends StatelessWidget {
127127
required this.onChanged,
128128
this.activeColor,
129129
this.checkColor,
130+
this.enabled,
130131
this.tileColor,
131132
this.title,
132133
this.subtitle,
@@ -295,6 +296,13 @@ class CheckboxListTile extends StatelessWidget {
295296
/// * [Feedback] for providing platform-specific feedback to certain actions.
296297
final bool? enableFeedback;
297298

299+
/// Whether the CheckboxListTile is interactive.
300+
///
301+
/// If false, this list tile is styled with the disabled color from the
302+
/// current [Theme] and the [ListTile.onTap] callback is
303+
/// inoperative.
304+
final bool? enabled;
305+
298306
void _handleValueChange() {
299307
assert(onChanged != null);
300308
switch (value) {
@@ -314,7 +322,7 @@ class CheckboxListTile extends StatelessWidget {
314322
Widget build(BuildContext context) {
315323
final Widget control = Checkbox(
316324
value: value,
317-
onChanged: onChanged,
325+
onChanged: enabled ?? true ? onChanged : null ,
318326
activeColor: activeColor,
319327
checkColor: checkColor,
320328
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
@@ -345,7 +353,7 @@ class CheckboxListTile extends StatelessWidget {
345353
trailing: trailing,
346354
isThreeLine: isThreeLine,
347355
dense: dense,
348-
enabled: onChanged != null,
356+
enabled: enabled ?? onChanged != null,
349357
onTap: onChanged != null ? _handleValueChange : null,
350358
selected: selected,
351359
autofocus: autofocus,

packages/flutter/test/material/checkbox_list_tile_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,47 @@ void main() {
421421
expect(tileNode.hasPrimaryFocus, isTrue);
422422
});
423423

424+
testWidgets('CheckboxListTile can be disabled', (WidgetTester tester) async {
425+
bool? value = false;
426+
bool enabled = true;
427+
428+
await tester.pumpWidget(
429+
Material(
430+
child: StatefulBuilder(
431+
builder: (BuildContext context, StateSetter setState) {
432+
return wrap(
433+
child: CheckboxListTile(
434+
title: const Text('Title'),
435+
enabled: enabled,
436+
value: value,
437+
onChanged: (bool? v) {
438+
setState(() {
439+
value = v;
440+
enabled = !enabled;
441+
});
442+
},
443+
),
444+
);
445+
},
446+
),
447+
),
448+
);
449+
450+
final Finder checkbox = find.byType(Checkbox);
451+
// verify initial values
452+
expect(tester.widget<Checkbox>(checkbox).value, false);
453+
expect(enabled, true);
454+
455+
// Tap the checkbox to disable CheckboxListTile
456+
await tester.tap(checkbox);
457+
await tester.pumpAndSettle();
458+
expect(tester.widget<Checkbox>(checkbox).value, true);
459+
expect(enabled, false);
460+
await tester.tap(checkbox);
461+
await tester.pumpAndSettle();
462+
expect(tester.widget<Checkbox>(checkbox).value, true);
463+
});
464+
424465
group('feedback', () {
425466
late FeedbackTester feedback;
426467

0 commit comments

Comments
 (0)