| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:go_router/go_router.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import '../../core/theme/app_colors_extension.dart';
- import '../../core/i18n/app_localizations.dart';
- import '../shell/nav_bar_config.dart';
- /// 权限管理 - 页面3.2 【管理员专属】
- class AdminPermissionsPage extends ConsumerStatefulWidget {
- const AdminPermissionsPage({super.key});
- @override
- ConsumerState<AdminPermissionsPage> createState() =>
- _AdminPermissionsPageState();
- }
- class _AdminPermissionsPageState extends ConsumerState<AdminPermissionsPage> {
- final _searchCtrl = TextEditingController();
- Timer? _debounce;
- String _searchQuery = '';
- // 模拟当前登录用户(自保护用)
- static const _currentUserId = '0001';
- final _employees = <_Employee>[
- _Employee(
- name: '张三',
- avatarText: '张',
- employeeId: '0048',
- department: '销售部',
- roles: ['普通员工', '审批人'],
- isActive: true,
- ),
- _Employee(
- name: '王经理',
- avatarText: '王',
- employeeId: '0012',
- department: '销售部',
- roles: ['审批人', '系统管理员'],
- isActive: true,
- ),
- _Employee(
- name: '李会计',
- avatarText: '李',
- employeeId: '0025',
- department: '财务部',
- roles: ['财务人员'],
- isActive: true,
- ),
- _Employee(
- name: '赵管理员',
- avatarText: '赵',
- employeeId: '0001',
- department: '信息技术部',
- roles: ['系统管理员'],
- isActive: true,
- ),
- _Employee(
- name: '钱六',
- avatarText: '钱',
- employeeId: '0052',
- department: '财务部',
- roles: ['财务人员'],
- isActive: false,
- ),
- _Employee(
- name: '孙七',
- avatarText: '孙',
- employeeId: '0078',
- department: '行政部',
- roles: ['普通员工'],
- isActive: true,
- ),
- _Employee(
- name: '周八',
- avatarText: '周',
- employeeId: '0091',
- department: '技术部',
- roles: ['普通员工'],
- isActive: true,
- ),
- ];
- List<_Employee> get _filteredEmployees {
- if (_searchQuery.isEmpty) return _employees;
- final q = _searchQuery.toLowerCase();
- return _employees.where((e) {
- return e.name.toLowerCase().contains(q) ||
- e.employeeId.toLowerCase().contains(q);
- }).toList();
- }
- @override
- void initState() {
- super.initState();
- }
- @override
- void dispose() {
- _searchCtrl.dispose();
- _debounce?.cancel();
- super.dispose();
- }
- void _onSearchChanged(String value) {
- _debounce?.cancel();
- _debounce = Timer(const Duration(milliseconds: 300), () {
- if (mounted) {
- setState(() => _searchQuery = value);
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- //final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final l10n = AppLocalizations.of(context);
- ref
- .read(navBarConfigProvider.notifier)
- .update(
- NavBarConfig(
- title: l10n.get('permissionManagement'),
- showBack: true,
- onBack: () => context.pop(),
- ),
- );
- return Column(
- children: [
- _buildSearchBar(),
- Expanded(
- child: ListView.builder(
- padding: const EdgeInsets.all(16),
- itemCount: _filteredEmployees.length,
- itemBuilder: (_, i) => _buildEmpCard(_filteredEmployees[i]),
- ),
- ),
- ],
- );
- }
- // ── 搜索栏(300ms 防抖) ──
- Widget _buildSearchBar() {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- return Container(
- width: double.infinity,
- padding: const EdgeInsets.fromLTRB(16, 10, 16, 10),
- color: colors.bgCard,
- child: Container(
- padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
- decoration: BoxDecoration(
- color: colors.bgPage,
- borderRadius: BorderRadius.circular(18),
- border: Border.all(color: colors.border),
- ),
- child: Row(
- children: [
- Icon(Icons.search, size: 18, color: colors.textPlaceholder),
- const SizedBox(width: 6),
- Expanded(
- child: TextField(
- controller: _searchCtrl,
- onChanged: _onSearchChanged,
- decoration: InputDecoration(
- hintText: '输入姓名或工号进行检索...',
- hintStyle: TextStyle(
- fontSize: 14,
- color: colors.textPlaceholder,
- ),
- border: InputBorder.none,
- contentPadding: EdgeInsets.symmetric(vertical: 10),
- isDense: true,
- ),
- style: TextStyle(fontSize: 14, color: colors.textPrimary),
- ),
- ),
- if (_searchCtrl.text.isNotEmpty)
- GestureDetector(
- onTap: () {
- _searchCtrl.clear();
- _onSearchChanged('');
- setState(() => _searchQuery = '');
- },
- child: Icon(
- Icons.clear,
- size: 16,
- color: colors.textPlaceholder,
- ),
- ),
- ],
- ),
- ),
- );
- }
- // ── 员工卡片 ──
- Widget _buildEmpCard(_Employee emp) {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- return Padding(
- padding: const EdgeInsets.only(bottom: 12),
- child: GestureDetector(
- onTap: () => _openPermissionDrawer(emp),
- child: Container(
- padding: const EdgeInsets.all(12),
- decoration: BoxDecoration(
- color: emp.isActive ? colors.bgCard : const Color(0xFFF9F9F9),
- borderRadius: BorderRadius.circular(8),
- boxShadow: const [
- BoxShadow(
- color: Color(0x08000000),
- blurRadius: 4,
- offset: Offset(0, 1),
- ),
- ],
- ),
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- // 头像
- Container(
- width: 40,
- height: 40,
- decoration: BoxDecoration(
- color: colors.primary,
- borderRadius: BorderRadius.circular(20),
- ),
- child: Center(
- child: Text(
- emp.avatarText,
- style: const TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.w600,
- color: Colors.white,
- ),
- ),
- ),
- ),
- const SizedBox(width: 10),
- // 信息区
- Expanded(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- children: [
- Text(
- emp.name,
- style: TextStyle(
- fontSize: 15,
- fontWeight: FontWeight.w600,
- color: colors.textPrimary,
- ),
- ),
- const SizedBox(width: 6),
- Text(
- '工号:${emp.employeeId}',
- style: TextStyle(
- fontSize: 12,
- color: colors.textPlaceholder,
- ),
- ),
- ],
- ),
- const SizedBox(height: 2),
- Text(
- emp.department,
- style: TextStyle(
- fontSize: 12,
- color: colors.textSecondary,
- ),
- ),
- ],
- ),
- ),
- // 角色标签区
- Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- ...emp.roles.map(
- (role) => Padding(
- padding: const EdgeInsets.only(bottom: 4),
- child: _buildRoleTag(role),
- ),
- ),
- if (!emp.isActive)
- Container(
- padding: const EdgeInsets.symmetric(
- horizontal: 6,
- vertical: 2,
- ),
- decoration: BoxDecoration(
- color: colors.dangerBg,
- borderRadius: BorderRadius.circular(3),
- ),
- child: Text(
- '已禁用',
- style: TextStyle(fontSize: 10, color: colors.danger),
- ),
- ),
- ],
- ),
- ],
- ),
- ),
- ),
- );
- }
- Widget _buildRoleTag(String role) {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- Color bgColor;
- Color textColor;
- switch (role) {
- case '审批人':
- bgColor = colors.warningBg;
- textColor = colors.warning;
- break;
- case '财务人员':
- bgColor = colors.successBg;
- textColor = colors.success;
- break;
- case '系统管理员':
- bgColor = colors.dangerBg;
- textColor = colors.danger;
- break;
- default:
- bgColor = colors.primaryLight;
- textColor = colors.primary;
- }
- return Container(
- padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
- decoration: BoxDecoration(
- color: bgColor,
- borderRadius: BorderRadius.circular(3),
- ),
- child: Text(role, style: TextStyle(fontSize: 10, color: textColor)),
- );
- }
- // ── 权限抽屉(右侧滑出) ──
- void _openPermissionDrawer(_Employee emp) {
- // 深拷贝当前权限状态
- final checked = <String, bool>{};
- for (final perm in _allPermissions) {
- checked[perm.id] = _getDefaultPerms(emp.roles).contains(perm.id);
- }
- showGeneralDialog(
- context: context,
- barrierDismissible: true,
- barrierLabel: '',
- barrierColor: Colors.black45,
- transitionDuration: const Duration(milliseconds: 300),
- pageBuilder: (ctx, anim1, anim2) {
- return _PermissionDrawer(
- employee: emp,
- checked: checked,
- currentUserId: _currentUserId,
- onSave: () {
- Navigator.of(ctx).pop();
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text('权限已更新'),
- duration: Duration(seconds: 2),
- ),
- );
- },
- onCancel: () => Navigator.of(ctx).pop(),
- );
- },
- transitionBuilder: (ctx, anim, secondaryAnim, child) {
- return SlideTransition(
- position: Tween<Offset>(
- begin: const Offset(1.0, 0.0),
- end: Offset.zero,
- ).animate(CurvedAnimation(parent: anim, curve: Curves.easeOutCubic)),
- child: child,
- );
- },
- );
- }
- }
- // ── 权限数据定义 ──
- class _PermModule {
- final String name;
- final List<_PermItem> items;
- const _PermModule({required this.name, required this.items});
- }
- class _PermItem {
- final String id;
- final String label;
- const _PermItem({required this.id, required this.label});
- }
- const _allPermissions = <_PermItem>[
- _PermItem(id: 'expense.apply', label: '发起报销'),
- _PermItem(id: 'expense.view_own', label: '查看本人报销'),
- _PermItem(id: 'expense.view_dept', label: '查看部门报销'),
- _PermItem(id: 'expense.view_all', label: '查看全公司报销'),
- _PermItem(id: 'expense.approve', label: '审批报销'),
- _PermItem(id: 'expense.mark_paid', label: '确认付款'),
- _PermItem(id: 'expense.export', label: '导出报销数据'),
- _PermItem(id: 'preapply.apply', label: '发起事前申请'),
- _PermItem(id: 'preapply.view_own', label: '查看本人申请'),
- _PermItem(id: 'preapply.view_dept', label: '查看部门申请'),
- _PermItem(id: 'preapply.approve', label: '审批事前申请'),
- _PermItem(id: 'overtime.apply', label: '发起加班'),
- _PermItem(id: 'overtime.view_own', label: '查看本人加班'),
- _PermItem(id: 'overtime.view_dept', label: '查看部门加班'),
- _PermItem(id: 'overtime.approve', label: '审批加班'),
- _PermItem(id: 'vehicle.apply', label: '发起用车'),
- _PermItem(id: 'vehicle.view_own', label: '查看本人用车'),
- _PermItem(id: 'vehicle.view_dept', label: '查看部门用车'),
- _PermItem(id: 'vehicle.approve', label: '审批用车'),
- _PermItem(id: 'outing.create', label: '创建外勤日志'),
- _PermItem(id: 'outing.view_own', label: '查看本人日志'),
- _PermItem(id: 'outing.view_dept', label: '查看部门日志'),
- _PermItem(id: 'outing.comment', label: '点评外勤日志'),
- _PermItem(id: 'announcement.view', label: '查看公告'),
- _PermItem(id: 'announcement.create', label: '发布公告'),
- _PermItem(id: 'report.view', label: '查看报表'),
- _PermItem(id: 'report.export', label: '导出报表'),
- _PermItem(id: 'admin.permissions', label: '管理权限'),
- ];
- // 按模块分组
- const _permModules = <_PermModule>[
- _PermModule(
- name: '报销管理',
- items: [
- _PermItem(id: 'expense.apply', label: '发起报销'),
- _PermItem(id: 'expense.view_own', label: '查看本人报销'),
- _PermItem(id: 'expense.view_dept', label: '查看部门报销'),
- _PermItem(id: 'expense.view_all', label: '查看全公司报销'),
- _PermItem(id: 'expense.approve', label: '审批报销'),
- _PermItem(id: 'expense.mark_paid', label: '确认付款'),
- _PermItem(id: 'expense.export', label: '导出报销数据'),
- ],
- ),
- _PermModule(
- name: '事前申请',
- items: [
- _PermItem(id: 'preapply.apply', label: '发起事前申请'),
- _PermItem(id: 'preapply.view_own', label: '查看本人申请'),
- _PermItem(id: 'preapply.view_dept', label: '查看部门申请'),
- _PermItem(id: 'preapply.approve', label: '审批事前申请'),
- ],
- ),
- _PermModule(
- name: '加班管理',
- items: [
- _PermItem(id: 'overtime.apply', label: '发起加班'),
- _PermItem(id: 'overtime.view_own', label: '查看本人加班'),
- _PermItem(id: 'overtime.view_dept', label: '查看部门加班'),
- _PermItem(id: 'overtime.approve', label: '审批加班'),
- ],
- ),
- _PermModule(
- name: '用车管理',
- items: [
- _PermItem(id: 'vehicle.apply', label: '发起用车'),
- _PermItem(id: 'vehicle.view_own', label: '查看本人用车'),
- _PermItem(id: 'vehicle.view_dept', label: '查看部门用车'),
- _PermItem(id: 'vehicle.approve', label: '审批用车'),
- ],
- ),
- _PermModule(
- name: '外勤管理',
- items: [
- _PermItem(id: 'outing.create', label: '创建外勤日志'),
- _PermItem(id: 'outing.view_own', label: '查看本人日志'),
- _PermItem(id: 'outing.view_dept', label: '查看部门日志'),
- _PermItem(id: 'outing.comment', label: '点评外勤日志'),
- ],
- ),
- _PermModule(
- name: '公告管理',
- items: [
- _PermItem(id: 'announcement.view', label: '查看公告'),
- _PermItem(id: 'announcement.create', label: '发布公告'),
- ],
- ),
- _PermModule(
- name: '报表管理',
- items: [
- _PermItem(id: 'report.view', label: '查看报表'),
- _PermItem(id: 'report.export', label: '导出报表'),
- ],
- ),
- _PermModule(
- name: '系统管理',
- items: [_PermItem(id: 'admin.permissions', label: '管理权限')],
- ),
- ];
- // 角色预设
- const _presets = <_RolePreset>[
- _RolePreset(
- name: '员工',
- permissions: [
- 'expense.apply',
- 'expense.view_own',
- 'preapply.apply',
- 'preapply.view_own',
- 'overtime.apply',
- 'overtime.view_own',
- 'vehicle.apply',
- 'vehicle.view_own',
- 'outing.create',
- 'outing.view_own',
- 'announcement.view',
- 'report.view',
- ],
- ),
- _RolePreset(
- name: '审批人',
- permissions: [
- 'expense.apply',
- 'expense.view_own',
- 'expense.view_dept',
- 'expense.approve',
- 'preapply.apply',
- 'preapply.view_own',
- 'preapply.view_dept',
- 'preapply.approve',
- 'overtime.apply',
- 'overtime.view_own',
- 'overtime.view_dept',
- 'overtime.approve',
- 'vehicle.apply',
- 'vehicle.view_own',
- 'vehicle.view_dept',
- 'vehicle.approve',
- 'outing.create',
- 'outing.view_own',
- 'outing.view_dept',
- 'outing.comment',
- 'announcement.view',
- 'report.view',
- ],
- ),
- _RolePreset(
- name: '财务人员',
- permissions: [
- 'expense.apply',
- 'expense.view_own',
- 'expense.view_all',
- 'expense.mark_paid',
- 'expense.export',
- 'preapply.apply',
- 'preapply.view_own',
- 'announcement.view',
- 'report.view',
- 'report.export',
- ],
- ),
- _RolePreset(
- name: '系统管理员',
- permissions: [
- 'expense.apply',
- 'expense.view_own',
- 'expense.view_dept',
- 'expense.view_all',
- 'expense.approve',
- 'expense.mark_paid',
- 'expense.export',
- 'preapply.apply',
- 'preapply.view_own',
- 'preapply.view_dept',
- 'preapply.approve',
- 'overtime.apply',
- 'overtime.view_own',
- 'overtime.view_dept',
- 'overtime.approve',
- 'vehicle.apply',
- 'vehicle.view_own',
- 'vehicle.view_dept',
- 'vehicle.approve',
- 'outing.create',
- 'outing.view_own',
- 'outing.view_dept',
- 'outing.comment',
- 'announcement.view',
- 'announcement.create',
- 'report.view',
- 'report.export',
- 'admin.permissions',
- ],
- ),
- ];
- Set<String> _getDefaultPerms(List<String> roles) {
- if (roles.contains('系统管理员')) return _presets[3].permissions.toSet();
- if (roles.contains('财务人员')) return _presets[2].permissions.toSet();
- if (roles.contains('审批人')) return _presets[1].permissions.toSet();
- return _presets[0].permissions.toSet();
- }
- class _RolePreset {
- final String name;
- final List<String> permissions;
- const _RolePreset({required this.name, required this.permissions});
- }
- // ── 员工数据模型 ──
- class _Employee {
- final String name;
- final String avatarText;
- final String employeeId;
- final String department;
- final List<String> roles;
- final bool isActive;
- const _Employee({
- required this.name,
- required this.avatarText,
- required this.employeeId,
- required this.department,
- required this.roles,
- this.isActive = true,
- });
- }
- // ── 权限抽屉组件 ──
- class _PermissionDrawer extends StatefulWidget {
- final _Employee employee;
- final Map<String, bool> checked;
- final String currentUserId;
- final VoidCallback onSave;
- final VoidCallback onCancel;
- const _PermissionDrawer({
- required this.employee,
- required this.checked,
- required this.currentUserId,
- required this.onSave,
- required this.onCancel,
- });
- @override
- State<_PermissionDrawer> createState() => _PermissionDrawerState();
- }
- class _PermissionDrawerState extends State<_PermissionDrawer> {
- late Map<String, bool> _checked;
- bool _showHistory = false;
- bool get _isSelfAdmin => widget.employee.employeeId == widget.currentUserId;
- // 模拟变更记录
- final _mockHistory = [
- _ChangeLog(
- time: '2026-06-04 14:32',
- operator: '赵管理员',
- summary: '添加了财务人员角色',
- ),
- _ChangeLog(
- time: '2026-06-03 09:15',
- operator: '赵管理员',
- summary: '添加了审批人权限(报销审批、加班审批)',
- ),
- _ChangeLog(time: '2026-05-28 16:40', operator: '王经理', summary: '修改为普通员工权限'),
- _ChangeLog(time: '2026-05-20 11:00', operator: '赵管理员', summary: '初始权限分配'),
- ];
- @override
- void initState() {
- super.initState();
- _checked = Map.from(widget.checked);
- }
- @override
- Widget build(BuildContext context) {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final width = MediaQuery.of(context).size.width * 0.82;
- return Material(
- color: Colors.transparent,
- child: Align(
- alignment: Alignment.centerRight,
- child: Container(
- width: width,
- height: double.infinity,
- decoration: BoxDecoration(
- color: colors.bgPage,
- borderRadius: BorderRadius.only(
- topLeft: Radius.circular(12),
- bottomLeft: Radius.circular(12),
- ),
- ),
- child: Column(
- children: [
- // 标题栏
- _buildHeader(),
- // 可滚动内容
- Expanded(
- child: SingleChildScrollView(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- _buildEmployeeInfo(),
- _buildQuickPresets(),
- _buildPermissionList(),
- _buildHistorySection(),
- const SizedBox(height: 24),
- ],
- ),
- ),
- ),
- // 底部保存按钮
- _buildSaveButton(),
- ],
- ),
- ),
- ),
- );
- }
- Widget _buildHeader() {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final l10n = AppLocalizations.of(context);
- return Container(
- padding: const EdgeInsets.fromLTRB(16, 48, 8, 12),
- decoration: BoxDecoration(
- color: colors.bgCard,
- border: Border(bottom: BorderSide(color: colors.border)),
- ),
- child: Row(
- children: [
- Text(
- l10n.get('permissionEdit'),
- style: TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.w600,
- color: colors.textPrimary,
- ),
- ),
- const Spacer(),
- IconButton(
- icon: Icon(Icons.close, size: 20, color: colors.textSecondary),
- onPressed: widget.onCancel,
- ),
- ],
- ),
- );
- }
- Widget _buildEmployeeInfo() {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- return Container(
- width: double.infinity,
- padding: const EdgeInsets.all(16),
- color: colors.bgCard,
- child: Row(
- children: [
- Container(
- width: 44,
- height: 44,
- decoration: BoxDecoration(
- color: colors.primary,
- borderRadius: BorderRadius.circular(22),
- ),
- child: Center(
- child: Text(
- widget.employee.avatarText,
- style: const TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.w600,
- color: Colors.white,
- ),
- ),
- ),
- ),
- const SizedBox(width: 12),
- Expanded(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- widget.employee.name,
- style: TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.w600,
- color: colors.textPrimary,
- ),
- ),
- const SizedBox(height: 2),
- Text(
- '${widget.employee.department} · 工号:${widget.employee.employeeId}',
- style: TextStyle(fontSize: 12, color: colors.textSecondary),
- ),
- ],
- ),
- ),
- ],
- ),
- );
- }
- // ── 快捷套餐 ──
- Widget _buildQuickPresets() {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final l10n = AppLocalizations.of(context);
- return Container(
- width: double.infinity,
- padding: const EdgeInsets.fromLTRB(16, 16, 16, 12),
- color: colors.bgCard,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- l10n.get('quickPresets'),
- style: TextStyle(
- fontSize: 13,
- fontWeight: FontWeight.w600,
- color: colors.textSecondary,
- ),
- ),
- const SizedBox(height: 10),
- Wrap(
- spacing: 8,
- runSpacing: 8,
- children: _presets.map((preset) {
- return GestureDetector(
- onTap: () => _applyPreset(preset),
- child: Container(
- padding: const EdgeInsets.symmetric(
- horizontal: 12,
- vertical: 6,
- ),
- decoration: BoxDecoration(
- color: colors.primaryLight,
- borderRadius: BorderRadius.circular(16),
- border: Border.all(
- color: colors.primary.withValues(alpha: 0.3),
- ),
- ),
- child: Text(
- preset.name,
- style: TextStyle(
- fontSize: 13,
- color: colors.primary,
- fontWeight: FontWeight.w500,
- ),
- ),
- ),
- );
- }).toList(),
- ),
- ],
- ),
- );
- }
- void _applyPreset(_RolePreset preset) {
- if (_isSelfAdmin && preset.name != '系统管理员') {
- // 自保护:自己是admin不能取消自己的admin
- final hadAdmin = widget.checked.keys.any(
- (k) => k == 'admin.permissions' && widget.checked[k] == true,
- );
- if (hadAdmin && !preset.permissions.contains('admin.permissions')) {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text('无法取消自己的管理员权限'),
- duration: Duration(seconds: 2),
- ),
- );
- return;
- }
- }
- setState(() {
- // 先全重置
- for (final k in _checked.keys) {
- _checked[k] = false;
- }
- // 再勾选预设
- for (final p in preset.permissions) {
- if (_checked.containsKey(p)) {
- _checked[p] = true;
- }
- }
- });
- }
- // ── 权限点列表 ──
- Widget _buildPermissionList() {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final l10n = AppLocalizations.of(context);
- return Container(
- width: double.infinity,
- padding: const EdgeInsets.fromLTRB(16, 8, 16, 4),
- color: colors.bgCard,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- l10n.get('permissionItems'),
- style: TextStyle(
- fontSize: 13,
- fontWeight: FontWeight.w600,
- color: colors.textSecondary,
- ),
- ),
- const SizedBox(height: 8),
- ..._permModules.map((module) => _buildModuleGroup(module)),
- ],
- ),
- );
- }
- Widget _buildModuleGroup(_PermModule module) {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- return Padding(
- padding: const EdgeInsets.only(bottom: 12),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- module.name,
- style: TextStyle(
- fontSize: 13,
- fontWeight: FontWeight.w600,
- color: colors.textPrimary,
- ),
- ),
- const SizedBox(height: 4),
- ...module.items.map((perm) {
- final val = _checked[perm.id] ?? false;
- final isAdminPerm = perm.id == 'admin.permissions';
- final canToggle = !(_isSelfAdmin && isAdminPerm);
- return GestureDetector(
- onTap: canToggle
- ? () => setState(() => _checked[perm.id] = !val)
- : null,
- child: Container(
- padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 4),
- child: Row(
- children: [
- Icon(
- val ? Icons.check_box : Icons.check_box_outline_blank,
- size: 20,
- color: val
- ? canToggle
- ? colors.primary
- : colors.textPlaceholder
- : colors.textPlaceholder,
- ),
- const SizedBox(width: 8),
- Text(
- perm.label,
- style: TextStyle(
- fontSize: 13,
- color: canToggle
- ? colors.textPrimary
- : colors.textPlaceholder,
- ),
- ),
- if (!canToggle)
- Padding(
- padding: EdgeInsets.only(left: 6),
- child: Icon(
- Icons.lock_outline,
- size: 12,
- color: colors.textPlaceholder,
- ),
- ),
- ],
- ),
- ),
- );
- }),
- ],
- ),
- );
- }
- // ── 变更记录 ──
- Widget _buildHistorySection() {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final l10n = AppLocalizations.of(context);
- return Container(
- width: double.infinity,
- margin: const EdgeInsets.symmetric(horizontal: 16),
- decoration: BoxDecoration(
- color: colors.bgCard,
- borderRadius: BorderRadius.circular(8),
- ),
- child: Column(
- children: [
- GestureDetector(
- onTap: () => setState(() => _showHistory = !_showHistory),
- child: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
- child: Row(
- children: [
- Icon(Icons.history, size: 16, color: colors.textSecondary),
- const SizedBox(width: 6),
- Text(
- l10n.get('changeLog'),
- style: TextStyle(
- fontSize: 13,
- fontWeight: FontWeight.w600,
- color: colors.textSecondary,
- ),
- ),
- const Spacer(),
- Text(
- l10n.getString(
- 'recentItems',
- args: {'count': '${_mockHistory.length}'},
- ),
- style: TextStyle(
- fontSize: 11,
- color: colors.textPlaceholder,
- ),
- ),
- Icon(
- _showHistory
- ? Icons.keyboard_arrow_up
- : Icons.keyboard_arrow_down,
- size: 18,
- color: colors.textPlaceholder,
- ),
- ],
- ),
- ),
- ),
- if (_showHistory)
- ..._mockHistory.map((log) => _buildTimelineItem(log)),
- ],
- ),
- );
- }
- Widget _buildTimelineItem(_ChangeLog log) {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- return Container(
- padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Column(
- children: [
- Container(
- width: 8,
- height: 8,
- decoration: BoxDecoration(
- color: colors.primary,
- shape: BoxShape.circle,
- ),
- ),
- Container(width: 1, height: 40, color: colors.border),
- ],
- ),
- const SizedBox(width: 10),
- Expanded(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- log.summary,
- style: TextStyle(fontSize: 13, color: colors.textPrimary),
- ),
- const SizedBox(height: 2),
- Text(
- '${log.operator} · ${log.time}',
- style: TextStyle(fontSize: 11, color: colors.textPlaceholder),
- ),
- ],
- ),
- ),
- ],
- ),
- );
- }
- // ── 保存按钮 ──
- Widget _buildSaveButton() {
- final colors = Theme.of(context).extension<AppColorsExtension>()!;
- final l10n = AppLocalizations.of(context);
- return Container(
- width: double.infinity,
- padding: const EdgeInsets.all(16),
- decoration: BoxDecoration(
- color: colors.bgCard,
- boxShadow: const [
- BoxShadow(
- color: Color(0x15000000),
- blurRadius: 8,
- offset: Offset(0, -2),
- ),
- ],
- ),
- child: GestureDetector(
- onTap: () {
- if (_isSelfAdmin && !(_checked['admin.permissions'] ?? false)) {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text('无法取消自己的管理员权限'),
- duration: Duration(seconds: 2),
- ),
- );
- return;
- }
- widget.onSave();
- },
- child: Container(
- width: double.infinity,
- padding: const EdgeInsets.symmetric(vertical: 14),
- decoration: BoxDecoration(
- color: colors.primary,
- borderRadius: BorderRadius.circular(22),
- ),
- child: Text(
- l10n.get('confirmSave'),
- textAlign: TextAlign.center,
- style: const TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.w600,
- color: Colors.white,
- ),
- ),
- ),
- ),
- );
- }
- }
- class _ChangeLog {
- final String time;
- final String operator;
- final String summary;
- const _ChangeLog({
- required this.time,
- required this.operator,
- required this.summary,
- });
- }
|