Files
wushu/lib/views/footprint/load/notice-page.dart
2026-04-10 06:22:06 +08:00

382 lines
11 KiB
Dart

/// 时间: 2026-04-09
/// 功能: 公告信息页面
/// 介绍: 显示系统公告列表,支持下拉刷新
/// 最新变化: 添加底部"到底了"提示,防止被底部 tabs 遮住
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../utils/http/http_client.dart';
import '../../../constants/app_constants.dart';
import '../../../config/app_config.dart';
import '../../../services/get/theme_controller.dart';
/// 公告数据模型
class NoticeItem {
final String id;
final String content;
final String? author;
final String createTime;
final String updateTime;
NoticeItem({
required this.id,
required this.content,
this.author,
required this.createTime,
required this.updateTime,
});
factory NoticeItem.fromJson(Map<String, dynamic> json) {
return NoticeItem(
id: json['id'] as String? ?? '',
content: json['content'] as String? ?? '',
author: json['author'] as String?,
createTime: json['create_time'] as String? ?? '',
updateTime: json['update_time'] as String? ?? '',
);
}
}
class NoticePage extends StatefulWidget {
const NoticePage({super.key, this.showAppBar = true});
final bool showAppBar;
@override
State<NoticePage> createState() => _NoticePageState();
}
class _NoticePageState extends State<NoticePage> {
List<NoticeItem> _noticeList = [];
bool _isLoading = false;
bool _hasError = false;
String _errorMessage = '';
final ThemeController _themeController = Get.find<ThemeController>();
@override
void initState() {
super.initState();
_loadNotices();
}
Future<void> _loadNotices() async {
if (_isLoading) return;
setState(() {
_isLoading = true;
_hasError = false;
_errorMessage = '';
});
try {
final response = await HttpClient.get('app/notice_api.php');
if (response.isSuccess) {
final jsonData = response.jsonData;
if (jsonData['code'] == 0) {
final data = jsonData['data'];
if (data != null && data['list'] != null) {
final list = data['list'] as List;
if (mounted) {
setState(() {
_noticeList = list
.map(
(item) =>
NoticeItem.fromJson(item as Map<String, dynamic>),
)
.toList();
_isLoading = false;
});
}
} else {
if (mounted) {
setState(() {
_noticeList = [];
_isLoading = false;
});
}
}
} else {
throw Exception(jsonData['msg'] ?? '获取公告失败');
}
} else {
throw Exception('网络请求失败');
}
} catch (e) {
if (mounted) {
setState(() {
_hasError = true;
_errorMessage = e.toString();
_isLoading = false;
});
}
}
}
@override
Widget build(BuildContext context) {
return Obx(() {
final isDark = _themeController.isDarkMode;
final themeColor = _themeController.currentThemeColor;
if (widget.showAppBar) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
backgroundColor: isDark ? const Color(0xFF2A2A2A) : themeColor,
middle: Text(
'公告',
style: TextStyle(
color: Colors.white,
fontSize: 17,
fontWeight: FontWeight.w600,
),
),
leading: CupertinoButton(
padding: EdgeInsets.zero,
onPressed: () => Navigator.of(context).pop(),
child: Icon(CupertinoIcons.back, color: Colors.white, size: 24),
),
),
child: _buildBody(isDark, themeColor),
);
} else {
return _buildBody(isDark, themeColor);
}
});
}
Widget _buildBody(bool isDark, Color themeColor) {
if (_isLoading) {
return Center(
child: CupertinoActivityIndicator(
color: isDark ? Colors.white : themeColor,
),
);
}
if (_hasError) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
CupertinoIcons.exclamationmark_circle,
size: 64,
color: isDark ? Colors.red[300] : AppConstants.errorColor,
),
const SizedBox(height: 16),
Text(
_errorMessage,
style: TextStyle(
fontSize: 16,
color: isDark ? Colors.red[300] : AppConstants.errorColor,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
CupertinoButton.filled(
onPressed: _loadNotices,
child: const Text('重试'),
),
],
),
);
}
if (_noticeList.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
CupertinoIcons.bell_slash,
size: 64,
color: isDark ? Colors.grey[600] : Colors.grey[400],
),
const SizedBox(height: 16),
Text(
'暂无公告',
style: TextStyle(
fontSize: 16,
color: isDark ? Colors.grey[400] : Colors.grey[600],
),
),
],
),
);
}
return RefreshIndicator(
onRefresh: _loadNotices,
color: themeColor,
child: ListView.builder(
padding: EdgeInsets.fromLTRB(
16,
16,
16,
AppConfig.liquidGlassTotalHeight + 16,
),
itemCount: _noticeList.length + 1,
itemBuilder: (context, index) {
if (index == _noticeList.length) {
return _buildBottomIndicator(isDark);
}
return _buildNoticeCard(_noticeList[index], isDark, themeColor);
},
),
);
}
Widget _buildBottomIndicator(bool isDark) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 24),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 40,
height: 1,
color: isDark ? Colors.grey[700] : Colors.grey[300],
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
'到底了',
style: TextStyle(
fontSize: 12,
color: isDark ? Colors.grey[500] : Colors.grey[400],
),
),
),
Container(
width: 40,
height: 1,
color: isDark ? Colors.grey[700] : Colors.grey[300],
),
],
),
);
}
Widget _buildNoticeCard(NoticeItem notice, bool isDark, Color themeColor) {
return Container(
margin: const EdgeInsets.only(bottom: 16),
decoration: BoxDecoration(
color: isDark ? const Color(0xFF2A2A2A) : Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: isDark
? Colors.black.withValues(alpha: 0.2)
: Colors.black.withValues(alpha: 0.05),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
border: Border.all(color: themeColor.withValues(alpha: 0.2), width: 1),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: themeColor.withValues(alpha: 0.1),
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Row(
children: [
Icon(CupertinoIcons.bell, color: themeColor, size: 20),
const SizedBox(width: 8),
Expanded(
child: Text(
'公告 #${notice.id}',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: themeColor,
),
),
),
if (notice.author != null && notice.author!.isNotEmpty) ...[
Icon(
CupertinoIcons.person,
color: themeColor.withValues(alpha: 0.7),
size: 16,
),
const SizedBox(width: 4),
Text(
notice.author!,
style: TextStyle(
fontSize: 12,
color: themeColor.withValues(alpha: 0.7),
),
),
],
],
),
),
Padding(
padding: const EdgeInsets.all(16),
child: Text(
notice.content,
style: TextStyle(
fontSize: 15,
color: isDark ? Colors.white : Colors.black87,
height: 1.6,
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
child: Row(
children: [
Icon(
CupertinoIcons.clock,
size: 14,
color: isDark ? Colors.grey[400] : Colors.grey[500],
),
const SizedBox(width: 6),
Text(
'创建时间: ${notice.createTime}',
style: TextStyle(
fontSize: 12,
color: isDark ? Colors.grey[400] : Colors.grey[500],
),
),
],
),
),
if (notice.updateTime != notice.createTime)
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
child: Row(
children: [
Icon(
CupertinoIcons.refresh,
size: 14,
color: isDark ? Colors.grey[400] : Colors.grey[500],
),
const SizedBox(width: 6),
Text(
'更新时间: ${notice.updateTime}',
style: TextStyle(
fontSize: 12,
color: isDark ? Colors.grey[400] : Colors.grey[500],
),
),
],
),
),
],
),
);
}
}