163 lines
4.3 KiB
Dart
163 lines
4.3 KiB
Dart
import 'package:flutter_common/utils/number_utils.dart';
|
|
import 'package:flutter_common/widget/chart.dart';
|
|
import 'package:flutter_common/widget/common_widget.dart';
|
|
import 'package:flutter_common/widget/year_selector.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:blog_app/provider/blog.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 _buildBlogStatsGrid(BlogProvider provider) {
|
|
return GridView.count(
|
|
crossAxisCount: 2,
|
|
shrinkWrap: true,
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
childAspectRatio: 2,
|
|
mainAxisSpacing: 8,
|
|
crossAxisSpacing: 8,
|
|
children: [
|
|
StatsCard(
|
|
icon: Icons.article,
|
|
title: '博客数量',
|
|
value: provider.overviewStats.blogCount.toString(),
|
|
unit: '篇',
|
|
),
|
|
StatsCard(
|
|
icon: Icons.folder,
|
|
title: '分类数量',
|
|
value: provider.overviewStats.categoryCount.toString(),
|
|
unit: '个',
|
|
),
|
|
StatsCard(
|
|
icon: Icons.remove_red_eye,
|
|
title: '访问量',
|
|
value: formatChineseDecimal(provider.overviewStats.visitCount),
|
|
unit: '次',
|
|
),
|
|
StatsCard(
|
|
icon: Icons.text_fields,
|
|
title: '总字数',
|
|
value: formatChineseDecimal(provider.overviewStats.wordCount),
|
|
unit: '字',
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildContent(BlogProvider provider) {
|
|
if (provider.error != null) {
|
|
return buildErrorInfo(
|
|
errorInfo: provider.error!,
|
|
onPressed: () => provider.refreshBlogStats(),
|
|
);
|
|
}
|
|
|
|
return Column(
|
|
children: [
|
|
YearSelector(
|
|
currentYear: provider.currentStatsYear,
|
|
onYearChanged: (year) {
|
|
provider.currentStatsYear = year;
|
|
provider.refreshBlogStats();
|
|
},
|
|
),
|
|
const SizedBox(height: 8),
|
|
_buildBlogStatsGrid(provider),
|
|
const SizedBox(height: 8),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: CommonCard(
|
|
child: LineChart(
|
|
title: '每月博客发布统计',
|
|
xAxisName: '月份',
|
|
yAxisName: '数量',
|
|
unit: '篇',
|
|
data: provider.approvedStats,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: CommonCard(
|
|
child: LineChart(
|
|
title: '每月博客访问统计',
|
|
xAxisName: '月份',
|
|
yAxisName: '访问量',
|
|
unit: '人次',
|
|
data: provider.visitStats,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: CommonCard(
|
|
child: RankChart(
|
|
title: '博客访问数量排行',
|
|
unit: '人次',
|
|
data: provider.visitRankStats,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: CommonCard(
|
|
child: PieChart(
|
|
title: '博客类别统计',
|
|
unit: '篇',
|
|
data: provider.categoryStats,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
SizedBox(
|
|
height: chartHeight,
|
|
child: CommonCard(
|
|
child: RankChart(
|
|
title: '博客阅读时长排行',
|
|
unit: '分钟',
|
|
data: provider.readRankStats,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final blogProvider = context.watch<BlogProvider>();
|
|
|
|
return Stack(
|
|
children: [
|
|
if (blogProvider.isLoading)
|
|
buildLoadingIndicator()
|
|
else
|
|
SingleChildScrollView(child: _buildContent(blogProvider)),
|
|
],
|
|
);
|
|
}
|
|
}
|