Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TreeView item add disabled option: Whether the current item is disabled #1023

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 95 additions & 1 deletion example/lib/screens/navigation/tree_view.dart
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

However, when the parent TreeViewItem is checked, all child TreeViewItems can still be selected.

In my application, if the parent TreeViewItem is disabled, then manually set the disabled property to true for all child elements.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can be misleading. Maybe adding this to the documentation OR creating a new property (maybe disableChildren) that will disable the children if the parent is disabled?

Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ TreeView(

// Do your fetching...
await Future.delayed(const Duration(seconds: 2));

// ...and add the fetched nodes.
item.children.addAll([
TreeViewItem(
Expand Down Expand Up @@ -219,6 +219,67 @@ TreeView(
},
),
),
subtitle(
content: const Text('A TreeView with checkbox disabled'),
),
CardHighlight(
codeSnippet: r'''late final disabledItems = [
TreeViewItem(
content: const Text('Personal Documents'),
value: 'personal_docs',
children: [
TreeViewItem(
content: const Text('Home Remodel'),
value: 'home_remodel',
disabled: true,
children: [
TreeViewItem(
content: const Text('Contractor Contact Info'),
value: 'contr_cont_inf',
),
TreeViewItem(
content: const Text('Paint Color Scheme'),
value: 'paint_color_scheme',
disabled: true,
),
],
),
TreeViewItem(
content: const Text('Tax Documents'),
value: 'tax_docs',
children: [
TreeViewItem(content: const Text('2017'), value: "tax_2017"),
TreeViewItem(content: const Text('Current Year'), value: "tax_cur"),
],
),
],
),
];

TreeView(
selectionMode: TreeViewSelectionMode.multiple,
items: disabledItems,
onItemInvoked: (item, reason) async =>
debugPrint('onItemInvoked(reason=$reason): $item'),
onSelectionChanged: (selectedItems) async => debugPrint(
'onSelectionChanged: ${selectedItems.map((i) => i.value)}'),
onSecondaryTap: (item, details) async {
debugPrint('onSecondaryTap $item at ${details.globalPosition}');
},
)
''',
child: TreeView(
selectionMode: TreeViewSelectionMode.multiple,
items: disabledItems,
onItemInvoked: (item, reason) async =>
debugPrint('onItemInvoked(reason=$reason): $item'),
onSelectionChanged: (selectedItems) async => debugPrint(
'onSelectionChanged: ${selectedItems.map((i) => i.value)}'),
onSecondaryTap: (item, details) async {
debugPrint('onSecondaryTap $item at ${details.globalPosition}');
},
),
),
],
);
}
Expand Down Expand Up @@ -316,4 +377,37 @@ TreeView(
},
),
];

late final disabledItems = [
TreeViewItem(
content: const Text('Personal Documents'),
value: 'personal_docs',
children: [
TreeViewItem(
content: const Text('Home Remodel'),
value: 'home_remodel',
disabled: true,
children: [
TreeViewItem(
content: const Text('Contractor Contact Info'),
value: 'contr_cont_inf',
),
TreeViewItem(
content: const Text('Paint Color Scheme'),
value: 'paint_color_scheme',
disabled: true,
),
],
),
TreeViewItem(
content: const Text('Tax Documents'),
value: 'tax_docs',
children: [
TreeViewItem(content: const Text('2017'), value: "tax_2017"),
TreeViewItem(content: const Text('Current Year'), value: "tax_cur"),
],
),
],
),
];
}
9 changes: 8 additions & 1 deletion lib/src/controls/navigation/tree_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ class TreeViewItem with Diagnosticable {
/// item is going to be one of the selected items
bool? selected;

/// Whether the current item is disabled.
///
bool disabled;

/// Called when this item is invoked
///
/// This item is passed to the callback.
Expand Down Expand Up @@ -210,6 +214,7 @@ class TreeViewItem with Diagnosticable {
this.collapsable = true,
bool? expanded,
this.selected = false,
this.disabled = false,
this.onInvoked,
this.onExpandToggle,
this.gestures = const {},
Expand Down Expand Up @@ -1031,7 +1036,9 @@ class _TreeViewItem extends StatelessWidget {
child: ExcludeFocus(
child: Checkbox(
checked: item.selected,
onChanged: (value) => _onCheckboxInvoked(),
onChanged: item.disabled ?? false
? null
: (value) => _onCheckboxInvoked(),
),
),
),
Expand Down