side_nav.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. const VSideNavView({
  21. super.key,
  22. this.navId,
  23. required this.items,
  24. this.controller,
  25. this.leftFlex = 1,
  26. this.rightFlex = 2,
  27. });
  28. @override
  29. State<StatefulWidget> createState() => _VSideNavViewState();
  30. }
  31. class _VSideNavViewState extends State<VSideNavView> {
  32. late final VSideNavViewController controller;
  33. @override
  34. void initState() {
  35. controller = widget.controller ?? VSideNavViewController();
  36. _init();
  37. super.initState();
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. final sideView = _SideBar(
  42. controller: controller,
  43. items: widget.items,
  44. navId: widget.navId,
  45. );
  46. final navView = _NavView(
  47. controller: controller,
  48. items: widget.items,
  49. navId: widget.navId,
  50. );
  51. return Row(
  52. children: [
  53. Expanded(
  54. flex: widget.leftFlex,
  55. child: sideView,
  56. ),
  57. const VerticalDivider(width: 1),
  58. Expanded(
  59. flex: widget.rightFlex,
  60. child: navView,
  61. ),
  62. ],
  63. );
  64. }
  65. void _init() {
  66. if (widget.navId != null) {
  67. _initRoute();
  68. } else {
  69. _initFirstPage();
  70. }
  71. }
  72. void _initFirstPage() {
  73. final firstPageItem =
  74. widget.items.firstWhereOrNull((e) => e.pageBuilder != null);
  75. final builder = firstPageItem?.pageBuilder;
  76. controller.titleChangedEvent.emit(this, firstPageItem?.title);
  77. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  78. controller.pageChangedEvent.emit(this, builder);
  79. });
  80. }
  81. void _initRoute() {
  82. final firstRouteItem =
  83. widget.items.firstWhereOrNull((e) => e.route != null);
  84. final firstRoute = firstRouteItem?.route;
  85. controller.titleChangedEvent.emit(this, firstRoute?.name);
  86. }
  87. }