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

Implement Lazy Tree View #139

Merged
merged 2 commits into from
Jan 21, 2022
Merged

Implement Lazy Tree View #139

merged 2 commits into from
Jan 21, 2022

Conversation

bdlukaa
Copy link
Owner

@bdlukaa bdlukaa commented Jan 20, 2022

Fixes #135

Usage:

  late List<TreeViewItem> items;

  @override
  initState() {
    super.initState();
    items = [
      TreeViewItem(
        content: const Text('Work Documents'),
        lazy: true,
        onInvoked: (item) async {
          // if it's already populated, return
          if (item.children.isNotEmpty) return;
          // mark as loading
          setState(() => item.loading = true);
          // do your fetching...
          await Future.delayed(const Duration(seconds: 2));
          setState(() {
            item
              // set loading to false
              ..loading = false
              // add the fetched nodes
              ..children.addAll([
                TreeViewItem(content: const Text('XYZ Functional Spec')),
                TreeViewItem(content: const Text('Feature Schedule')),
                TreeViewItem(content: const Text('Overall Project Plan')),
                TreeViewItem(
                    content: const Text('Feature Resources Allocation')),
              ]);
          });
        },
        children: [],
      ),
      TreeViewItem(
        content: const Text('Personal Documents'),
        lazy: true,
        children: [
          TreeViewItem(
            content: const Text('Home Remodel'),
            children: [
              TreeViewItem(content: const Text('Contractor Contact Info')),
              TreeViewItem(content: const Text('Paint Color Scheme')),
              TreeViewItem(content: const Text('Flooring weedgrain type')),
              TreeViewItem(content: const Text('Kitchen cabinet style')),
            ],
          ),
        ],
      ),
    ];
  }

TreeView(
  items: items,
  onItemInvoked: (item) async => debugPrint('$item'),
),

Pre-launch Checklist

  • I have run dartfmt on all changed files
  • I have updated CHANGELOG.md with my changes
  • I have run "optimize/organize imports" on all changed files
  • I have addressed all analyzer warnings as best I could
  • I have added/updated relevant documentation
  • I have run flutter pub publish --dry-run and addressed any warnings

@timsneath
Copy link

Splendid! I wonder whether the loading state will flicker if the expansion only takes ~100ms?

In my scenario, no individual node takes very long to populate, it's just that the entire tree is too large to load. I'll try this out and report back.

@bdlukaa
Copy link
Owner Author

bdlukaa commented Jan 20, 2022

I wonder whether the loading state will flicker if the expansion only takes ~100ms?

Probably. The loading indicator can be customizable by providing loadingWidget on both TreeView and TreeViewItem if you wish a custom loading indicator!

@timsneath
Copy link

Ah, that probably solves my concern. Thanks!

@bdlukaa bdlukaa merged commit 3ed8386 into master Jan 21, 2022
@bdlukaa bdlukaa deleted the Lazy-tree-view branch January 21, 2022 16:38
@yepyeniceri
Copy link

When I tried this the "super.initState();" is being highlighted. I get the Errormessage "The method 'initState' isn't defined in a superclass of 'Testpage4'."
Testpage4 is an alteration of the example file "tree_view.dart"
What might I do wrong?

@bdlukaa
Copy link
Owner Author

bdlukaa commented Aug 9, 2022

the example tree_view isn't a widget, thefore there isn't a initState. You must do that when initializing the list

dev-hann added a commit to dev-hann/fluent_ui that referenced this pull request Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lazy expansion of TreeView nodes
3 participants