Comments (3)
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.
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.
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()
yourTreeController
😉
from flutter_tree_view.
Related Issues (20)
- Migration guide for new version package is required HOT 11
- `TreeController.collapseCascading()` has a funky behavior when paired with `SliverAnimatedTree`
- `TreeView` and `AnimatedTreeView` does not refresh on `treeController` change HOT 1
- Best Way to Scroll Horizontally and Vertically HOT 2
- Adding a vertical scroll bar to the tree HOT 6
- How to change solid line to dashed line HOT 13
- When adding a child node to a node, setState() does not take effect, and only when expanding the node again can the newly added node be displayed. HOT 2
- How to use provider (riverpod) in the childrenProvider HOT 10
- Is it possible to have no interaction ? HOT 1
- Both horizontal and vertical scroll? HOT 9
- Selected node not highlighted HOT 3
- How to dynamically load tree for selected root? HOT 3
- how make search field to filter tree data HOT 2
- how to collapse all the children if parent collapse HOT 2
- [Help] DataTable inside tree, when node !hasChildren HOT 2
- how to fetch and load node data on tap HOT 4
- Add children on tap for given node HOT 1
- How to have tree fully expanded at start and on refresh? HOT 2
- Re-ordering and Moving Nodes HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_tree_view.