menu_buttons.dart 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import 'dart:io' show Platform;
  2. import 'package:chicken_farm/stores/auth_store.dart';
  3. import 'package:chicken_farm/stores/menu_store.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_riverpod/flutter_riverpod.dart';
  6. import 'package:go_router/go_router.dart';
  7. class MenuButtons extends ConsumerWidget {
  8. const MenuButtons({super.key});
  9. @override
  10. Widget build(BuildContext context, WidgetRef ref) {
  11. final authState = ref.watch(authStoreProvider);
  12. final authStore = ref.watch(authStoreProvider.notifier);
  13. final isSuperAdmin = authStore.isSuperAdmin();
  14. // 获取当前平台类型
  15. // 平台类型,0:所有平台,1:Android手机,2:Windows
  16. int currentPlatform = 0;
  17. if (Platform.isAndroid) {
  18. currentPlatform = 1;
  19. } else if (Platform.isWindows) {
  20. currentPlatform = 2;
  21. }
  22. // 根据权限和平台类型过滤菜单项
  23. final visibleMenuItems = MenuStore.menuItems.where((item) {
  24. // 平台过滤
  25. if (item.platform != 0 && item.platform != currentPlatform) {
  26. return false;
  27. }
  28. // 权限过滤
  29. if (isSuperAdmin) return true;
  30. if (item.permission == null) return true;
  31. return authState.permissions?.contains(item.permission) ?? false;
  32. }).toList();
  33. final screenWidth = MediaQuery.of(context).size.width;
  34. final buttonWidth = screenWidth * 0.8;
  35. return SingleChildScrollView(
  36. child: Center(
  37. child: Column(
  38. mainAxisAlignment: MainAxisAlignment.center,
  39. children: [
  40. for (var i = 0; i < visibleMenuItems.length; i++) ...[
  41. SizedBox(
  42. width: buttonWidth,
  43. height: 50,
  44. child: ElevatedButton.icon(
  45. onPressed: () {
  46. Future.delayed(const Duration(milliseconds: 100), () {
  47. if (context.mounted) {
  48. context.pushNamed(visibleMenuItems[i].routeName);
  49. }
  50. });
  51. },
  52. icon: visibleMenuItems[i].icon != null
  53. ? Icon(visibleMenuItems[i].icon)
  54. : const SizedBox.shrink(),
  55. label: Text(visibleMenuItems[i].name),
  56. ),
  57. ),
  58. if (i < visibleMenuItems.length - 1) const SizedBox(height: 20),
  59. ],
  60. ],
  61. ),
  62. ),
  63. );
  64. }
  65. }