import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:vitalapp/components/cell.dart'; import 'package:vitalapp/consts/styles.dart'; import 'package:vitalapp/routes/transition.dart'; import 'controller.dart'; import 'defines.dart'; part 'nav_view.dart'; part 'side_bar.dart'; class VSideNavView extends StatefulWidget { /// 导航Id /// /// 设为`null`时,不使用路由模式,通过`VSideNavMenuItem.pageBuilder`构建页面 final int? navId; /// 菜单项集合 final List items; final VSideNavViewController? controller; final int leftFlex; final int rightFlex; final bool isRemoveSideView; const VSideNavView({ super.key, this.navId, required this.items, this.controller, this.leftFlex = 1, this.rightFlex = 2, this.isRemoveSideView = false, }); @override State createState() => _VSideNavViewState(); } class _VSideNavViewState extends State { late final VSideNavViewController controller; @override void initState() { controller = widget.controller ?? VSideNavViewController(); _init(); super.initState(); } @override Widget build(BuildContext context) { final sideView = _SideBar( controller: controller, items: widget.items, navId: widget.navId, ); final navView = _NavView( controller: controller, items: widget.items, navId: widget.navId, ); if (widget.isRemoveSideView) { return navView; } return Row( children: [ Expanded( flex: widget.leftFlex, child: sideView, ), const VerticalDivider(width: 1), Expanded( flex: widget.rightFlex, child: navView, ), ], ); } void _init() { if (widget.navId != null) { _initRoute(); } else { _initFirstPage(); } } void _initFirstPage() { final firstPageItem = widget.items.firstWhereOrNull((e) => e.pageBuilder != null); final builder = firstPageItem?.pageBuilder; controller.titleChangedEvent.emit(this, firstPageItem?.title); WidgetsBinding.instance.addPostFrameCallback((timeStamp) { controller.pageChangedEvent.emit(this, builder); }); } void _initRoute() { final firstRouteItem = widget.items.firstWhereOrNull((e) => e.route != null); final firstRoute = firstRouteItem?.route; controller.titleChangedEvent.emit(this, firstRoute?.name); } }