63 lines
1.4 KiB
Dart
63 lines
1.4 KiB
Dart
|
import 'package:flutter/widgets.dart';
|
||
|
|
||
|
class _BreadCrumbNode {
|
||
|
String name;
|
||
|
Widget widget;
|
||
|
_BreadCrumbNode? next;
|
||
|
|
||
|
_BreadCrumbNode(this.name, this.widget);
|
||
|
}
|
||
|
|
||
|
class BreadCrumbController extends ChangeNotifier {
|
||
|
final Widget defaultRoute;
|
||
|
final String rootName;
|
||
|
|
||
|
late final _BreadCrumbNode _rootPage =
|
||
|
_BreadCrumbNode(rootName, defaultRoute);
|
||
|
late Widget _currentpage = _rootPage.widget;
|
||
|
|
||
|
void pushPage(Widget widget, String name) {
|
||
|
_findPage(null, _rootPage).next = _BreadCrumbNode(name, widget);
|
||
|
_currentpage = widget;
|
||
|
notifyListeners();
|
||
|
}
|
||
|
|
||
|
void switchPage(String name) {
|
||
|
final pageNode = _findPage(name, _rootPage);
|
||
|
_currentpage = pageNode.widget;
|
||
|
// hope for garbage collector to free the floating items (;
|
||
|
pageNode.next = null;
|
||
|
notifyListeners();
|
||
|
}
|
||
|
|
||
|
List<String> getNames() {
|
||
|
return _genNameList(_rootPage);
|
||
|
}
|
||
|
|
||
|
Widget getCurrentPage() {
|
||
|
return _currentpage;
|
||
|
}
|
||
|
|
||
|
_BreadCrumbNode _findPage(String? name, _BreadCrumbNode node) {
|
||
|
if (node.name == name) {
|
||
|
return node;
|
||
|
}
|
||
|
|
||
|
if (node.next != null) {
|
||
|
return _findPage(name, node.next!);
|
||
|
} else {
|
||
|
return node;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
List<String> _genNameList(_BreadCrumbNode node) {
|
||
|
if (node.next != null) {
|
||
|
return _genNameList(node.next!)..insert(0, node.name);
|
||
|
} else {
|
||
|
return [node.name];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
BreadCrumbController(this.defaultRoute, this.rootName);
|
||
|
}
|