feat:增加加载中组件
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_hub_app/apis/stats.dart';
|
||||
import 'package:food_hub_app/models/stats.dart';
|
||||
import 'package:food_hub_app/provider/food_provider.dart';
|
||||
import 'package:food_hub_app/widgets/common/chart.dart';
|
||||
import 'package:food_hub_app/widgets/common/index.dart';
|
||||
import 'package:food_hub_app/widgets/stats/card.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class StatsPage extends StatefulWidget {
|
||||
const StatsPage({super.key});
|
||||
@@ -15,70 +15,17 @@ class StatsPage extends StatefulWidget {
|
||||
class _StatsPage extends State<StatsPage> {
|
||||
final double chartHeight = 400;
|
||||
|
||||
SummaryStats summaryStats = SummaryStats(
|
||||
recipeCount: 0,
|
||||
categoryCount: 0,
|
||||
workCount: 0,
|
||||
);
|
||||
|
||||
late double averageRecordCount = 0;
|
||||
|
||||
List<ChartData> recordStats = [];
|
||||
List<ChartData> categoryStats = [];
|
||||
List<ChartData> rankStats = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
refreshStats();
|
||||
}
|
||||
|
||||
Future<void> refreshStats() async {
|
||||
final result1 = await queryStatsApi();
|
||||
final result2 = await queryRecordStatsApi();
|
||||
final result3 = await queryCategoryStatsApi();
|
||||
final result4 = await queryRankStatsApi();
|
||||
|
||||
setState(() {
|
||||
summaryStats = result1;
|
||||
recordStats = result2;
|
||||
categoryStats = result3;
|
||||
rankStats = result4;
|
||||
|
||||
if (recordStats.isNotEmpty) {
|
||||
double sumValue = recordStats.fold(
|
||||
0.0,
|
||||
(sum, item) => sum + item.value,
|
||||
);
|
||||
averageRecordCount = sumValue / recordStats.length;
|
||||
}
|
||||
// 初始化加载数据
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<FoodProvider>().refreshBlogStats();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
_buildSummaryStats(),
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: buildCard(context: context, child: _buildRecordStats()),
|
||||
),
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: buildCard(context: context, child: _buildCategoryStats()),
|
||||
),
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: buildCard(context: context, child: _buildRankStats()),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSummaryStats() {
|
||||
Widget _buildSummaryStats(FoodProvider provider) {
|
||||
return GridView.count(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
@@ -92,40 +39,40 @@ class _StatsPage extends State<StatsPage> {
|
||||
icon: Icons.restaurant_menu,
|
||||
color: Colors.blue,
|
||||
title: '菜谱总数',
|
||||
value: summaryStats.recipeCount,
|
||||
value: provider.summaryStats.recipeCount,
|
||||
unit: '个',
|
||||
),
|
||||
StatisticCard(
|
||||
icon: Icons.grid_view_rounded,
|
||||
color: Colors.green,
|
||||
title: '菜谱类别',
|
||||
value: summaryStats.categoryCount,
|
||||
value: provider.summaryStats.categoryCount,
|
||||
unit: '种',
|
||||
),
|
||||
StatisticCard(
|
||||
icon: Icons.flag,
|
||||
color: Colors.orange,
|
||||
title: '做菜次数',
|
||||
value: summaryStats.workCount,
|
||||
value: provider.summaryStats.workCount,
|
||||
unit: '个',
|
||||
),
|
||||
StatisticCard(
|
||||
icon: Icons.show_chart,
|
||||
color: Colors.red,
|
||||
title: '平均次数',
|
||||
value: double.parse(averageRecordCount.toStringAsFixed(2)),
|
||||
value: double.parse(provider.averageRecordCount.toStringAsFixed(2)),
|
||||
unit: '次/月',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecordStats() {
|
||||
Widget _buildRecordStats(FoodProvider provider) {
|
||||
return Column(
|
||||
children: [
|
||||
_buildTitleSection('记录统计'),
|
||||
buildChartTitle(context: context, title: '记录统计'),
|
||||
const SizedBox(height: 3),
|
||||
_buildDivider(),
|
||||
buildChartDivider(context: context),
|
||||
const SizedBox(height: 3),
|
||||
Expanded(
|
||||
child: lineChart(
|
||||
@@ -133,56 +80,80 @@ class _StatsPage extends State<StatsPage> {
|
||||
xAxisName: '日期',
|
||||
yAxisName: '次数',
|
||||
unit: '次',
|
||||
data: recordStats,
|
||||
data: provider.recordStats,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCategoryStats() {
|
||||
Widget _buildCategoryStats(FoodProvider provider) {
|
||||
return Column(
|
||||
children: [
|
||||
_buildTitleSection('菜谱统计'),
|
||||
buildChartTitle(context: context, title: '菜谱统计'),
|
||||
const SizedBox(height: 3),
|
||||
_buildDivider(),
|
||||
buildChartDivider(context: context),
|
||||
const SizedBox(height: 3),
|
||||
Expanded(
|
||||
child: pieChart(context: context, unit: '个', data: categoryStats),
|
||||
child: pieChart(
|
||||
context: context,
|
||||
unit: '个',
|
||||
data: provider.categoryStats,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRankStats() {
|
||||
Widget _buildRankStats(FoodProvider provider) {
|
||||
return Column(
|
||||
children: [
|
||||
_buildTitleSection('排行榜'),
|
||||
buildChartTitle(context: context, title: '排行榜'),
|
||||
const SizedBox(height: 3),
|
||||
_buildDivider(),
|
||||
buildChartDivider(context: context),
|
||||
const SizedBox(height: 3),
|
||||
Expanded(child: rankChart(rankStats)),
|
||||
Expanded(child: rankChart(data: provider.rankStats, unit: '次')),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// 构建标题区域
|
||||
Widget _buildTitleSection(String title) {
|
||||
return Row(
|
||||
Widget _buildContent(FoodProvider provider) {
|
||||
return Column(
|
||||
children: [
|
||||
Icon(Icons.insert_chart, color: Theme.of(context).primaryColor),
|
||||
Text(title),
|
||||
_buildSummaryStats(provider),
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: buildCard(
|
||||
context: context,
|
||||
child: _buildRecordStats(provider),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: buildCard(
|
||||
context: context,
|
||||
child: _buildCategoryStats(provider),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: chartHeight,
|
||||
child: buildCard(context: context, child: _buildRankStats(provider)),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDivider() {
|
||||
return Divider(
|
||||
height: 1,
|
||||
thickness: 1,
|
||||
color: Theme.of(context).primaryColor,
|
||||
indent: 0,
|
||||
endIndent: 0,
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final provider = context.watch<FoodProvider>();
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
if (provider.isLoading)
|
||||
buildLoadingIndicator(context)
|
||||
else
|
||||
SingleChildScrollView(child: _buildContent(provider)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user