-
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
3 changed files
with
116 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
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,112 @@ | ||
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'; | ||
|
||
const String expandedIdsKey = 'fftv.restorableTreeController.expandedIds'; | ||
|
||
class RestorableTreeController extends TreeController<Node> { | ||
RestorableTreeController({ | ||
required super.roots, | ||
}) : super(childrenProvider: (Node node) => node.children); | ||
|
||
late final Set<String> expandedIds = <String>{}; | ||
|
||
void restoreExpandedIds(SharedPreferences prefs) { | ||
final List<String>? storedExpandedIds = prefs.getStringList(expandedIdsKey); | ||
|
||
if (storedExpandedIds != null) { | ||
expandedIds.addAll(storedExpandedIds); | ||
} | ||
} | ||
|
||
void persistExpandedIds(SharedPreferences prefs) { | ||
prefs.setStringList(expandedIdsKey, expandedIds.toList()); | ||
} | ||
|
||
@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; | ||
} | ||
|
||
class RestorableTreeView extends StatefulWidget { | ||
const RestorableTreeView({super.key}); | ||
|
||
@override | ||
State<RestorableTreeView> createState() => _RestorableTreeViewState(); | ||
} | ||
|
||
class _RestorableTreeViewState extends State<RestorableTreeView> { | ||
late final RestorableTreeController treeController; | ||
late final SharedPreferences prefs = context.read<SharedPreferences>(); | ||
|
||
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 = RestorableTreeController(roots: <Node>[root]); | ||
treeController.restoreExpandedIds(prefs); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
treeController.persistExpandedIds(prefs); | ||
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}'), | ||
), | ||
], | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
} |