admin_permissions_page.dart 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:go_router/go_router.dart';
  4. import 'package:flutter_riverpod/flutter_riverpod.dart';
  5. import '../../core/theme/app_colors_extension.dart';
  6. import '../../core/i18n/app_localizations.dart';
  7. import '../../shared/widgets/nav_bar_config.dart';
  8. /// 权限管理 - 页面3.2 【管理员专属】
  9. class AdminPermissionsPage extends ConsumerStatefulWidget {
  10. const AdminPermissionsPage({super.key});
  11. @override
  12. ConsumerState<AdminPermissionsPage> createState() =>
  13. _AdminPermissionsPageState();
  14. }
  15. class _AdminPermissionsPageState extends ConsumerState<AdminPermissionsPage> {
  16. final _searchCtrl = TextEditingController();
  17. Timer? _debounce;
  18. String _searchQuery = '';
  19. // 模拟当前登录用户(自保护用)
  20. static const _currentUserId = '0001';
  21. final _employees = <_Employee>[
  22. _Employee(
  23. name: '张三',
  24. avatarText: '张',
  25. employeeId: '0048',
  26. department: '销售部',
  27. roles: ['普通员工', '审批人'],
  28. isActive: true,
  29. ),
  30. _Employee(
  31. name: '王经理',
  32. avatarText: '王',
  33. employeeId: '0012',
  34. department: '销售部',
  35. roles: ['审批人', '系统管理员'],
  36. isActive: true,
  37. ),
  38. _Employee(
  39. name: '李会计',
  40. avatarText: '李',
  41. employeeId: '0025',
  42. department: '财务部',
  43. roles: ['财务人员'],
  44. isActive: true,
  45. ),
  46. _Employee(
  47. name: '赵管理员',
  48. avatarText: '赵',
  49. employeeId: '0001',
  50. department: '信息技术部',
  51. roles: ['系统管理员'],
  52. isActive: true,
  53. ),
  54. _Employee(
  55. name: '钱六',
  56. avatarText: '钱',
  57. employeeId: '0052',
  58. department: '财务部',
  59. roles: ['财务人员'],
  60. isActive: false,
  61. ),
  62. _Employee(
  63. name: '孙七',
  64. avatarText: '孙',
  65. employeeId: '0078',
  66. department: '行政部',
  67. roles: ['普通员工'],
  68. isActive: true,
  69. ),
  70. _Employee(
  71. name: '周八',
  72. avatarText: '周',
  73. employeeId: '0091',
  74. department: '技术部',
  75. roles: ['普通员工'],
  76. isActive: true,
  77. ),
  78. ];
  79. List<_Employee> get _filteredEmployees {
  80. if (_searchQuery.isEmpty) return _employees;
  81. final q = _searchQuery.toLowerCase();
  82. return _employees.where((e) {
  83. return e.name.toLowerCase().contains(q) ||
  84. e.employeeId.toLowerCase().contains(q);
  85. }).toList();
  86. }
  87. @override
  88. void initState() {
  89. super.initState();
  90. }
  91. @override
  92. void dispose() {
  93. _searchCtrl.dispose();
  94. _debounce?.cancel();
  95. super.dispose();
  96. }
  97. void _onSearchChanged(String value) {
  98. _debounce?.cancel();
  99. _debounce = Timer(const Duration(milliseconds: 300), () {
  100. if (mounted) {
  101. setState(() => _searchQuery = value);
  102. }
  103. });
  104. }
  105. @override
  106. Widget build(BuildContext context) {
  107. //final colors = Theme.of(context).extension<AppColorsExtension>()!;
  108. final l10n = AppLocalizations.of(context);
  109. setNavBarTitle(context, ref, NavBarConfig(
  110. title: l10n.get('permissionManagement'),
  111. showBack: true,
  112. onBack: () => context.pop(),
  113. ));
  114. return Column(
  115. children: [
  116. _buildSearchBar(),
  117. Expanded(
  118. child: ListView.builder(
  119. padding: const EdgeInsets.all(16),
  120. itemCount: _filteredEmployees.length,
  121. itemBuilder: (_, i) => _buildEmpCard(_filteredEmployees[i]),
  122. ),
  123. ),
  124. ],
  125. );
  126. }
  127. // ── 搜索栏(300ms 防抖) ──
  128. Widget _buildSearchBar() {
  129. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  130. return Container(
  131. width: double.infinity,
  132. padding: const EdgeInsets.fromLTRB(16, 10, 16, 10),
  133. color: colors.bgCard,
  134. child: Container(
  135. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
  136. decoration: BoxDecoration(
  137. color: colors.bgPage,
  138. borderRadius: BorderRadius.circular(18),
  139. border: Border.all(color: colors.border),
  140. ),
  141. child: Row(
  142. children: [
  143. Icon(Icons.search, size: 18, color: colors.textPlaceholder),
  144. const SizedBox(width: 6),
  145. Expanded(
  146. child: TextField(
  147. controller: _searchCtrl,
  148. onChanged: _onSearchChanged,
  149. decoration: InputDecoration(
  150. hintText: '输入姓名或工号进行检索...',
  151. hintStyle: TextStyle(
  152. fontSize: 14,
  153. color: colors.textPlaceholder,
  154. ),
  155. border: InputBorder.none,
  156. contentPadding: EdgeInsets.symmetric(vertical: 10),
  157. isDense: true,
  158. ),
  159. style: TextStyle(fontSize: 14, color: colors.textPrimary),
  160. ),
  161. ),
  162. if (_searchCtrl.text.isNotEmpty)
  163. GestureDetector(
  164. onTap: () {
  165. _searchCtrl.clear();
  166. _onSearchChanged('');
  167. setState(() => _searchQuery = '');
  168. },
  169. child: Icon(
  170. Icons.clear,
  171. size: 16,
  172. color: colors.textPlaceholder,
  173. ),
  174. ),
  175. ],
  176. ),
  177. ),
  178. );
  179. }
  180. // ── 员工卡片 ──
  181. Widget _buildEmpCard(_Employee emp) {
  182. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  183. return Padding(
  184. padding: const EdgeInsets.only(bottom: 12),
  185. child: GestureDetector(
  186. onTap: () => _openPermissionDrawer(emp),
  187. child: Container(
  188. padding: const EdgeInsets.all(12),
  189. decoration: BoxDecoration(
  190. color: emp.isActive ? colors.bgCard : colors.bgDisabled,
  191. borderRadius: BorderRadius.circular(8),
  192. boxShadow: const [
  193. BoxShadow(
  194. color: Color(0x08000000),
  195. blurRadius: 4,
  196. offset: Offset(0, 1),
  197. ),
  198. ],
  199. ),
  200. child: Row(
  201. crossAxisAlignment: CrossAxisAlignment.start,
  202. children: [
  203. // 头像
  204. Container(
  205. width: 40,
  206. height: 40,
  207. decoration: BoxDecoration(
  208. color: colors.primary,
  209. borderRadius: BorderRadius.circular(20),
  210. ),
  211. child: Center(
  212. child: Text(
  213. emp.avatarText,
  214. style: const TextStyle(
  215. fontSize: 16,
  216. fontWeight: FontWeight.w600,
  217. color: Colors.white,
  218. ),
  219. ),
  220. ),
  221. ),
  222. const SizedBox(width: 10),
  223. // 信息区
  224. Expanded(
  225. child: Column(
  226. crossAxisAlignment: CrossAxisAlignment.start,
  227. children: [
  228. Row(
  229. children: [
  230. Text(
  231. emp.name,
  232. style: TextStyle(
  233. fontSize: 15,
  234. fontWeight: FontWeight.w600,
  235. color: colors.textPrimary,
  236. ),
  237. ),
  238. const SizedBox(width: 6),
  239. Text(
  240. '工号:${emp.employeeId}',
  241. style: TextStyle(
  242. fontSize: 12,
  243. color: colors.textPlaceholder,
  244. ),
  245. ),
  246. ],
  247. ),
  248. const SizedBox(height: 2),
  249. Text(
  250. emp.department,
  251. style: TextStyle(
  252. fontSize: 12,
  253. color: colors.textSecondary,
  254. ),
  255. ),
  256. ],
  257. ),
  258. ),
  259. // 角色标签区
  260. Column(
  261. crossAxisAlignment: CrossAxisAlignment.end,
  262. children: [
  263. ...emp.roles.map(
  264. (role) => Padding(
  265. padding: const EdgeInsets.only(bottom: 4),
  266. child: _buildRoleTag(role),
  267. ),
  268. ),
  269. if (!emp.isActive)
  270. Container(
  271. padding: const EdgeInsets.symmetric(
  272. horizontal: 6,
  273. vertical: 2,
  274. ),
  275. decoration: BoxDecoration(
  276. color: colors.dangerBg,
  277. borderRadius: BorderRadius.circular(3),
  278. ),
  279. child: Text(
  280. '已禁用',
  281. style: TextStyle(fontSize: 10, color: colors.danger),
  282. ),
  283. ),
  284. ],
  285. ),
  286. ],
  287. ),
  288. ),
  289. ),
  290. );
  291. }
  292. Widget _buildRoleTag(String role) {
  293. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  294. Color bgColor;
  295. Color textColor;
  296. switch (role) {
  297. case '审批人':
  298. bgColor = colors.warningBg;
  299. textColor = colors.warning;
  300. break;
  301. case '财务人员':
  302. bgColor = colors.successBg;
  303. textColor = colors.success;
  304. break;
  305. case '系统管理员':
  306. bgColor = colors.dangerBg;
  307. textColor = colors.danger;
  308. break;
  309. default:
  310. bgColor = colors.primaryLight;
  311. textColor = colors.primary;
  312. }
  313. return Container(
  314. padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
  315. decoration: BoxDecoration(
  316. color: bgColor,
  317. borderRadius: BorderRadius.circular(3),
  318. ),
  319. child: Text(role, style: TextStyle(fontSize: 10, color: textColor)),
  320. );
  321. }
  322. // ── 权限抽屉(右侧滑出) ──
  323. void _openPermissionDrawer(_Employee emp) {
  324. // 深拷贝当前权限状态
  325. final checked = <String, bool>{};
  326. for (final perm in _allPermissions) {
  327. checked[perm.id] = _getDefaultPerms(emp.roles).contains(perm.id);
  328. }
  329. showGeneralDialog(
  330. context: context,
  331. barrierDismissible: true,
  332. barrierLabel: '',
  333. barrierColor: Colors.black45,
  334. transitionDuration: const Duration(milliseconds: 300),
  335. pageBuilder: (ctx, anim1, anim2) {
  336. return _PermissionDrawer(
  337. employee: emp,
  338. checked: checked,
  339. currentUserId: _currentUserId,
  340. onSave: () {
  341. Navigator.of(ctx).pop();
  342. ScaffoldMessenger.of(context).showSnackBar(
  343. const SnackBar(
  344. content: Text('权限已更新'),
  345. duration: Duration(seconds: 2),
  346. ),
  347. );
  348. },
  349. onCancel: () => Navigator.of(ctx).pop(),
  350. );
  351. },
  352. transitionBuilder: (ctx, anim, secondaryAnim, child) {
  353. return SlideTransition(
  354. position: Tween<Offset>(
  355. begin: const Offset(1.0, 0.0),
  356. end: Offset.zero,
  357. ).animate(CurvedAnimation(parent: anim, curve: Curves.easeOutCubic)),
  358. child: child,
  359. );
  360. },
  361. );
  362. }
  363. }
  364. // ── 权限数据定义 ──
  365. class _PermModule {
  366. final String name;
  367. final List<_PermItem> items;
  368. const _PermModule({required this.name, required this.items});
  369. }
  370. class _PermItem {
  371. final String id;
  372. final String label;
  373. const _PermItem({required this.id, required this.label});
  374. }
  375. const _allPermissions = <_PermItem>[
  376. _PermItem(id: 'expense.apply', label: '发起报销'),
  377. _PermItem(id: 'expense.view_own', label: '查看本人报销'),
  378. _PermItem(id: 'expense.view_dept', label: '查看部门报销'),
  379. _PermItem(id: 'expense.view_all', label: '查看全公司报销'),
  380. _PermItem(id: 'expense.approve', label: '审批报销'),
  381. _PermItem(id: 'expense.mark_paid', label: '确认付款'),
  382. _PermItem(id: 'expense.export', label: '导出报销数据'),
  383. _PermItem(id: 'preapply.apply', label: '发起事前申请'),
  384. _PermItem(id: 'preapply.view_own', label: '查看本人申请'),
  385. _PermItem(id: 'preapply.view_dept', label: '查看部门申请'),
  386. _PermItem(id: 'preapply.approve', label: '审批事前申请'),
  387. _PermItem(id: 'overtime.apply', label: '发起加班'),
  388. _PermItem(id: 'overtime.view_own', label: '查看本人加班'),
  389. _PermItem(id: 'overtime.view_dept', label: '查看部门加班'),
  390. _PermItem(id: 'overtime.approve', label: '审批加班'),
  391. _PermItem(id: 'vehicle.apply', label: '发起用车'),
  392. _PermItem(id: 'vehicle.view_own', label: '查看本人用车'),
  393. _PermItem(id: 'vehicle.view_dept', label: '查看部门用车'),
  394. _PermItem(id: 'vehicle.approve', label: '审批用车'),
  395. _PermItem(id: 'outing.create', label: '创建外勤日志'),
  396. _PermItem(id: 'outing.view_own', label: '查看本人日志'),
  397. _PermItem(id: 'outing.view_dept', label: '查看部门日志'),
  398. _PermItem(id: 'outing.comment', label: '点评外勤日志'),
  399. _PermItem(id: 'announcement.view', label: '查看公告'),
  400. _PermItem(id: 'announcement.create', label: '发布公告'),
  401. _PermItem(id: 'report.view', label: '查看报表'),
  402. _PermItem(id: 'report.export', label: '导出报表'),
  403. _PermItem(id: 'admin.permissions', label: '管理权限'),
  404. ];
  405. // 按模块分组
  406. const _permModules = <_PermModule>[
  407. _PermModule(
  408. name: '报销管理',
  409. items: [
  410. _PermItem(id: 'expense.apply', label: '发起报销'),
  411. _PermItem(id: 'expense.view_own', label: '查看本人报销'),
  412. _PermItem(id: 'expense.view_dept', label: '查看部门报销'),
  413. _PermItem(id: 'expense.view_all', label: '查看全公司报销'),
  414. _PermItem(id: 'expense.approve', label: '审批报销'),
  415. _PermItem(id: 'expense.mark_paid', label: '确认付款'),
  416. _PermItem(id: 'expense.export', label: '导出报销数据'),
  417. ],
  418. ),
  419. _PermModule(
  420. name: '事前申请',
  421. items: [
  422. _PermItem(id: 'preapply.apply', label: '发起事前申请'),
  423. _PermItem(id: 'preapply.view_own', label: '查看本人申请'),
  424. _PermItem(id: 'preapply.view_dept', label: '查看部门申请'),
  425. _PermItem(id: 'preapply.approve', label: '审批事前申请'),
  426. ],
  427. ),
  428. _PermModule(
  429. name: '加班管理',
  430. items: [
  431. _PermItem(id: 'overtime.apply', label: '发起加班'),
  432. _PermItem(id: 'overtime.view_own', label: '查看本人加班'),
  433. _PermItem(id: 'overtime.view_dept', label: '查看部门加班'),
  434. _PermItem(id: 'overtime.approve', label: '审批加班'),
  435. ],
  436. ),
  437. _PermModule(
  438. name: '用车管理',
  439. items: [
  440. _PermItem(id: 'vehicle.apply', label: '发起用车'),
  441. _PermItem(id: 'vehicle.view_own', label: '查看本人用车'),
  442. _PermItem(id: 'vehicle.view_dept', label: '查看部门用车'),
  443. _PermItem(id: 'vehicle.approve', label: '审批用车'),
  444. ],
  445. ),
  446. _PermModule(
  447. name: '外勤管理',
  448. items: [
  449. _PermItem(id: 'outing.create', label: '创建外勤日志'),
  450. _PermItem(id: 'outing.view_own', label: '查看本人日志'),
  451. _PermItem(id: 'outing.view_dept', label: '查看部门日志'),
  452. _PermItem(id: 'outing.comment', label: '点评外勤日志'),
  453. ],
  454. ),
  455. _PermModule(
  456. name: '公告管理',
  457. items: [
  458. _PermItem(id: 'announcement.view', label: '查看公告'),
  459. _PermItem(id: 'announcement.create', label: '发布公告'),
  460. ],
  461. ),
  462. _PermModule(
  463. name: '报表管理',
  464. items: [
  465. _PermItem(id: 'report.view', label: '查看报表'),
  466. _PermItem(id: 'report.export', label: '导出报表'),
  467. ],
  468. ),
  469. _PermModule(
  470. name: '系统管理',
  471. items: [_PermItem(id: 'admin.permissions', label: '管理权限')],
  472. ),
  473. ];
  474. // 角色预设
  475. const _presets = <_RolePreset>[
  476. _RolePreset(
  477. name: '员工',
  478. permissions: [
  479. 'expense.apply',
  480. 'expense.view_own',
  481. 'preapply.apply',
  482. 'preapply.view_own',
  483. 'overtime.apply',
  484. 'overtime.view_own',
  485. 'vehicle.apply',
  486. 'vehicle.view_own',
  487. 'outing.create',
  488. 'outing.view_own',
  489. 'announcement.view',
  490. 'report.view',
  491. ],
  492. ),
  493. _RolePreset(
  494. name: '审批人',
  495. permissions: [
  496. 'expense.apply',
  497. 'expense.view_own',
  498. 'expense.view_dept',
  499. 'expense.approve',
  500. 'preapply.apply',
  501. 'preapply.view_own',
  502. 'preapply.view_dept',
  503. 'preapply.approve',
  504. 'overtime.apply',
  505. 'overtime.view_own',
  506. 'overtime.view_dept',
  507. 'overtime.approve',
  508. 'vehicle.apply',
  509. 'vehicle.view_own',
  510. 'vehicle.view_dept',
  511. 'vehicle.approve',
  512. 'outing.create',
  513. 'outing.view_own',
  514. 'outing.view_dept',
  515. 'outing.comment',
  516. 'announcement.view',
  517. 'report.view',
  518. ],
  519. ),
  520. _RolePreset(
  521. name: '财务人员',
  522. permissions: [
  523. 'expense.apply',
  524. 'expense.view_own',
  525. 'expense.view_all',
  526. 'expense.mark_paid',
  527. 'expense.export',
  528. 'preapply.apply',
  529. 'preapply.view_own',
  530. 'announcement.view',
  531. 'report.view',
  532. 'report.export',
  533. ],
  534. ),
  535. _RolePreset(
  536. name: '系统管理员',
  537. permissions: [
  538. 'expense.apply',
  539. 'expense.view_own',
  540. 'expense.view_dept',
  541. 'expense.view_all',
  542. 'expense.approve',
  543. 'expense.mark_paid',
  544. 'expense.export',
  545. 'preapply.apply',
  546. 'preapply.view_own',
  547. 'preapply.view_dept',
  548. 'preapply.approve',
  549. 'overtime.apply',
  550. 'overtime.view_own',
  551. 'overtime.view_dept',
  552. 'overtime.approve',
  553. 'vehicle.apply',
  554. 'vehicle.view_own',
  555. 'vehicle.view_dept',
  556. 'vehicle.approve',
  557. 'outing.create',
  558. 'outing.view_own',
  559. 'outing.view_dept',
  560. 'outing.comment',
  561. 'announcement.view',
  562. 'announcement.create',
  563. 'report.view',
  564. 'report.export',
  565. 'admin.permissions',
  566. ],
  567. ),
  568. ];
  569. Set<String> _getDefaultPerms(List<String> roles) {
  570. if (roles.contains('系统管理员')) return _presets[3].permissions.toSet();
  571. if (roles.contains('财务人员')) return _presets[2].permissions.toSet();
  572. if (roles.contains('审批人')) return _presets[1].permissions.toSet();
  573. return _presets[0].permissions.toSet();
  574. }
  575. class _RolePreset {
  576. final String name;
  577. final List<String> permissions;
  578. const _RolePreset({required this.name, required this.permissions});
  579. }
  580. // ── 员工数据模型 ──
  581. class _Employee {
  582. final String name;
  583. final String avatarText;
  584. final String employeeId;
  585. final String department;
  586. final List<String> roles;
  587. final bool isActive;
  588. const _Employee({
  589. required this.name,
  590. required this.avatarText,
  591. required this.employeeId,
  592. required this.department,
  593. required this.roles,
  594. this.isActive = true,
  595. });
  596. }
  597. // ── 权限抽屉组件 ──
  598. class _PermissionDrawer extends StatefulWidget {
  599. final _Employee employee;
  600. final Map<String, bool> checked;
  601. final String currentUserId;
  602. final VoidCallback onSave;
  603. final VoidCallback onCancel;
  604. const _PermissionDrawer({
  605. required this.employee,
  606. required this.checked,
  607. required this.currentUserId,
  608. required this.onSave,
  609. required this.onCancel,
  610. });
  611. @override
  612. State<_PermissionDrawer> createState() => _PermissionDrawerState();
  613. }
  614. class _PermissionDrawerState extends State<_PermissionDrawer> {
  615. late Map<String, bool> _checked;
  616. bool _showHistory = false;
  617. bool get _isSelfAdmin => widget.employee.employeeId == widget.currentUserId;
  618. // 模拟变更记录
  619. final _mockHistory = [
  620. _ChangeLog(
  621. time: '2026-06-04 14:32',
  622. operator: '赵管理员',
  623. summary: '添加了财务人员角色',
  624. ),
  625. _ChangeLog(
  626. time: '2026-06-03 09:15',
  627. operator: '赵管理员',
  628. summary: '添加了审批人权限(报销审批、加班审批)',
  629. ),
  630. _ChangeLog(time: '2026-05-28 16:40', operator: '王经理', summary: '修改为普通员工权限'),
  631. _ChangeLog(time: '2026-05-20 11:00', operator: '赵管理员', summary: '初始权限分配'),
  632. ];
  633. @override
  634. void initState() {
  635. super.initState();
  636. _checked = Map.from(widget.checked);
  637. }
  638. @override
  639. Widget build(BuildContext context) {
  640. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  641. final width = MediaQuery.of(context).size.width * 0.82;
  642. return Material(
  643. color: Colors.transparent,
  644. child: Align(
  645. alignment: Alignment.centerRight,
  646. child: Container(
  647. width: width,
  648. height: double.infinity,
  649. decoration: BoxDecoration(
  650. color: colors.bgPage,
  651. borderRadius: BorderRadius.only(
  652. topLeft: Radius.circular(12),
  653. bottomLeft: Radius.circular(12),
  654. ),
  655. ),
  656. child: Column(
  657. children: [
  658. // 标题栏
  659. _buildHeader(),
  660. // 可滚动内容
  661. Expanded(
  662. child: SingleChildScrollView(
  663. child: Column(
  664. crossAxisAlignment: CrossAxisAlignment.start,
  665. children: [
  666. _buildEmployeeInfo(),
  667. _buildQuickPresets(),
  668. _buildPermissionList(),
  669. _buildHistorySection(),
  670. const SizedBox(height: 24),
  671. ],
  672. ),
  673. ),
  674. ),
  675. // 底部保存按钮
  676. _buildSaveButton(),
  677. ],
  678. ),
  679. ),
  680. ),
  681. );
  682. }
  683. Widget _buildHeader() {
  684. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  685. final l10n = AppLocalizations.of(context);
  686. return Container(
  687. padding: const EdgeInsets.fromLTRB(16, 48, 8, 12),
  688. decoration: BoxDecoration(
  689. color: colors.bgCard,
  690. border: Border(bottom: BorderSide(color: colors.border)),
  691. ),
  692. child: Row(
  693. children: [
  694. Text(
  695. l10n.get('permissionEdit'),
  696. style: TextStyle(
  697. fontSize: 18,
  698. fontWeight: FontWeight.w600,
  699. color: colors.textPrimary,
  700. ),
  701. ),
  702. const Spacer(),
  703. IconButton(
  704. icon: Icon(Icons.close, size: 20, color: colors.textSecondary),
  705. onPressed: widget.onCancel,
  706. ),
  707. ],
  708. ),
  709. );
  710. }
  711. Widget _buildEmployeeInfo() {
  712. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  713. return Container(
  714. width: double.infinity,
  715. padding: const EdgeInsets.all(16),
  716. color: colors.bgCard,
  717. child: Row(
  718. children: [
  719. Container(
  720. width: 44,
  721. height: 44,
  722. decoration: BoxDecoration(
  723. color: colors.primary,
  724. borderRadius: BorderRadius.circular(22),
  725. ),
  726. child: Center(
  727. child: Text(
  728. widget.employee.avatarText,
  729. style: const TextStyle(
  730. fontSize: 18,
  731. fontWeight: FontWeight.w600,
  732. color: Colors.white,
  733. ),
  734. ),
  735. ),
  736. ),
  737. const SizedBox(width: 12),
  738. Expanded(
  739. child: Column(
  740. crossAxisAlignment: CrossAxisAlignment.start,
  741. children: [
  742. Text(
  743. widget.employee.name,
  744. style: TextStyle(
  745. fontSize: 16,
  746. fontWeight: FontWeight.w600,
  747. color: colors.textPrimary,
  748. ),
  749. ),
  750. const SizedBox(height: 2),
  751. Text(
  752. '${widget.employee.department} · 工号:${widget.employee.employeeId}',
  753. style: TextStyle(fontSize: 12, color: colors.textSecondary),
  754. ),
  755. ],
  756. ),
  757. ),
  758. ],
  759. ),
  760. );
  761. }
  762. // ── 快捷套餐 ──
  763. Widget _buildQuickPresets() {
  764. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  765. final l10n = AppLocalizations.of(context);
  766. return Container(
  767. width: double.infinity,
  768. padding: const EdgeInsets.fromLTRB(16, 16, 16, 12),
  769. color: colors.bgCard,
  770. child: Column(
  771. crossAxisAlignment: CrossAxisAlignment.start,
  772. children: [
  773. Text(
  774. l10n.get('quickPresets'),
  775. style: TextStyle(
  776. fontSize: 13,
  777. fontWeight: FontWeight.w600,
  778. color: colors.textSecondary,
  779. ),
  780. ),
  781. const SizedBox(height: 10),
  782. Wrap(
  783. spacing: 8,
  784. runSpacing: 8,
  785. children: _presets.map((preset) {
  786. return GestureDetector(
  787. onTap: () => _applyPreset(preset),
  788. child: Container(
  789. padding: const EdgeInsets.symmetric(
  790. horizontal: 12,
  791. vertical: 6,
  792. ),
  793. decoration: BoxDecoration(
  794. color: colors.primaryLight,
  795. borderRadius: BorderRadius.circular(16),
  796. border: Border.all(
  797. color: colors.primary.withValues(alpha: 0.3),
  798. ),
  799. ),
  800. child: Text(
  801. preset.name,
  802. style: TextStyle(
  803. fontSize: 13,
  804. color: colors.primary,
  805. fontWeight: FontWeight.w500,
  806. ),
  807. ),
  808. ),
  809. );
  810. }).toList(),
  811. ),
  812. ],
  813. ),
  814. );
  815. }
  816. void _applyPreset(_RolePreset preset) {
  817. if (_isSelfAdmin && preset.name != '系统管理员') {
  818. // 自保护:自己是admin不能取消自己的admin
  819. final hadAdmin = widget.checked.keys.any(
  820. (k) => k == 'admin.permissions' && widget.checked[k] == true,
  821. );
  822. if (hadAdmin && !preset.permissions.contains('admin.permissions')) {
  823. ScaffoldMessenger.of(context).showSnackBar(
  824. const SnackBar(
  825. content: Text('无法取消自己的管理员权限'),
  826. duration: Duration(seconds: 2),
  827. ),
  828. );
  829. return;
  830. }
  831. }
  832. setState(() {
  833. // 先全重置
  834. for (final k in _checked.keys) {
  835. _checked[k] = false;
  836. }
  837. // 再勾选预设
  838. for (final p in preset.permissions) {
  839. if (_checked.containsKey(p)) {
  840. _checked[p] = true;
  841. }
  842. }
  843. });
  844. }
  845. // ── 权限点列表 ──
  846. Widget _buildPermissionList() {
  847. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  848. final l10n = AppLocalizations.of(context);
  849. return Container(
  850. width: double.infinity,
  851. padding: const EdgeInsets.fromLTRB(16, 8, 16, 4),
  852. color: colors.bgCard,
  853. child: Column(
  854. crossAxisAlignment: CrossAxisAlignment.start,
  855. children: [
  856. Text(
  857. l10n.get('permissionItems'),
  858. style: TextStyle(
  859. fontSize: 13,
  860. fontWeight: FontWeight.w600,
  861. color: colors.textSecondary,
  862. ),
  863. ),
  864. const SizedBox(height: 8),
  865. ..._permModules.map((module) => _buildModuleGroup(module)),
  866. ],
  867. ),
  868. );
  869. }
  870. Widget _buildModuleGroup(_PermModule module) {
  871. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  872. return Padding(
  873. padding: const EdgeInsets.only(bottom: 12),
  874. child: Column(
  875. crossAxisAlignment: CrossAxisAlignment.start,
  876. children: [
  877. Text(
  878. module.name,
  879. style: TextStyle(
  880. fontSize: 13,
  881. fontWeight: FontWeight.w600,
  882. color: colors.textPrimary,
  883. ),
  884. ),
  885. const SizedBox(height: 4),
  886. ...module.items.map((perm) {
  887. final val = _checked[perm.id] ?? false;
  888. final isAdminPerm = perm.id == 'admin.permissions';
  889. final canToggle = !(_isSelfAdmin && isAdminPerm);
  890. return GestureDetector(
  891. onTap: canToggle
  892. ? () => setState(() => _checked[perm.id] = !val)
  893. : null,
  894. child: Container(
  895. padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 4),
  896. child: Row(
  897. children: [
  898. Icon(
  899. val ? Icons.check_box : Icons.check_box_outline_blank,
  900. size: 20,
  901. color: val
  902. ? canToggle
  903. ? colors.primary
  904. : colors.textPlaceholder
  905. : colors.textPlaceholder,
  906. ),
  907. const SizedBox(width: 8),
  908. Text(
  909. perm.label,
  910. style: TextStyle(
  911. fontSize: 13,
  912. color: canToggle
  913. ? colors.textPrimary
  914. : colors.textPlaceholder,
  915. ),
  916. ),
  917. if (!canToggle)
  918. Padding(
  919. padding: EdgeInsets.only(left: 6),
  920. child: Icon(
  921. Icons.lock_outline,
  922. size: 12,
  923. color: colors.textPlaceholder,
  924. ),
  925. ),
  926. ],
  927. ),
  928. ),
  929. );
  930. }),
  931. ],
  932. ),
  933. );
  934. }
  935. // ── 变更记录 ──
  936. Widget _buildHistorySection() {
  937. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  938. final l10n = AppLocalizations.of(context);
  939. return Container(
  940. width: double.infinity,
  941. margin: const EdgeInsets.symmetric(horizontal: 16),
  942. decoration: BoxDecoration(
  943. color: colors.bgCard,
  944. borderRadius: BorderRadius.circular(8),
  945. ),
  946. child: Column(
  947. children: [
  948. GestureDetector(
  949. onTap: () => setState(() => _showHistory = !_showHistory),
  950. child: Padding(
  951. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  952. child: Row(
  953. children: [
  954. Icon(Icons.history, size: 16, color: colors.textSecondary),
  955. const SizedBox(width: 6),
  956. Text(
  957. l10n.get('changeLog'),
  958. style: TextStyle(
  959. fontSize: 13,
  960. fontWeight: FontWeight.w600,
  961. color: colors.textSecondary,
  962. ),
  963. ),
  964. const Spacer(),
  965. Text(
  966. l10n.getString(
  967. 'recentItems',
  968. args: {'count': '${_mockHistory.length}'},
  969. ),
  970. style: TextStyle(
  971. fontSize: 11,
  972. color: colors.textPlaceholder,
  973. ),
  974. ),
  975. Icon(
  976. _showHistory
  977. ? Icons.keyboard_arrow_up
  978. : Icons.keyboard_arrow_down,
  979. size: 18,
  980. color: colors.textPlaceholder,
  981. ),
  982. ],
  983. ),
  984. ),
  985. ),
  986. if (_showHistory)
  987. ..._mockHistory.map((log) => _buildTimelineItem(log)),
  988. ],
  989. ),
  990. );
  991. }
  992. Widget _buildTimelineItem(_ChangeLog log) {
  993. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  994. return Container(
  995. padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
  996. child: Row(
  997. crossAxisAlignment: CrossAxisAlignment.start,
  998. children: [
  999. Column(
  1000. children: [
  1001. Container(
  1002. width: 8,
  1003. height: 8,
  1004. decoration: BoxDecoration(
  1005. color: colors.primary,
  1006. shape: BoxShape.circle,
  1007. ),
  1008. ),
  1009. Container(width: 1, height: 40, color: colors.border),
  1010. ],
  1011. ),
  1012. const SizedBox(width: 10),
  1013. Expanded(
  1014. child: Column(
  1015. crossAxisAlignment: CrossAxisAlignment.start,
  1016. children: [
  1017. Text(
  1018. log.summary,
  1019. style: TextStyle(fontSize: 13, color: colors.textPrimary),
  1020. ),
  1021. const SizedBox(height: 2),
  1022. Text(
  1023. '${log.operator} · ${log.time}',
  1024. style: TextStyle(fontSize: 11, color: colors.textPlaceholder),
  1025. ),
  1026. ],
  1027. ),
  1028. ),
  1029. ],
  1030. ),
  1031. );
  1032. }
  1033. // ── 保存按钮 ──
  1034. Widget _buildSaveButton() {
  1035. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1036. final l10n = AppLocalizations.of(context);
  1037. return Container(
  1038. width: double.infinity,
  1039. padding: const EdgeInsets.all(16),
  1040. decoration: BoxDecoration(
  1041. color: colors.bgCard,
  1042. boxShadow: const [
  1043. BoxShadow(
  1044. color: Color(0x15000000),
  1045. blurRadius: 8,
  1046. offset: Offset(0, -2),
  1047. ),
  1048. ],
  1049. ),
  1050. child: GestureDetector(
  1051. onTap: () {
  1052. if (_isSelfAdmin && !(_checked['admin.permissions'] ?? false)) {
  1053. ScaffoldMessenger.of(context).showSnackBar(
  1054. const SnackBar(
  1055. content: Text('无法取消自己的管理员权限'),
  1056. duration: Duration(seconds: 2),
  1057. ),
  1058. );
  1059. return;
  1060. }
  1061. widget.onSave();
  1062. },
  1063. child: Container(
  1064. width: double.infinity,
  1065. padding: const EdgeInsets.symmetric(vertical: 14),
  1066. decoration: BoxDecoration(
  1067. color: colors.primary,
  1068. borderRadius: BorderRadius.circular(22),
  1069. ),
  1070. child: Text(
  1071. l10n.get('confirmSave'),
  1072. textAlign: TextAlign.center,
  1073. style: const TextStyle(
  1074. fontSize: 16,
  1075. fontWeight: FontWeight.w600,
  1076. color: Colors.white,
  1077. ),
  1078. ),
  1079. ),
  1080. ),
  1081. );
  1082. }
  1083. }
  1084. class _ChangeLog {
  1085. final String time;
  1086. final String operator;
  1087. final String summary;
  1088. const _ChangeLog({
  1089. required this.time,
  1090. required this.operator,
  1091. required this.summary,
  1092. });
  1093. }