import 'package:flutter/material.dart'; import 'package:flutter_common/widget/chart.dart'; import 'package:flutter_common/widget/common_widget.dart'; import 'package:food_hub_app/provider/food_provider.dart'; import 'package:liquid_glass_widgets/liquid_glass_widgets.dart'; import 'package:provider/provider.dart'; class StatsPage extends StatefulWidget { const StatsPage({super.key}); @override State createState() => _StatsPage(); } class _StatsPage extends State { final double chartHeight = 400; @override void initState() { super.initState(); // 初始化加载数据 WidgetsBinding.instance.addPostFrameCallback((_) { context.read().refreshFoodStats(); }); } Widget _buildSummaryStats(BuildContext context, FoodProvider provider) { final colors = Theme.of(context).colorScheme; return GridView.count( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), // 禁用网格自身滚动 crossAxisCount: 2, crossAxisSpacing: 8, mainAxisSpacing: 8, childAspectRatio: 2, children: [ GlassCard( useOwnLayer: true, settings: LiquidGlassSettings(glassColor: colors.surface), child: StatsCard( icon: Icons.restaurant_menu, title: '菜谱总数', value: provider.summaryStats.recipeCount.toString(), unit: '个', ), ), GlassCard( useOwnLayer: true, settings: LiquidGlassSettings(glassColor: colors.surface), child: StatsCard( icon: Icons.grid_view_rounded, title: '菜谱类别', value: provider.summaryStats.categoryCount.toString(), unit: '种', ), ), GlassCard( useOwnLayer: true, settings: LiquidGlassSettings(glassColor: colors.surface), child: StatsCard( icon: Icons.flag, title: '做菜次数', value: provider.summaryStats.workCount.toString(), unit: '个', ), ), GlassCard( useOwnLayer: true, settings: LiquidGlassSettings(glassColor: colors.surface), child: StatsCard( icon: Icons.show_chart, title: '平均次数', value: provider.averageRecordCount.toStringAsFixed(2), unit: '次/月', ), ), ], ); } Widget _buildContent(BuildContext context, FoodProvider provider) { final colors = Theme.of(context).colorScheme; return Column( children: [ _buildSummaryStats(context, provider), SizedBox(height: 8), SizedBox( height: chartHeight, child: GlassCard( useOwnLayer: true, settings: LiquidGlassSettings(glassColor: colors.surface), child: LineChart( title: '记录统计', xAxisName: '日期', yAxisName: '次数', unit: '次', data: provider.recordStats, ), ), ), SizedBox(height: 8), SizedBox( height: chartHeight, child: GlassCard( useOwnLayer: true, settings: LiquidGlassSettings(glassColor: colors.surface), child: PieChart( title: '菜谱统计', unit: '个', data: provider.categoryStats, ), ), ), SizedBox(height: 8), SizedBox( height: chartHeight, child: GlassCard( useOwnLayer: true, settings: LiquidGlassSettings(glassColor: colors.surface), child: RankChart(title: '排行榜', data: provider.rankStats, unit: '次'), ), ), ], ); } @override Widget build(BuildContext context) { final provider = context.watch(); return Scaffold( backgroundColor: Colors.transparent, appBar: AppBar( backgroundColor: Theme.of(context).scaffoldBackgroundColor, title: Text('统计', style: Theme.of(context).textTheme.titleLarge), centerTitle: true, automaticallyImplyLeading: false, ), body: Padding( padding: EdgeInsetsGeometry.all(10), child: Stack( children: [ if (provider.isLoading) buildLoadingIndicator() else SingleChildScrollView( padding: const EdgeInsets.only(bottom: 90), child: _buildContent(context, provider), ), ], ), ), ); } }