side_nav.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:vitalapp/components/cell.dart';
  4. import 'package:vitalapp/consts/styles.dart';
  5. import 'package:vitalapp/routes/transition.dart';
  6. import 'controller.dart';
  7. import 'defines.dart';
  8. part 'nav_view.dart';
  9. part 'side_bar.dart';
  10. class VSideNavView extends StatefulWidget {
  11. /// 导航Id
  12. ///
  13. /// 设为`null`时,不使用路由模式,通过`VSideNavMenuItem.pageBuilder`构建页面
  14. final int? navId;
  15. /// 菜单项集合
  16. final List<VSideNavMenuItem> items;
  17. final VSideNavViewController? controller;
  18. final int leftFlex;
  19. final int rightFlex;
  20. final bool isRemoveSideView;
  21. const VSideNavView({
  22. super.key,
  23. this.navId,
  24. required this.items,
  25. this.controller,
  26. this.leftFlex = 1,
  27. this.rightFlex = 2,
  28. this.isRemoveSideView = false,
  29. });
  30. @override
  31. State<StatefulWidget> createState() => _VSideNavViewState();
  32. }
  33. class _VSideNavViewState extends State<VSideNavView> {
  34. late final VSideNavViewController controller;
  35. @override
  36. void initState() {
  37. controller = widget.controller ?? VSideNavViewController();
  38. _init();
  39. super.initState();
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. final sideView = _SideBar(
  44. controller: controller,
  45. items: widget.items,
  46. navId: widget.navId,
  47. );
  48. final navView = _NavView(
  49. controller: controller,
  50. items: widget.items,
  51. navId: widget.navId,
  52. );
  53. if (widget.isRemoveSideView) {
  54. return navView;
  55. }
  56. return Row(
  57. children: [
  58. Expanded(
  59. flex: widget.leftFlex,
  60. child: sideView,
  61. ),
  62. const VerticalDivider(width: 1),
  63. Expanded(
  64. flex: widget.rightFlex,
  65. child: navView,
  66. ),
  67. ],
  68. );
  69. }
  70. void _init() {
  71. if (widget.navId != null) {
  72. _initRoute();
  73. } else {
  74. _initFirstPage();
  75. }
  76. }
  77. void _initFirstPage() {
  78. final firstPageItem =
  79. widget.items.firstWhereOrNull((e) => e.pageBuilder != null);
  80. final builder = firstPageItem?.pageBuilder;
  81. controller.titleChangedEvent.emit(this, firstPageItem?.title);
  82. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  83. controller.pageChangedEvent.emit(this, builder);
  84. });
  85. }
  86. void _initRoute() {
  87. final firstRouteItem =
  88. widget.items.firstWhereOrNull((e) => e.route != null);
  89. final firstRoute = firstRouteItem?.route;
  90. controller.titleChangedEvent.emit(this, firstRoute?.name);
  91. }
  92. }