nav_view.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. part of "./side_nav.dart";
  2. class _NavView extends StatefulWidget {
  3. /// 导航Id
  4. final int? navId;
  5. /// 菜单项集合
  6. final List<VSideNavMenuItem> items;
  7. final VSideNavViewController controller;
  8. const _NavView({
  9. this.navId,
  10. required this.items,
  11. required this.controller,
  12. });
  13. @override
  14. State<StatefulWidget> createState() => _NavViewState();
  15. }
  16. class NavigatorObserverS extends NavigatorObserver {}
  17. class _NavViewState extends State<_NavView> {
  18. late final WidgetBuilder _bodyBuilder;
  19. WidgetBuilder? _currentPageBuilder;
  20. @override
  21. void initState() {
  22. if (widget.navId != null) {
  23. _bodyBuilder = _buildNavigator;
  24. } else {
  25. _bodyBuilder = _buildPageBody;
  26. widget.controller.pageChangedEvent.addListener(_onPageChanged);
  27. }
  28. super.initState();
  29. }
  30. @override
  31. void dispose() {
  32. widget.controller.pageChangedEvent.removeListener(_onPageChanged);
  33. super.dispose();
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. return Container(
  38. child: _bodyBuilder.call(context),
  39. );
  40. }
  41. Widget _buildPageBody(BuildContext context) {
  42. return _currentPageBuilder?.call(context) ?? const SizedBox();
  43. }
  44. Widget _buildNavigator(BuildContext context) {
  45. return Navigator(
  46. key: Get.nestedKey(widget.navId),
  47. initialRoute: widget.controller.currentTitle,
  48. onGenerateRoute: onGenerateRoute,
  49. );
  50. }
  51. void _onPageChanged(_, WidgetBuilder? e) {
  52. _currentPageBuilder = e;
  53. setState(() {});
  54. }
  55. /// 路由生成器
  56. Route? onGenerateRoute(RouteSettings settings) {
  57. String? name = settings.name;
  58. if (name == null) return null;
  59. final item = widget.items.firstWhereOrNull((e) => e.title == name);
  60. if (item == null) return null;
  61. final route = item.route!;
  62. widget.controller.titleChangedEvent.emit(this, item.title);
  63. return GetPageRoute(
  64. page: route.page,
  65. binding: route.binding,
  66. customTransition: FTransitions.sharedAxisHorizontal,
  67. transitionDuration: GlobalStyles.navTransitionDuration,
  68. );
  69. }
  70. }