Initial project import

This commit is contained in:
2026-07-24 23:11:20 +08:00
commit 6396eabb87
372 changed files with 49682 additions and 0 deletions
@@ -0,0 +1,564 @@
import 'package:flutter/material.dart';
import 'package:miaoji_zhang/shared/api/api_client.dart';
import 'package:miaoji_zhang/shared/api/business_api.dart';
import 'package:miaoji_zhang/shared/api/config_api.dart';
import 'package:miaoji_zhang/shared/services/current_ledger_store.dart';
import 'package:miaoji_zhang/shared/theme/app_theme.dart';
import 'package:miaoji_zhang/shared/widgets/app_icons.dart';
import 'package:miaoji_zhang/shared/widgets/async_error_view.dart';
import 'package:miaoji_zhang/shared/widgets/category_icon.dart';
import 'package:miaoji_zhang/shared/services/shanghai_time.dart';
import 'package:miaoji_zhang/shared/services/session_store.dart';
enum _ReportKind { weekly, monthly, yearly }
class ReportPage extends StatefulWidget {
const ReportPage({super.key});
@override
State<ReportPage> createState() => _ReportPageState();
}
class _ReportPageState extends State<ReportPage> {
PeriodReport? _report;
bool _loading = true;
String? _error;
DateTime _anchor = ShanghaiTime.now;
_ReportKind _kind = _ReportKind.monthly;
@override
void initState() {
super.initState();
PublicConfigApi.companionNotifier.addListener(_refreshCompanion);
CurrentLedgerStore.instance.addListener(_load);
_load();
}
@override
void dispose() {
PublicConfigApi.companionNotifier.removeListener(_refreshCompanion);
CurrentLedgerStore.instance.removeListener(_load);
super.dispose();
}
void _refreshCompanion() {
if (mounted) setState(() {});
}
Future<void> _load() async {
setState(() {
_loading = true;
_error = null;
});
try {
final report = switch (_kind) {
_ReportKind.weekly => await ReportApi.weekly(_anchor),
_ReportKind.monthly => await ReportApi.monthlyPeriod(
_anchor.year,
_anchor.month,
),
_ReportKind.yearly => await ReportApi.yearly(_anchor.year),
};
if (mounted) setState(() => _report = report);
} catch (error) {
if (mounted) setState(() => _error = apiErrorMessage(error));
} finally {
if (mounted) setState(() => _loading = false);
}
}
void _selectKind(_ReportKind kind) {
if (_kind == kind) return;
setState(() {
_kind = kind;
_anchor = ShanghaiTime.now;
_report = null;
});
_load();
}
void _move(int direction) {
final next = switch (_kind) {
_ReportKind.weekly => _anchor.add(Duration(days: 7 * direction)),
_ReportKind.monthly => DateTime(_anchor.year, _anchor.month + direction),
_ReportKind.yearly => DateTime(_anchor.year + direction),
};
if (direction > 0 && _isFuture(next)) return;
setState(() {
_anchor = next;
_report = null;
});
_load();
}
bool _isFuture(DateTime value) {
final now = ShanghaiTime.now;
return switch (_kind) {
_ReportKind.weekly => value.isAfter(now),
_ReportKind.monthly =>
value.year > now.year ||
(value.year == now.year && value.month > now.month),
_ReportKind.yearly => value.year > now.year,
};
}
@override
Widget build(BuildContext context) {
final report = _report;
return Scaffold(
appBar: AppBar(
title: Text(SessionStore.instance.aiEnabled ? 'AI 报告' : '报告'),
),
body: Column(
children: [
_periodTabs(),
_periodNavigator(),
Expanded(
child: !_loading && report == null && _error != null
? AsyncErrorView(message: _error!, onRetry: _load)
: _loading && report == null
? Center(
child: CircularProgressIndicator(
color: AppTheme.primary,
strokeWidth: 2,
),
)
: report == null
? Center(
child: Text(
'暂无数据',
style: TextStyle(color: context.jz.text3),
),
)
: _reportBody(report),
),
],
),
);
}
Widget _periodTabs() {
const labels = {
_ReportKind.weekly: '周报',
_ReportKind.monthly: '月报',
_ReportKind.yearly: '年报',
};
return Container(
margin: const EdgeInsets.fromLTRB(16, 4, 16, 8),
padding: const EdgeInsets.all(3),
decoration: BoxDecoration(
color: context.jz.line,
borderRadius: BorderRadius.circular(11),
),
child: Row(
children: labels.entries.map((entry) {
final selected = entry.key == _kind;
return Expanded(
child: InkWell(
onTap: () => _selectKind(entry.key),
borderRadius: BorderRadius.circular(9),
child: AnimatedContainer(
duration: const Duration(milliseconds: 180),
padding: const EdgeInsets.symmetric(vertical: 8),
decoration: BoxDecoration(
color: selected ? context.jz.card : Colors.transparent,
borderRadius: BorderRadius.circular(9),
),
child: Text(
entry.value,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 12.5,
fontWeight: selected ? FontWeight.w800 : FontWeight.w500,
color: selected ? AppTheme.ai : context.jz.text2,
),
),
),
),
);
}).toList(),
),
);
}
Widget _periodNavigator() {
final label =
_report?.periodLabel ??
switch (_kind) {
_ReportKind.weekly => '本周',
_ReportKind.monthly =>
_anchor.year.toString() + '' + _anchor.month.toString() + '',
_ReportKind.yearly => _anchor.year.toString() + '',
};
return Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
tooltip: '上一个周期',
onPressed: () => _move(-1),
icon: Icon(Icons.chevron_left_rounded),
),
Container(
constraints: const BoxConstraints(minWidth: 150),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
decoration: BoxDecoration(
color: context.jz.card,
borderRadius: BorderRadius.circular(18),
border: Border.all(color: context.jz.line),
),
child: Text(
label,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 13, fontWeight: FontWeight.w700),
),
),
IconButton(
tooltip: '下一个周期',
onPressed: _isFuture(_nextAnchor()) ? null : () => _move(1),
icon: Icon(Icons.chevron_right_rounded),
),
],
),
);
}
DateTime _nextAnchor() => switch (_kind) {
_ReportKind.weekly => _anchor.add(const Duration(days: 7)),
_ReportKind.monthly => DateTime(_anchor.year, _anchor.month + 1),
_ReportKind.yearly => DateTime(_anchor.year + 1),
};
Widget _reportBody(PeriodReport report) {
return RefreshIndicator(
onRefresh: _load,
child: ListView(
padding: const EdgeInsets.fromLTRB(16, 4, 16, 24),
children: [
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: AppTheme.ai,
borderRadius: BorderRadius.circular(18),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'' + PublicConfigApi.companionName + _kindLabel + '报告',
style: TextStyle(
fontSize: 11,
color: Colors.white.withValues(alpha: 0.85),
),
),
SizedBox(height: 8),
Text(
report.periodLabel +
',一共支出 ¥' +
report.expense.toStringAsFixed(0),
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w800,
color: Colors.white,
),
),
SizedBox(height: 16),
Row(
children: [
_num('收入', report.income),
_num('支出', report.expense),
_num('结余', report.balance),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'笔数',
style: TextStyle(
fontSize: 10,
color: Colors.white.withValues(alpha: 0.8),
),
),
SizedBox(height: 3),
Text(
report.count.toString(),
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w700,
color: Colors.white,
),
),
],
),
),
],
),
],
),
),
SizedBox(height: 10),
Card(
child: Column(
children: [
if (report.peakLabel != null)
_highlight(
AppIcons.fire,
context.jz.expenseBackground,
AppTheme.red,
(_kind == _ReportKind.yearly ? '支出最高的月份:' : '最烧钱的一天:') +
report.peakLabel!,
'支出 ¥' +
report.peakAmount.toStringAsFixed(0) +
(report.peakNote != null
? ',最大一笔是“' + report.peakNote! + ''
: ''),
),
if (SessionStore.instance.aiEnabled)
_highlight(
AppIcons.chat,
context.jz.aiBackground,
AppTheme.ai,
PublicConfigApi.companionName +
'记账占比 ' +
report.aiRatio.toStringAsFixed(0) +
'%',
report.aiRatio > 50 ? '大部分账单都通过 AI 完成' : '可以试试用一句话完成记账',
),
],
),
),
if (report.categoryRanking.isNotEmpty) ...[
SizedBox(height: 10),
_categoryRanking(report.categoryRanking),
],
SizedBox(height: 10),
if (SessionStore.instance.aiEnabled)
Container(
padding: const EdgeInsets.all(14),
decoration: BoxDecoration(
color: context.jz.aiBackground,
borderRadius: BorderRadius.circular(14),
border: Border.all(color: AppTheme.ai.withValues(alpha: 0.12)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
AppIcons.icon(
AppIcons.avatarAsset(
PublicConfigApi.companionAvatarKey,
),
size: 15,
color: AppTheme.ai,
),
SizedBox(width: 7),
Text(
PublicConfigApi.companionName + '点评',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w700,
color: AppTheme.ai,
),
),
],
),
SizedBox(height: 8),
Text(
'' + report.commentary + '',
style: TextStyle(
fontSize: 12.5,
height: 1.75,
color: context.jz.text2,
),
),
],
),
),
],
),
);
}
Widget _categoryRanking(List<ReportCategoryRank> ranking) {
return Card(
child: Padding(
padding: const EdgeInsets.fromLTRB(14, 14, 14, 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'支出分类排行',
style: TextStyle(fontSize: 13.5, fontWeight: FontWeight.w800),
),
SizedBox(height: 12),
...ranking.indexed.map((entry) {
final index = entry.$1;
final item = entry.$2;
return Padding(
padding: const EdgeInsets.only(bottom: 11),
child: Row(
children: [
SizedBox(
width: 22,
child: Text(
(index + 1).toString().padLeft(2, '0'),
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w800,
color: index == 0
? AppTheme.primary
: context.jz.text3,
),
),
),
CategoryIconBox(
iconKey: item.iconKey,
colorKey: item.colorKey,
size: 32,
),
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
item.name,
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w700,
),
),
),
Text(
'¥' + item.amount.toStringAsFixed(0),
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w700,
),
),
SizedBox(width: 7),
SizedBox(
width: 38,
child: Text(
item.percent.toStringAsFixed(0) + '%',
textAlign: TextAlign.right,
style: TextStyle(
fontSize: 10.5,
color: context.jz.text3,
),
),
),
],
),
SizedBox(height: 6),
ClipRRect(
borderRadius: BorderRadius.circular(3),
child: LinearProgressIndicator(
minHeight: 5,
value: (item.percent / 100).clamp(0.0, 1.0),
backgroundColor: context.jz.line,
color: index == 0
? AppTheme.primary
: AppTheme.primaryLight,
),
),
],
),
),
],
),
);
}),
],
),
),
);
}
String get _kindLabel => switch (_kind) {
_ReportKind.weekly => '',
_ReportKind.monthly => '',
_ReportKind.yearly => '',
};
Widget _num(String label, double value) {
return Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: TextStyle(
fontSize: 10,
color: Colors.white.withValues(alpha: 0.8),
),
),
SizedBox(height: 3),
Text(
'¥' + value.toStringAsFixed(0),
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w700,
color: Colors.white,
),
),
],
),
);
}
Widget _highlight(
String iconAsset,
Color background,
Color foreground,
String title,
String description,
) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 30,
height: 30,
decoration: BoxDecoration(
color: background,
borderRadius: BorderRadius.circular(9),
),
child: Center(
child: AppIcons.icon(iconAsset, size: 16, color: foreground),
),
),
SizedBox(width: 11),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(fontSize: 12.5, fontWeight: FontWeight.w600),
),
SizedBox(height: 3),
Text(
description,
style: TextStyle(
fontSize: 11,
color: context.jz.text2,
height: 1.55,
),
),
],
),
),
],
),
);
}
}
+605
View File
@@ -0,0 +1,605 @@
import 'dart:math' as math;
import 'package:miaoji_zhang/shared/services/shanghai_time.dart';
import 'package:flutter/material.dart';
import 'package:miaoji_zhang/shared/api/api_client.dart';
import 'package:miaoji_zhang/shared/api/business_api.dart';
import 'package:miaoji_zhang/shared/services/current_ledger_store.dart';
import 'package:miaoji_zhang/shared/theme/app_theme.dart';
import 'package:miaoji_zhang/shared/widgets/app_controls.dart';
import 'package:miaoji_zhang/shared/widgets/async_error_view.dart';
import 'package:miaoji_zhang/shared/widgets/category_icon.dart';
class StatsPage extends StatefulWidget {
const StatsPage({super.key});
@override
State<StatsPage> createState() => _StatsPageState();
}
class _StatsPageState extends State<StatsPage> {
String _period = 'month';
DateTime _anchor = ShanghaiTime.now;
PeriodStats? _stats;
String? _error;
bool _loading = true;
@override
void initState() {
super.initState();
CurrentLedgerStore.instance.addListener(_load);
_load();
}
@override
void dispose() {
CurrentLedgerStore.instance.removeListener(_load);
super.dispose();
}
Future<void> _load() async {
if (mounted) {
setState(() {
_loading = true;
_error = null;
});
}
try {
final stats = await TxApi.periodStats(_period, _anchor);
if (mounted) setState(() => _stats = stats);
} catch (error) {
if (mounted) setState(() => _error = apiErrorMessage(error));
} finally {
if (mounted) setState(() => _loading = false);
}
}
void _selectPeriod(String value) {
if (_period == value) return;
setState(() {
_period = value;
_anchor = ShanghaiTime.now;
_stats = null;
});
_load();
}
void _shift(int direction) {
final next = switch (_period) {
'week' => _anchor.add(Duration(days: 7 * direction)),
'year' => DateTime(_anchor.year + direction, 1, 1),
_ => DateTime(_anchor.year, _anchor.month + direction, 1),
};
if (direction > 0 && _isAfterCurrentPeriod(next)) return;
setState(() {
_anchor = next;
_stats = null;
});
_load();
}
bool _isAfterCurrentPeriod(DateTime value) {
final now = ShanghaiTime.now;
return switch (_period) {
'week' => value.isAfter(now),
'year' => value.year > now.year,
_ =>
value.year > now.year ||
(value.year == now.year && value.month > now.month),
};
}
@override
Widget build(BuildContext context) {
final stats = _stats;
return Scaffold(
appBar: AppBar(title: Text('统计')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 4, 16, 8),
child: JzSegmentedControl<String>(
value: _period,
options: const [
JzOption(value: 'week', label: ''),
JzOption(value: 'month', label: ''),
JzOption(value: 'year', label: ''),
],
onChanged: _loading ? null : _selectPeriod,
),
),
Expanded(
child: _loading && stats == null
? Center(
child: CircularProgressIndicator(
color: AppTheme.primary,
strokeWidth: 2,
),
)
: stats == null && _error != null
? AsyncErrorView(message: _error!, onRetry: _load)
: RefreshIndicator(
onRefresh: _load,
child: ListView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.fromLTRB(16, 0, 16, 24),
children: [
_periodNavigator(stats?.periodLabel ?? ''),
if (_loading)
LinearProgressIndicator(
minHeight: 2,
color: AppTheme.primary,
backgroundColor: context.jz.primaryBackground,
),
if (_error != null) _refreshNotice(_error!),
if (stats != null) ...[
SizedBox(height: 10),
_summaryCard(stats),
SizedBox(height: 10),
if (stats.totalExpense == 0 && stats.totalIncome == 0)
_emptyCard()
else ...[
_categoryCard(stats),
SizedBox(height: 10),
if (stats.analysis case final analysis?)
_analysisCard(analysis),
SizedBox(height: 10),
_trendCard(stats),
if (stats.byCategory.isNotEmpty) ...[
SizedBox(height: 10),
_rankingCard(stats),
],
],
],
],
),
),
),
],
),
);
}
Widget _periodNavigator(String label) {
final nextAnchor = switch (_period) {
'week' => _anchor.add(const Duration(days: 7)),
'year' => DateTime(_anchor.year + 1, 1, 1),
_ => DateTime(_anchor.year, _anchor.month + 1, 1),
};
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
tooltip: '上一个周期',
onPressed: _loading ? null : () => _shift(-1),
icon: Icon(Icons.chevron_left_rounded),
),
SizedBox(
width: 170,
child: Text(
label,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w700),
),
),
IconButton(
tooltip: '下一个周期',
onPressed: _loading || _isAfterCurrentPeriod(nextAnchor)
? null
: () => _shift(1),
icon: Icon(Icons.chevron_right_rounded),
),
],
);
}
Widget _summaryCard(PeriodStats stats) {
return Card(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 17),
child: Row(
children: [
_summaryValue('支出', stats.totalExpense, AppTheme.red),
_divider(),
_summaryValue('收入', stats.totalIncome, AppTheme.primaryDeep),
_divider(),
_summaryValue(
'结余',
stats.balance,
stats.balance < 0 ? AppTheme.red : context.jz.text,
),
],
),
),
);
}
Widget _summaryValue(String label, double amount, Color color) {
return Expanded(
child: Column(
children: [
Text(
label,
style: TextStyle(fontSize: 10.5, color: context.jz.text3),
),
SizedBox(height: 5),
FittedBox(
fit: BoxFit.scaleDown,
child: Text(
'¥${amount.toStringAsFixed(2)}',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w800,
color: color,
),
),
),
],
),
);
}
Widget _divider() => Container(width: 1, height: 34, color: context.jz.line);
Widget _emptyCard() {
return Card(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 48),
child: Center(
child: Text(
'这个周期还没有账单记录',
style: TextStyle(fontSize: 13, color: context.jz.text3),
),
),
),
);
}
Widget _categoryCard(PeriodStats stats) {
return Card(
child: Padding(
padding: const EdgeInsets.fromLTRB(16, 18, 16, 16),
child: Column(
children: [
SizedBox(
height: 178,
child: _CategoryDonut(
categories: stats.byCategory,
total: stats.totalExpense,
),
),
SizedBox(height: 12),
Wrap(
spacing: 12,
runSpacing: 8,
children: stats.byCategory.take(8).map((category) {
final color = categoryColorMeta(category.colorKey).foreground;
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 9,
height: 9,
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(3),
),
),
SizedBox(width: 5),
Text(
'${category.name} ${category.percent.toStringAsFixed(0)}%',
style: TextStyle(fontSize: 10.5, color: context.jz.text2),
),
],
);
}).toList(),
),
],
),
),
);
}
Widget _analysisCard(String text) {
return Container(
padding: const EdgeInsets.all(14),
decoration: BoxDecoration(
color: context.jz.aiBackground,
borderRadius: BorderRadius.circular(14),
border: Border.all(color: AppTheme.ai.withValues(alpha: 0.12)),
),
child: Row(
children: [
Container(
width: 30,
height: 30,
decoration: BoxDecoration(
color: context.jz.card,
borderRadius: BorderRadius.circular(10),
),
child: Icon(
Icons.auto_awesome_rounded,
size: 16,
color: AppTheme.ai,
),
),
SizedBox(width: 10),
Expanded(
child: Text(
text,
style: TextStyle(
fontSize: 12,
color: context.jz.text2,
height: 1.5,
),
),
),
],
),
);
}
Widget _trendCard(PeriodStats stats) {
final title = switch (_period) {
'week' => '每日支出趋势',
'year' => '每月支出趋势',
_ => '每日支出趋势',
};
return Card(
child: Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 14),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(fontSize: 13, fontWeight: FontWeight.w700),
),
SizedBox(height: 16),
SizedBox(
height: 116,
child: _TrendBars(points: stats.trend, period: _period),
),
],
),
),
);
}
Widget _rankingCard(PeriodStats stats) {
final maxAmount = stats.byCategory.first.amount;
return Card(
child: Padding(
padding: const EdgeInsets.fromLTRB(14, 8, 14, 8),
child: Column(
children: stats.byCategory.take(8).map((category) {
final color = categoryColorMeta(category.colorKey).foreground;
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: [
CategoryIconBox(
iconKey: category.iconKey,
colorKey: category.colorKey,
size: 34,
),
SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
category.name,
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w600,
),
),
Spacer(),
Text(
'¥${category.amount.toStringAsFixed(2)}',
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w700,
),
),
],
),
SizedBox(height: 6),
ClipRRect(
borderRadius: BorderRadius.circular(3),
child: LinearProgressIndicator(
value: maxAmount == 0
? 0
: category.amount / maxAmount,
minHeight: 4,
backgroundColor: context.jz.background,
color: color,
),
),
],
),
),
],
),
);
}).toList(),
),
),
);
}
Widget _refreshNotice(String message) {
return Container(
margin: const EdgeInsets.only(top: 8),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 9),
decoration: BoxDecoration(
color: context.jz.warningBackground,
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
Icon(Icons.info_outline_rounded, size: 16, color: AppTheme.orange),
SizedBox(width: 8),
Expanded(
child: Text(
message,
style: TextStyle(fontSize: 11, color: context.jz.text2),
),
),
TextButton(onPressed: _load, child: Text('重试')),
],
),
);
}
}
class _CategoryDonut extends StatelessWidget {
final List<CategoryStat> categories;
final double total;
const _CategoryDonut({required this.categories, required this.total});
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: _DonutPainter(categories.take(8).toList(), context.jz.line),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'周期支出',
style: TextStyle(fontSize: 10.5, color: context.jz.text3),
),
SizedBox(height: 3),
Text(
'¥${total.toStringAsFixed(0)}',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w800),
),
],
),
),
);
}
}
class _DonutPainter extends CustomPainter {
final List<CategoryStat> categories;
final Color lineColor;
_DonutPainter(this.categories, this.lineColor);
@override
void paint(Canvas canvas, Size size) {
final center = Offset(size.width / 2, size.height / 2);
final radius = math.min(size.width, size.height) / 2 - 12;
const strokeWidth = 22.0;
final rect = Rect.fromCircle(center: center, radius: radius);
canvas.drawCircle(
center,
radius,
Paint()
..color = lineColor
..style = PaintingStyle.stroke
..strokeWidth = strokeWidth,
);
final total = categories.fold<double>(0, (sum, item) => sum + item.amount);
if (total == 0) return;
var start = -math.pi / 2;
for (final category in categories) {
final sweep = category.amount / total * math.pi * 2;
final gap = math.min(0.025, sweep / 4);
canvas.drawArc(
rect,
start + gap,
math.max(0, sweep - gap * 2),
false,
Paint()
..color = categoryColorMeta(category.colorKey).foreground
..style = PaintingStyle.stroke
..strokeWidth = strokeWidth
..strokeCap = StrokeCap.round,
);
start += sweep;
}
}
@override
bool shouldRepaint(covariant _DonutPainter oldDelegate) => true;
}
class _TrendBars extends StatelessWidget {
final List<PeriodTrendPoint> points;
final String period;
const _TrendBars({required this.points, required this.period});
@override
Widget build(BuildContext context) {
final maxValue = points.fold<double>(
0,
(maximum, point) => math.max(maximum, point.expense),
);
return Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: points.indexed.map((entry) {
final index = entry.$1;
final point = entry.$2;
final height = maxValue == 0
? 2.0
: math.max(2.0, point.expense / maxValue * 78).toDouble();
final showLabel =
period != 'month' ||
index == 0 ||
index == points.length - 1 ||
(index + 1) % 5 == 0;
return Expanded(
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: period == 'month' ? 1 : 3,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Tooltip(
message:
'${point.label} ¥${point.expense.toStringAsFixed(2)}',
child: Container(
height: height,
decoration: BoxDecoration(
color: point.expense == maxValue && maxValue > 0
? AppTheme.primary
: context.jz.primaryBackground,
borderRadius: const BorderRadius.vertical(
top: Radius.circular(4),
),
),
),
),
SizedBox(height: 6),
SizedBox(
height: 14,
child: showLabel
? FittedBox(
fit: BoxFit.scaleDown,
child: Text(
point.label,
style: TextStyle(
fontSize: 9,
color: context.jz.text3,
),
),
)
: null,
),
],
),
),
);
}).toList(),
);
}
}