| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721 |
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:go_router/go_router.dart';
- import '../../core/theme/app_colors.dart';
- import '../shell/nav_bar_config.dart';
- import '../../core/utils/responsive.dart';
- import '../../shared/widgets/action_bar.dart';
- import '../../shared/widgets/form_section.dart';
- import '../../core/i18n/app_localizations.dart';
- class AnnouncementCreatePage extends ConsumerStatefulWidget {
- const AnnouncementCreatePage({super.key});
- @override
- ConsumerState<AnnouncementCreatePage> createState() =>
- _AnnouncementCreatePageState();
- }
- class _AnnouncementCreatePageState
- extends ConsumerState<AnnouncementCreatePage> {
- final _titleCtrl = TextEditingController();
- final _contentCtrl = TextEditingController();
- String _type = '通知公告';
- bool _isTop = false;
- DateTime? _expiryDate;
- // 接收范围
- int _scopeMode = 0; // 0=全员, 1=按部门, 2=按指定用户
- final List<String> _selectedDepts = [];
- final List<String> _selectedUsers = [];
- final int _totalCoverage = 128; // 模拟覆盖人数
- // 附件模拟
- final List<String> _attachments = [];
- static const int _maxAttachments = 5;
- final _types = ['通知公告', '人事与制度', '放假与活动'];
- final List<String> _mockDepts = [
- '市场部',
- '技术部',
- '销售部',
- '财务部',
- '人力资源部',
- '行政管理部',
- ];
- @override
- void dispose() {
- _titleCtrl.dispose();
- _contentCtrl.dispose();
- super.dispose();
- }
- Future<void> _pickType() async {
- final l10n = AppLocalizations.of(context);
- final result = await showDialog<String>(
- context: context,
- builder: (ctx) => SimpleDialog(
- title: Text(l10n.get('announcementTypes')),
- children: _types
- .map(
- (t) => SimpleDialogOption(
- onPressed: () => Navigator.pop(ctx, t),
- child: Text(t),
- ),
- )
- .toList(),
- ),
- );
- if (result != null) setState(() => _type = result);
- }
- Future<void> _pickExpiryDate() async {
- final picked = await showDatePicker(
- context: context,
- initialDate: _expiryDate ?? DateTime.now().add(const Duration(days: 30)),
- firstDate: DateTime.now(),
- lastDate: DateTime(2030),
- );
- if (picked != null) setState(() => _expiryDate = picked);
- }
- void _showScopeDrawer() {
- showModalBottomSheet(
- context: context,
- isScrollControlled: true,
- shape: const RoundedRectangleBorder(
- borderRadius: BorderRadius.vertical(top: Radius.circular(12)),
- ),
- builder: (ctx) => _buildScopeDrawerContent(ctx),
- );
- }
- Widget _buildScopeDrawerContent(BuildContext ctx) {
- return StatefulBuilder(
- builder: (context, setInnerState) {
- return Container(
- height: MediaQuery.of(context).size.height * 0.7,
- padding: const EdgeInsets.all(16),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const Text('接收范围',
- style: TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.w600,
- color: AppColors.textPrimary)),
- GestureDetector(
- onTap: () => Navigator.pop(context),
- child: const Icon(Icons.close, size: 20),
- ),
- ],
- ),
- const SizedBox(height: 16),
- RadioGroup<int>(
- groupValue: _scopeMode,
- onChanged: (v) {
- setInnerState(() => _scopeMode = v!);
- setState(() {});
- },
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- _buildScopeOption(
- context,
- '全员',
- '所有员工均可查看',
- 0,
- setInnerState,
- ),
- const SizedBox(height: 8),
- _buildScopeOption(
- context,
- '按部门',
- '按部门树多选',
- 1,
- setInnerState,
- ),
- const SizedBox(height: 8),
- _buildScopeOption(
- context,
- '按指定用户',
- '按员工搜索多选',
- 2,
- setInnerState,
- ),
- ],
- ),
- ),
- if (_scopeMode == 1) ...[
- const SizedBox(height: 12),
- const Text('选择部门',
- style: TextStyle(
- fontSize: 13, color: AppColors.textSecondary)),
- const SizedBox(height: 4),
- ..._mockDepts.map(
- (dept) => CheckboxListTile(
- title: Text(dept,
- style: const TextStyle(fontSize: 14)),
- value: _selectedDepts.contains(dept),
- onChanged: (checked) {
- setInnerState(() {
- if (checked == true) {
- _selectedDepts.add(dept);
- } else {
- _selectedDepts.remove(dept);
- }
- });
- setState(() {});
- },
- dense: true,
- contentPadding: EdgeInsets.zero,
- ),
- ),
- ],
- if (_scopeMode == 2) ...[
- const SizedBox(height: 12),
- const Text('搜索员工',
- style: TextStyle(
- fontSize: 13, color: AppColors.textSecondary)),
- const SizedBox(height: 4),
- Container(
- padding:
- const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
- decoration: BoxDecoration(
- color: AppColors.bgPage,
- borderRadius: BorderRadius.circular(4),
- ),
- child: TextField(
- decoration: const InputDecoration(
- hintText: '输入姓名或工号搜索',
- hintStyle: TextStyle(
- fontSize: 14, color: AppColors.textPlaceholder),
- border: InputBorder.none,
- isDense: true,
- ),
- style: const TextStyle(
- fontSize: 14, color: AppColors.textPrimary),
- ),
- ),
- const SizedBox(height: 8),
- Text(
- '已选 ${_selectedUsers.length} 人',
- style: const TextStyle(
- fontSize: 12, color: AppColors.textSecondary),
- ),
- ],
- const Spacer(),
- // 覆盖统计
- Container(
- padding: const EdgeInsets.all(12),
- decoration: BoxDecoration(
- color: AppColors.primaryLight,
- borderRadius: BorderRadius.circular(8),
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const Text('覆盖人数',
- style: TextStyle(
- fontSize: 14, color: AppColors.textPrimary)),
- Text(
- '${_scopeMode == 0 ? _totalCoverage : (_scopeMode == 1 ? _selectedDepts.length * 15 : _selectedUsers.length)} 人',
- style: const TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.w700,
- color: AppColors.primary),
- ),
- ],
- ),
- ),
- const SizedBox(height: 12),
- SizedBox(
- width: double.infinity,
- child: ElevatedButton(
- onPressed: () => Navigator.pop(context),
- style: ElevatedButton.styleFrom(
- backgroundColor: AppColors.primary,
- foregroundColor: Colors.white,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(8)),
- padding: const EdgeInsets.symmetric(vertical: 12),
- ),
- child: const Text('确认'),
- ),
- ),
- ],
- ),
- );
- },
- );
- }
- Widget _buildScopeOption(
- BuildContext context,
- String title,
- String subtitle,
- int mode,
- void Function(void Function()) setInnerState,
- ) {
- final selected = _scopeMode == mode;
- return GestureDetector(
- onTap: () {
- setInnerState(() => _scopeMode = mode);
- setState(() {});
- },
- child: Container(
- padding: const EdgeInsets.all(12),
- decoration: BoxDecoration(
- color: selected ? AppColors.primaryLight : AppColors.bgPage,
- borderRadius: BorderRadius.circular(8),
- border: Border.all(
- color: selected ? AppColors.primary : Colors.transparent,
- ),
- ),
- child: Row(
- children: [
- Radio<int>(
- value: mode,
- activeColor: AppColors.primary,
- ),
- const SizedBox(width: 8),
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(title,
- style: const TextStyle(
- fontSize: 14, color: AppColors.textPrimary)),
- Text(subtitle,
- style: const TextStyle(
- fontSize: 12, color: AppColors.textSecondary)),
- ],
- ),
- ],
- ),
- ),
- );
- }
- void _showPreview() {
- final l10n = AppLocalizations.of(context);
- showDialog(
- context: context,
- barrierDismissible: true,
- builder: (ctx) => Dialog(
- insetPadding: const EdgeInsets.all(16),
- child: Container(
- width: double.infinity,
- height: MediaQuery.of(context).size.height * 0.85,
- padding: const EdgeInsets.all(16),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(l10n.get('previewTitle'),
- style: const TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.w600,
- color: AppColors.textPrimary)),
- GestureDetector(
- onTap: () => Navigator.pop(ctx),
- child: const Icon(Icons.close, size: 20),
- ),
- ],
- ),
- const Divider(),
- Expanded(
- child: SingleChildScrollView(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Container(
- width: 60,
- height: 4,
- color: AppColors.danger,
- ),
- const SizedBox(height: 16),
- Text(
- _titleCtrl.text.isEmpty ? '(未填写标题)' : _titleCtrl.text,
- style: const TextStyle(
- fontSize: 20,
- fontWeight: FontWeight.w700,
- color: AppColors.textPrimary,
- ),
- ),
- const SizedBox(height: 12),
- Text(
- '$_type · 发布后将显示',
- style: const TextStyle(
- fontSize: 13, color: AppColors.textSecondary),
- ),
- const SizedBox(height: 16),
- Container(
- width: double.infinity,
- height: 2,
- color: AppColors.danger,
- ),
- const SizedBox(height: 16),
- Text(
- _contentCtrl.text.isEmpty
- ? '(未填写正文)'
- : _contentCtrl.text,
- style: const TextStyle(
- fontSize: 14,
- color: AppColors.textSecondary,
- height: 1.7),
- ),
- ],
- ),
- ),
- ),
- ],
- ),
- ),
- ),
- );
- }
- void _confirmPublish() {
- final l10n = AppLocalizations.of(context);
- showDialog(
- context: context,
- builder: (ctx) => AlertDialog(
- title: Text(l10n.get('confirmPublishTitle')),
- content: Text(l10n.getString('confirmPublishContent', args: {'title': _titleCtrl.text})),
- actions: [
- TextButton(
- onPressed: () => Navigator.pop(ctx),
- child: Text(l10n.get('cancel')),
- ),
- TextButton(
- onPressed: () {
- Navigator.pop(ctx);
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(content: Text(l10n.get('announcementPublished'))),
- );
- context.pop();
- },
- child: Text(l10n.get('confirmPublish'),
- style: const TextStyle(color: AppColors.primary)),
- ),
- ],
- ),
- );
- }
- void _saveDraft() {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(content: Text('已保存为草稿')),
- );
- }
- void _pickAttachment() {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(content: Text('模拟:选择附件(PDF/图片/Word/Excel,≤20MB)')),
- );
- }
- @override
- Widget build(BuildContext context) {
- final r = ResponsiveHelper.of(context);
- final l10n = AppLocalizations.of(context);
- ref
- .read(navBarConfigProvider.notifier)
- .update(
- NavBarConfig(
- title: l10n.get('announcementCreate'),
- showBack: true,
- showRight: true,
- rightWidget: TextButton(
- onPressed: _showPreview,
- child: Text(l10n.get('preview'),
- style: const TextStyle(
- fontSize: 14,
- color: AppColors.primary,
- fontWeight: FontWeight.w500)),
- ),
- onBack: () => context.pop(),
- ),
- );
- return Column(
- children: [
- Expanded(
- child: Align(
- alignment: Alignment.topCenter,
- child: ConstrainedBox(
- constraints: BoxConstraints(maxWidth: r.formMaxWidth),
- child: SingleChildScrollView(
- padding: const EdgeInsets.symmetric(vertical: 8),
- child: Column(
- children: [
- // 基本信息
- FormSection(
- title: l10n.get('basicInfo'),
- children: [
- Container(
- padding: const EdgeInsets.symmetric(
- horizontal: 10, vertical: 4),
- decoration: BoxDecoration(
- color: AppColors.bgPage,
- borderRadius: BorderRadius.circular(4),
- ),
- child: TextField(
- controller: _titleCtrl,
- decoration: InputDecoration(
- hintText: l10n.get('enterTitle'),
- hintStyle: TextStyle(
- fontSize: 14,
- color: AppColors.textPlaceholder),
- border: InputBorder.none,
- contentPadding:
- EdgeInsets.symmetric(vertical: 10),
- isDense: true,
- ),
- style: const TextStyle(
- fontSize: 14,
- color: AppColors.textPrimary),
- ),
- ),
- const SizedBox(height: 12),
- GestureDetector(
- onTap: _pickType,
- child: Container(
- padding: const EdgeInsets.symmetric(
- horizontal: 10, vertical: 12),
- decoration: BoxDecoration(
- color: AppColors.bgPage,
- borderRadius: BorderRadius.circular(4),
- ),
- child: Row(
- mainAxisAlignment:
- MainAxisAlignment.spaceBetween,
- children: [
- Text(_type,
- style: const TextStyle(
- fontSize: 14,
- color: AppColors.textPrimary)),
- const Icon(Icons.chevron_right,
- size: 14,
- color: AppColors.textPlaceholder),
- ],
- ),
- ),
- ),
- ],
- ),
- const SizedBox(height: 8),
- // 公告正文
- FormSection(
- title: l10n.get('announcementContent'),
- children: [
- Container(
- padding: const EdgeInsets.all(12),
- decoration: BoxDecoration(
- color: AppColors.bgPage,
- borderRadius: BorderRadius.circular(4),
- ),
- height: 200,
- child: TextField(
- controller: _contentCtrl,
- maxLines: null,
- expands: true,
- textAlignVertical: TextAlignVertical.top,
- decoration: InputDecoration(
- hintText: l10n.get('enterContent'),
- hintStyle: TextStyle(
- fontSize: 14,
- color: AppColors.textPlaceholder),
- border: InputBorder.none,
- contentPadding: EdgeInsets.zero,
- ),
- style: const TextStyle(
- fontSize: 14,
- color: AppColors.textPrimary,
- height: 1.7),
- ),
- ),
- ],
- ),
- const SizedBox(height: 8),
- // 附件
- FormSection(
- title: l10n.get('attachments'),
- actionText: _attachments.length >= _maxAttachments
- ? l10n.get('limitReached')
- : l10n.get('add'),
- showAction: _attachments.length < _maxAttachments,
- actionIcon: Icons.attach_file,
- onActionTap: _pickAttachment,
- children: [
- if (_attachments.isEmpty)
- const Text('最多5个附件,支持PDF/图片/Word/Excel,单文件≤20MB',
- style: TextStyle(
- fontSize: 12,
- color: AppColors.textPlaceholder))
- else
- Wrap(
- spacing: 8,
- runSpacing: 8,
- children: _attachments
- .asMap()
- .entries
- .map((entry) {
- return Chip(
- label: Text(entry.value,
- style: const TextStyle(
- fontSize: 12)),
- deleteIcon: const Icon(Icons.close,
- size: 16),
- onDeleted: () {
- setState(() =>
- _attachments.removeAt(entry.key));
- },
- );
- }).toList(),
- ),
- ],
- ),
- const SizedBox(height: 8),
- // 发布设置
- FormSection(
- title: l10n.get('publishSettings'),
- children: [
- _buildSwitchRow(l10n.get('pinAnnouncement'), _isTop, (v) {
- setState(() => _isTop = v);
- }),
- const Divider(height: 1, color: AppColors.border),
- GestureDetector(
- onTap: _pickExpiryDate,
- child: Container(
- height: 44,
- alignment: Alignment.centerLeft,
- child: Row(
- mainAxisAlignment:
- MainAxisAlignment.spaceBetween,
- children: [
- Text(l10n.get('validUntil'),
- style: TextStyle(
- fontSize: 14,
- color: AppColors.textSecondary)),
- Text(
- _expiryDate != null
- ? '${_expiryDate!.year}-${_expiryDate!.month.toString().padLeft(2, '0')}-${_expiryDate!.day.toString().padLeft(2, '0')}'
- : l10n.get('expiryNever'),
- style: TextStyle(
- fontSize: 14,
- color: _expiryDate != null
- ? AppColors.primary
- : AppColors.textPlaceholder,
- ),
- ),
- const Icon(Icons.chevron_right,
- size: 14,
- color: AppColors.textPlaceholder),
- ],
- ),
- ),
- ),
- const Divider(height: 1, color: AppColors.border),
- GestureDetector(
- onTap: _showScopeDrawer,
- child: Container(
- height: 44,
- alignment: Alignment.centerLeft,
- child: Row(
- mainAxisAlignment:
- MainAxisAlignment.spaceBetween,
- children: [
- Text(l10n.get('recipientScope'),
- style: TextStyle(
- fontSize: 14,
- color: AppColors.textSecondary)),
- Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- Text(
- _scopeMode == 0
- ? l10n.get('allStaff')
- : _scopeMode == 1
- ? '${l10n.get('byDept')}(${_selectedDepts.length})'
- : '${l10n.get('byUser')}(${_selectedUsers.length})',
- style: const TextStyle(
- fontSize: 14,
- color: AppColors.primary),
- ),
- Text(
- ' · $_totalCoverage 人',
- style: const TextStyle(
- fontSize: 12,
- color:
- AppColors.textPlaceholder),
- ),
- const SizedBox(width: 4),
- const Icon(Icons.chevron_right,
- size: 14,
- color: AppColors.textPlaceholder),
- ],
- ),
- ],
- ),
- ),
- ),
- ],
- ),
- const SizedBox(height: 80),
- ],
- ),
- ),
- ),
- ),
- ),
- ActionBar(
- centerLabel: l10n.get('saveDraft'),
- rightLabel: l10n.get('publish'),
- showLeft: false,
- onCenterTap: _saveDraft,
- onRightTap: _confirmPublish,
- ),
- ],
- );
- }
- Widget _buildSwitchRow(
- String label, bool value, ValueChanged<bool> onChanged) {
- return Container(
- height: 44,
- alignment: Alignment.centerLeft,
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(label,
- style: const TextStyle(
- fontSize: 14, color: AppColors.textSecondary)),
- Switch(
- value: value,
- onChanged: onChanged,
- activeThumbColor: AppColors.primary,
- ),
- ],
- ),
- );
- }
- }
|