forked from firka/flutter
Add enabled property to CheckboxlistTile (#102314)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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>(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', () {
|
||||
late FeedbackTester feedback;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user