241 lines
6.2 KiB
Dart
241 lines
6.2 KiB
Dart
import 'package:provider/provider.dart';
|
|
import 'package:blog_app/provider/blog.dart';
|
|
import 'package:blog_app/widget/blog.dart';
|
|
import 'package:blog_app/widget/chart.dart';
|
|
import 'package:blog_app/widget/common.dart';
|
|
import 'package:blog_app/widget/year_selector.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class StatsPage extends StatefulWidget {
|
|
const StatsPage({super.key});
|
|
|
|
@override
|
|
StatsPageState createState() => StatsPageState();
|
|
}
|
|
|
|
class StatsPageState extends State<StatsPage> {
|
|
final double chartHeight = 400;
|
|
final double statsHeight = 120;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
// 初始化加载数据
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
context.read<BlogProvider>().refreshBlogStats();
|
|
});
|
|
}
|
|
|
|
Widget _buildApprovedStats(BlogProvider provider) {
|
|
return Column(
|
|
children: [
|
|
buildChartTitle(context, '每月博客发布统计', Icons.show_chart),
|
|
const SizedBox(height: 3),
|
|
buildChartDivider(context),
|
|
const SizedBox(height: 3),
|
|
Expanded(
|
|
child: lineChart(
|
|
context: context,
|
|
xAxisName: '月份',
|
|
yAxisName: '数量',
|
|
unit: '篇',
|
|
data: provider.approvedStats,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildVisitStats(BlogProvider provider) {
|
|
return Column(
|
|
children: [
|
|
buildChartTitle(context, '每月博客访问统计', Icons.show_chart),
|
|
const SizedBox(height: 3),
|
|
buildChartDivider(context),
|
|
const SizedBox(height: 3),
|
|
Expanded(
|
|
child: lineChart(
|
|
context: context,
|
|
xAxisName: '月份',
|
|
yAxisName: '访问量',
|
|
unit: '人次',
|
|
data: provider.visitStats,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildVisitRankStats(BlogProvider provider) {
|
|
return Column(
|
|
children: [
|
|
buildChartTitle(context, '博客访问数量排行', Icons.bar_chart),
|
|
const SizedBox(height: 3),
|
|
buildChartDivider(context),
|
|
const SizedBox(height: 3),
|
|
Expanded(
|
|
child: barChart(
|
|
context: context,
|
|
xAxisName: '名称',
|
|
yAxisName: '访问量',
|
|
unit: '人次',
|
|
data: provider.visitRankStats,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildCategoryStats(BlogProvider provider) {
|
|
return Column(
|
|
children: [
|
|
buildChartTitle(context, '博客类别统计', Icons.pie_chart),
|
|
const SizedBox(height: 3),
|
|
buildChartDivider(context),
|
|
const SizedBox(height: 3),
|
|
Expanded(
|
|
child: pieChart(
|
|
context: context,
|
|
unit: '篇',
|
|
data: provider.categoryStats,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildReadRankStats(BlogProvider provider) {
|
|
return Column(
|
|
children: [
|
|
buildChartTitle(context, '博客阅读时长排行', Icons.bar_chart),
|
|
const SizedBox(height: 3),
|
|
buildChartDivider(context),
|
|
const SizedBox(height: 3),
|
|
Expanded(
|
|
child: barChart(
|
|
context: context,
|
|
xAxisName: '名称',
|
|
yAxisName: '时长',
|
|
unit: '分钟',
|
|
data: provider.readRankStats,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildBlogStatsGrid(BlogProvider provider) {
|
|
return GridView.count(
|
|
crossAxisCount: 2,
|
|
shrinkWrap: true,
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
childAspectRatio: 2,
|
|
children: [
|
|
buildStatsCard(
|
|
context: context,
|
|
title: '博客数量',
|
|
count: provider.overviewStats.blogCount,
|
|
unit: '篇',
|
|
),
|
|
buildStatsCard(
|
|
context: context,
|
|
title: '分类数量',
|
|
count: provider.overviewStats.categoryCount,
|
|
unit: '个',
|
|
),
|
|
buildStatsCard(
|
|
context: context,
|
|
title: '访问量',
|
|
count: provider.overviewStats.visitCount,
|
|
unit: '次',
|
|
),
|
|
buildStatsCard(
|
|
context: context,
|
|
title: '总字数',
|
|
count: provider.overviewStats.wordCount,
|
|
unit: '字',
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildContent(BlogProvider provider) {
|
|
if (provider.error != null) {
|
|
return buildErrorInfo(
|
|
errorInfo: provider.error!,
|
|
onPressed: () => provider.refreshBlogStats(),
|
|
);
|
|
}
|
|
|
|
return Column(
|
|
children: [
|
|
YearSelector(
|
|
initialYear: provider.currentYear,
|
|
minYear: 2000,
|
|
maxYear: 2100,
|
|
onYearChanged: (year) {
|
|
setState(() {
|
|
provider.currentYear = year;
|
|
provider.refreshBlogStats();
|
|
});
|
|
},
|
|
),
|
|
const SizedBox(height: 6),
|
|
_buildBlogStatsGrid(provider),
|
|
const SizedBox(height: 6),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: buildCard(
|
|
context: context,
|
|
child: _buildApprovedStats(provider),
|
|
),
|
|
),
|
|
const SizedBox(height: 6),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: buildCard(context: context, child: _buildVisitStats(provider)),
|
|
),
|
|
const SizedBox(height: 6),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: buildCard(
|
|
context: context,
|
|
child: _buildVisitRankStats(provider),
|
|
),
|
|
),
|
|
const SizedBox(height: 6),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: buildCard(
|
|
context: context,
|
|
child: _buildCategoryStats(provider),
|
|
),
|
|
),
|
|
const SizedBox(height: 6),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: buildCard(
|
|
context: context,
|
|
child: _buildReadRankStats(provider),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final blogProvider = context.watch<BlogProvider>();
|
|
|
|
return Stack(
|
|
children: [
|
|
if (blogProvider.isLoading)
|
|
buildLoadingIndicator()
|
|
else
|
|
SingleChildScrollView(child: _buildContent(blogProvider))
|
|
]
|
|
);
|
|
}
|
|
}
|