123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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<VSideNavMenuItem> items;
- final VSideNavViewController? controller;
- final int leftFlex;
- final int rightFlex;
- const VSideNavView({
- super.key,
- this.navId,
- required this.items,
- this.controller,
- this.leftFlex = 1,
- this.rightFlex = 2,
- });
- @override
- State<StatefulWidget> createState() => _VSideNavViewState();
- }
- class _VSideNavViewState extends State<VSideNavView> {
- 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,
- );
- 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);
- }
- }
|