Unverified Commit 38df107b authored by Mahesh Jamdade's avatar Mahesh Jamdade Committed by GitHub

Add enabled property to CheckboxlistTile (#102314)

parent d31c250e
...@@ -127,6 +127,7 @@ class CheckboxListTile extends StatelessWidget { ...@@ -127,6 +127,7 @@ class CheckboxListTile extends StatelessWidget {
required this.onChanged, required this.onChanged,
this.activeColor, this.activeColor,
this.checkColor, this.checkColor,
this.enabled,
this.tileColor, this.tileColor,
this.title, this.title,
this.subtitle, this.subtitle,
...@@ -295,6 +296,13 @@ class CheckboxListTile extends StatelessWidget { ...@@ -295,6 +296,13 @@ class CheckboxListTile extends StatelessWidget {
/// * [Feedback] for providing platform-specific feedback to certain actions. /// * [Feedback] for providing platform-specific feedback to certain actions.
final bool? enableFeedback; 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() { void _handleValueChange() {
assert(onChanged != null); assert(onChanged != null);
switch (value) { switch (value) {
...@@ -314,7 +322,7 @@ class CheckboxListTile extends StatelessWidget { ...@@ -314,7 +322,7 @@ class CheckboxListTile extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final Widget control = Checkbox( final Widget control = Checkbox(
value: value, value: value,
onChanged: onChanged, onChanged: enabled ?? true ? onChanged : null ,
activeColor: activeColor, activeColor: activeColor,
checkColor: checkColor, checkColor: checkColor,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
...@@ -345,7 +353,7 @@ class CheckboxListTile extends StatelessWidget { ...@@ -345,7 +353,7 @@ class CheckboxListTile extends StatelessWidget {
trailing: trailing, trailing: trailing,
isThreeLine: isThreeLine, isThreeLine: isThreeLine,
dense: dense, dense: dense,
enabled: onChanged != null, enabled: enabled ?? onChanged != null,
onTap: onChanged != null ? _handleValueChange : null, onTap: onChanged != null ? _handleValueChange : null,
selected: selected, selected: selected,
autofocus: autofocus, autofocus: autofocus,
......
...@@ -421,6 +421,47 @@ void main() { ...@@ -421,6 +421,47 @@ void main() {
expect(tileNode.hasPrimaryFocus, isTrue); 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>(checkbox).value, false);
expect(enabled, true);
// Tap the checkbox to disable CheckboxListTile
await tester.tap(checkbox);
await tester.pumpAndSettle();
expect(tester.widget<Checkbox>(checkbox).value, true);
expect(enabled, false);
await tester.tap(checkbox);
await tester.pumpAndSettle();
expect(tester.widget<Checkbox>(checkbox).value, true);
});
group('feedback', () { group('feedback', () {
late FeedbackTester feedback; late FeedbackTester feedback;
......
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