-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add expansion state restoration example
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_fancy_tree_view/flutter_fancy_tree_view.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
import '../settings/controller.dart'; | ||
|
||
class MyTreeController extends TreeController<Node> { | ||
MyTreeController({ | ||
required super.roots, | ||
required this.expandedIds, | ||
}) : super(childrenProvider: (Node node) => node.children); | ||
|
||
final Set<String> expandedIds; | ||
|
||
@override | ||
bool getExpansionState(Node node) => expandedIds.contains(node.id); | ||
|
||
@override | ||
void setExpansionState(Node node, bool expanded) { | ||
expanded ? expandedIds.add(node.id) : expandedIds.remove(node.id); | ||
} | ||
} | ||
|
||
class Node { | ||
Node({required this.id, this.children = const <Node>[]}); | ||
|
||
final String id; | ||
final List<Node> children; | ||
|
||
@override | ||
int get hashCode => id.hashCode; | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) return true; | ||
return other is Node && other.id == id; | ||
} | ||
} | ||
|
||
class RestorableTreeView extends StatefulWidget { | ||
const RestorableTreeView({super.key}); | ||
|
||
@override | ||
State<RestorableTreeView> createState() => _RestorableTreeViewState(); | ||
} | ||
|
||
class _RestorableTreeViewState extends State<RestorableTreeView> { | ||
static const String expandedIdsKey = 'fftv.restorableTreeView.expandedIds'; | ||
|
||
late final MyTreeController treeController; | ||
late final SharedPreferences prefs = context.read<SettingsController>().prefs; | ||
|
||
final Node root = Node(id: '0', children: [ | ||
Node(id: '1', children: [ | ||
Node(id: '2', children: [ | ||
Node(id: '3', children: [ | ||
Node(id: '4'), | ||
Node(id: '5', children: [ | ||
Node(id: '6'), | ||
Node(id: '7', children: [ | ||
Node(id: '8'), | ||
]), | ||
]), | ||
]), | ||
Node(id: '9'), | ||
]), | ||
Node(id: '10'), | ||
]), | ||
Node(id: '11'), | ||
]); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
treeController = MyTreeController( | ||
roots: <Node>[root], | ||
// Restore expanded ids | ||
expandedIds: <String>{...?prefs.getStringList(expandedIdsKey)}, | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
// Persist expanded ids | ||
prefs.setStringList( | ||
expandedIdsKey, | ||
treeController.expandedIds.toList(), | ||
); | ||
treeController.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return TreeView<Node>( | ||
treeController: treeController, | ||
nodeBuilder: (BuildContext context, TreeEntry<Node> entry) { | ||
return TreeIndentation( | ||
entry: entry, | ||
child: Row( | ||
children: [ | ||
FolderButton( | ||
key: Key('FolderButton#${entry.node.id}'), | ||
isOpen: entry.hasChildren ? entry.isExpanded : null, | ||
onPressed: entry.hasChildren | ||
? () => treeController.toggleExpansion(entry.node) | ||
: null, | ||
), | ||
Flexible( | ||
child: Text('Node #${entry.node.id}'), | ||
), | ||
], | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
} |