Code Monkey home page Code Monkey logo

Comments (3)

smcdonald45 avatar smcdonald45 commented on May 17, 2024 1

Thx a lot. I will try this out. Im quite new to flutter, dart and bloc but it makes really fun ;D

from flutter_tree_view.

smcdonald45 avatar smcdonald45 commented on May 17, 2024 1

Wow, that was easy ;D Thx a lot. Your tree works really great with a really low amont of code!!

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_fancy_tree_view/flutter_fancy_tree_view.dart';
import 'package:picmac/tree/bloc/directory_tree_bloc.dart';

class TreeNodeData {
  TreeNodeData(this.path, this.title, {this.children});

  final String path;
  final String title;
  List<TreeNodeData>? children;
}

class DirectoryTreeView extends StatefulWidget {
  const DirectoryTreeView({super.key});

  @override
  State<DirectoryTreeView> createState() => _DirectoryTreeViewState();
}

class _DirectoryTreeViewState extends State<DirectoryTreeView> {
  late final TreeController<TreeNodeData> _treeController;

  @override
  void initState() {
    super.initState();

    // build initial empty tree controller. will get some roots later by the bloc
    _treeController = TreeController(
      roots: <TreeNodeData>[],
      childrenProvider: (node) => node.children ?? <TreeNodeData>[],
    );
  }

  @override
  Widget build(BuildContext context) {
    // the bloc listener only listen to refreshed states emitted by the bloc if a new directory
    // appears in the path somewhere
    return BlocListener<DirectoryTreeBloc, DirectoryTreeState>(
      listenWhen: (previous, current) {
        return current.status == DirectoryTreeStatus.refreshed;
      },
      listener: (context, state) {
        // update the complete tree
        _treeController.roots = [state.rootNode];
      },

      // as soon as the directories are loaded the success state is emitted and we can build
      // the directory tree with the final tree data
      child: BlocBuilder<DirectoryTreeBloc, DirectoryTreeState>(
        buildWhen: (previous, current) {
          return current.status == DirectoryTreeStatus.success;
        },
        builder: (context, state) {
          _treeController.roots = [state.rootNode];

          return Material(
            child: Column(
              children: [
                Expanded(
                  child: AnimatedTreeView(
                    treeController: _treeController,
                    nodeBuilder: (context, entry) {
                      return TreeIndentation(
                        entry: entry,
                        child: Row(
                          children: [
                            FolderButton(
                              splashRadius: 20,
                              isOpen: _treeController.getExpansionState(
                                entry.node,
                              ),
                              onPressed: () => _treeController.toggleExpansion(
                                entry.node,
                              ),
                            ),
                            Text(entry.node.title),
                          ],
                        ),
                      );
                    },
                  ),
                ),
                OutlinedButton(
                  onPressed: () => context.read<DirectoryTreeBloc>().add(
                        SimulateDirectoriesChangedEvent(),
                      ),
                  child: const Text('foo'),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}

from flutter_tree_view.

baumths avatar baumths commented on May 17, 2024

Hey @smcdonald45 thank you for using the package!

The first thing I see that I must point out is that you are creating a new TreeController each time the DirectoryTreeBloc emits a new state. The controller should live in a StatefulWidget or in your dependency injection tool (e.g., Provider).

With the TreeController lifted out you could add a BlocListener on your DirectoryTreeBloc to update the TreeController.roots variable when a new root is emitted (the roots setter of the controller will rebuild the tree implicitly for you).

Other than that, the snippet of code that you shared didn't include your TreeNodeData neither the function that is finding and populating the tree nodes so I don't know how to help you in that regard.

Everytime the tree changes in any way that is not a call to a method on TreeController, you must call TreeController.rebuild() to make sure the controller notifies the listening tree views that the tree structure changed and they should update their values (the flat tree).

Try the above and let me know if it helped!

remember to dispose() your TreeController 😉

from flutter_tree_view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.