| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import 'dart:io' show Platform;
- import 'package:chicken_farm/stores/auth_store.dart';
- import 'package:chicken_farm/stores/menu_store.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:go_router/go_router.dart';
- class MenuButtons extends ConsumerWidget {
- const MenuButtons({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final authState = ref.watch(authStoreProvider);
- final authStore = ref.watch(authStoreProvider.notifier);
- final isSuperAdmin = authStore.isSuperAdmin();
- // 获取当前平台类型
- // 平台类型,0:所有平台,1:Android手机,2:Windows
- int currentPlatform = 0;
- if (Platform.isAndroid) {
- currentPlatform = 1;
- } else if (Platform.isWindows) {
- currentPlatform = 2;
- }
- // 根据权限和平台类型过滤菜单项
- final visibleMenuItems = MenuStore.menuItems.where((item) {
- // 平台过滤
- if (item.platform != 0 && item.platform != currentPlatform) {
- return false;
- }
-
- // 权限过滤
- if (isSuperAdmin) return true;
- if (item.permission == null) return true;
- return authState.permissions?.contains(item.permission) ?? false;
- }).toList();
- final screenWidth = MediaQuery.of(context).size.width;
- final buttonWidth = screenWidth * 0.8;
- return SingleChildScrollView(
- child: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- for (var i = 0; i < visibleMenuItems.length; i++) ...[
- SizedBox(
- width: buttonWidth,
- height: 50,
- child: ElevatedButton.icon(
- onPressed: () {
- Future.delayed(const Duration(milliseconds: 100), () {
- if (context.mounted) {
- context.pushNamed(visibleMenuItems[i].routeName);
- }
- });
- },
- icon: visibleMenuItems[i].icon != null
- ? Icon(visibleMenuItems[i].icon)
- : const SizedBox.shrink(),
- label: Text(visibleMenuItems[i].name),
- ),
- ),
- if (i < visibleMenuItems.length - 1) const SizedBox(height: 20),
- ],
- ],
- ),
- ),
- );
- }
- }
|