admin_permissions_page.dart 34 KB

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