home_page.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
  4. import 'package:go_router/go_router.dart';
  5. import 'package:tdesign_flutter/tdesign_flutter.dart';
  6. import '../../core/theme/app_colors.dart';
  7. import '../../shared/widgets/section_card.dart';
  8. import '../../core/i18n/app_localizations.dart';
  9. import '../shell/nav_bar_config.dart';
  10. import 'home_controller.dart';
  11. class HomePage extends ConsumerWidget {
  12. const HomePage({super.key});
  13. @override
  14. Widget build(BuildContext context, WidgetRef ref) {
  15. final summaryAsync = ref.watch(homeSummaryProvider);
  16. final location = GoRouterState.of(context).uri.toString();
  17. final l10n = AppLocalizations.of(context);
  18. if (location == '/') {
  19. ref
  20. .read(navBarConfigProvider.notifier)
  21. .update(
  22. NavBarConfig(
  23. title: l10n.get('appName'),
  24. showBack: true,
  25. leadingIcon: Icons.close,
  26. ),
  27. );
  28. }
  29. return summaryAsync.when(
  30. loading: () => const Center(child: CircularProgressIndicator()),
  31. error: (_, _) => Center(child: Text(l10n.get('loadFailed'))),
  32. data: (summary) => SingleChildScrollView(
  33. physics: const AlwaysScrollableScrollPhysics(),
  34. padding: const EdgeInsets.fromLTRB(16, 16, 16, 24),
  35. child: Column(
  36. children: [
  37. // Banner 区域
  38. _buildBanner(),
  39. const SizedBox(height: 16),
  40. // 发起
  41. SectionCard(
  42. title: l10n.get('initiate'),
  43. actionText: l10n.get('more'),
  44. actionIcon: Icons.chevron_right,
  45. onActionTap: () => context.push('/expense-apply/list'),
  46. children: [
  47. _buildGrid([
  48. _GridItem(
  49. icon: Icons.add_card,
  50. label: l10n.get('preApplication'),
  51. onTap: () => context.push('/expense-apply/apply'),
  52. ),
  53. _GridItem(
  54. icon: Icons.receipt_long,
  55. label: l10n.get('expenseReimbursement'),
  56. onTap: () => context.push('/expense/apply'),
  57. ),
  58. _GridItem(
  59. icon: Icons.directions_car,
  60. label: l10n.get('vehicleApplication'),
  61. onTap: () => context.push('/vehicle/apply'),
  62. ),
  63. _GridItem(
  64. icon: Icons.access_time,
  65. label: l10n.get('overtimeApplication'),
  66. onTap: () => context.push('/overtime/apply'),
  67. ),
  68. ]),
  69. ],
  70. ),
  71. const SizedBox(height: 16),
  72. // 记录
  73. SectionCard(
  74. title: l10n.get('records'),
  75. actionText: l10n.get('more'),
  76. actionIcon: Icons.chevron_right,
  77. onActionTap: () => context.push('/expense-apply/list'),
  78. children: [
  79. _buildGrid([
  80. _GridItem(
  81. icon: Icons.folder_copy,
  82. label: l10n.get('applicationRecords'),
  83. onTap: () => context.push('/expense-apply/list'),
  84. ),
  85. _GridItem(
  86. icon: Icons.article_outlined,
  87. label: l10n.get('expenseRecords'),
  88. onTap: () => context.push('/expense/list'),
  89. ),
  90. _GridItem(
  91. icon: Icons.push_pin_outlined,
  92. label: l10n.get('outingLogs'),
  93. onTap: () => context.push('/outing-log/list'),
  94. ),
  95. _GridItem(
  96. icon: Icons.campaign,
  97. label: l10n.get('companyAnnouncements'),
  98. onTap: () => context.push('/announcement/list'),
  99. ),
  100. ]),
  101. ],
  102. ),
  103. const SizedBox(height: 16),
  104. // 我的快捷看板
  105. SectionCard(
  106. title: l10n.get('myDashboard'),
  107. showAction: false,
  108. children: [_buildStatsRow(summary, l10n)],
  109. ),
  110. ],
  111. ),
  112. ),
  113. );
  114. }
  115. static const _banners = [
  116. 'assets/img/banner_1.png',
  117. 'assets/img/banner_2.png',
  118. 'assets/img/banner_3.png',
  119. ];
  120. Widget _buildBanner() {
  121. return ClipRRect(
  122. borderRadius: BorderRadius.circular(8),
  123. child: SizedBox(
  124. height: 160,
  125. child: Swiper(
  126. autoplay: true,
  127. itemCount: _banners.length,
  128. loop: true,
  129. pagination: const SwiperPagination(
  130. alignment: Alignment.bottomCenter,
  131. builder: TDSwiperPagination.dotsBar,
  132. ),
  133. itemBuilder: (BuildContext context, int index) {
  134. return TDImage(assetUrl: _banners[index], fit: BoxFit.cover);
  135. },
  136. ),
  137. ),
  138. );
  139. }
  140. Widget _buildGrid(List<_GridItem> items) {
  141. return Row(
  142. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  143. children: items.map((item) => _buildGridItem(item)).toList(),
  144. );
  145. }
  146. Widget _buildGridItem(_GridItem item) {
  147. return GestureDetector(
  148. onTap: item.onTap,
  149. child: SizedBox(
  150. width: 64,
  151. child: Column(
  152. mainAxisSize: MainAxisSize.min,
  153. children: [
  154. Container(
  155. width: 36,
  156. height: 36,
  157. decoration: BoxDecoration(
  158. color: AppColors.primaryLight,
  159. borderRadius: BorderRadius.circular(10),
  160. ),
  161. child: Icon(item.icon, size: 22, color: AppColors.primary),
  162. ),
  163. const SizedBox(height: 6),
  164. Text(
  165. item.label,
  166. style: const TextStyle(
  167. fontSize: AppFontSizes.caption,
  168. color: AppColors.textSecondary,
  169. ),
  170. textAlign: TextAlign.center,
  171. ),
  172. ],
  173. ),
  174. ),
  175. );
  176. }
  177. Widget _buildStatsRow(HomeSummary summary, AppLocalizations l10n) {
  178. final pendingTotal =
  179. summary.expensePending +
  180. summary.overtimePending +
  181. summary.vehiclePending;
  182. final submittedTotal =
  183. summary.expensePending +
  184. summary.overtimePending +
  185. summary.vehiclePending +
  186. summary.expenseApplyPending;
  187. return Row(
  188. crossAxisAlignment: CrossAxisAlignment.end,
  189. mainAxisAlignment: MainAxisAlignment.spaceAround,
  190. children: [
  191. _buildStat(
  192. '¥${summary.totalCount * 100}',
  193. l10n.get('monthlyTotalExpense'),
  194. AppColors.amountPrimary,
  195. ),
  196. _buildStat(
  197. '$submittedTotal',
  198. l10n.get('monthlySubmitted'),
  199. AppColors.textPrimary,
  200. ),
  201. _buildStat(
  202. '$pendingTotal',
  203. l10n.get('pendingDocuments'),
  204. AppColors.warning,
  205. ),
  206. ],
  207. );
  208. }
  209. Widget _buildStat(String value, String label, Color valueColor) {
  210. return Column(
  211. mainAxisSize: MainAxisSize.min,
  212. children: [
  213. Text(
  214. value,
  215. style: TextStyle(
  216. fontSize: 22,
  217. fontWeight: FontWeight.w700,
  218. color: valueColor,
  219. ),
  220. ),
  221. const SizedBox(height: AppSpacing.xs),
  222. Text(
  223. label,
  224. style: const TextStyle(
  225. fontSize: AppFontSizes.caption,
  226. color: AppColors.textSecondary,
  227. ),
  228. ),
  229. ],
  230. );
  231. }
  232. }
  233. class _GridItem {
  234. final IconData icon;
  235. final String label;
  236. final VoidCallback onTap;
  237. const _GridItem({
  238. required this.icon,
  239. required this.label,
  240. required this.onTap,
  241. });
  242. }