first commit
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../models/category.dart';
|
||||
|
||||
/// 分类 emoji 宫格(手动记账页与账单编辑页共用)
|
||||
///
|
||||
/// 这是纯同步组件:分类列表的加载/错误/空态由调用方用 AsyncValue.when 处理,
|
||||
/// 组件本身不关心 type 之类的业务参数。
|
||||
class CategoryPickerGrid extends StatelessWidget {
|
||||
const CategoryPickerGrid({
|
||||
super.key,
|
||||
required this.categories,
|
||||
required this.selectedId,
|
||||
required this.onSelect,
|
||||
this.crossAxisCount = 5,
|
||||
this.emptyText = '暂无分类',
|
||||
});
|
||||
|
||||
final List<Category> categories;
|
||||
final int? selectedId;
|
||||
final ValueChanged<int> onSelect;
|
||||
final int crossAxisCount;
|
||||
final String emptyText;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (categories.isEmpty) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withValues(alpha: 0.8),
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
),
|
||||
child: Text(emptyText, style: const TextStyle(color: Color(0xFF8D6E63))),
|
||||
);
|
||||
}
|
||||
|
||||
return GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: crossAxisCount,
|
||||
mainAxisSpacing: 10,
|
||||
crossAxisSpacing: 6,
|
||||
childAspectRatio: 0.85,
|
||||
),
|
||||
itemCount: categories.length,
|
||||
itemBuilder: (context, i) => _item(categories[i]),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _item(Category category) {
|
||||
final active = selectedId == category.id;
|
||||
return GestureDetector(
|
||||
onTap: () => onSelect(category.id),
|
||||
child: Column(
|
||||
children: [
|
||||
AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 150),
|
||||
width: 52,
|
||||
height: 52,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: active ? const Color(0xFFC8956E) : Colors.white,
|
||||
border: Border.all(
|
||||
color: active ? const Color(0xFF5D4037) : const Color(0xFFE0C9A6),
|
||||
),
|
||||
),
|
||||
alignment: Alignment.center,
|
||||
child: Text(category.icon, style: const TextStyle(fontSize: 24)),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
category.name,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: active ? const Color(0xFF5D4037) : const Color(0xFF8D6E63),
|
||||
fontWeight: active ? FontWeight.bold : FontWeight.normal,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 一个扇区
|
||||
class DonutSlice {
|
||||
final double value;
|
||||
final Color color;
|
||||
final String label;
|
||||
|
||||
const DonutSlice({required this.value, required this.color, this.label = ''});
|
||||
}
|
||||
|
||||
/// 图表配色:以主题橙棕/金色为主,穿插少量对比色便于区分分类
|
||||
const List<Color> kChartPalette = [
|
||||
Color(0xFFC8956E), // 橙棕(主色)
|
||||
Color(0xFFFFB74D), // 亮橙
|
||||
Color(0xFFE57373), // 红
|
||||
Color(0xFF81C784), // 绿
|
||||
Color(0xFF64B5F6), // 蓝
|
||||
Color(0xFFBA68C8), // 紫
|
||||
Color(0xFF4DB6AC), // 青
|
||||
Color(0xFFF06292), // 粉
|
||||
Color(0xFFA1887F), // 棕灰
|
||||
Color(0xFF90A4AE), // 蓝灰
|
||||
];
|
||||
|
||||
Color chartColorAt(int index) => kChartPalette[index % kChartPalette.length];
|
||||
|
||||
/// 通用环形图(首页预算环 + 统计页分类饼图共用)
|
||||
///
|
||||
/// 内部处理了 fl_chart 的空数据问题:`PieChartSectionData.value` 必须 > 0,
|
||||
/// 全 0 / NaN 会算出 NaN 角度导致渲染异常甚至崩溃,所以这里统一过滤并兜底成灰环。
|
||||
/// fl_chart 类型只出现在本文件,升级图表库时只需改这一处。
|
||||
class DonutChart extends StatelessWidget {
|
||||
const DonutChart({
|
||||
super.key,
|
||||
required this.slices,
|
||||
this.size = 150,
|
||||
this.centerSpaceRadius = 46,
|
||||
this.sectionsSpace = 2,
|
||||
this.center,
|
||||
});
|
||||
|
||||
final List<DonutSlice> slices;
|
||||
final double size;
|
||||
final double centerSpaceRadius;
|
||||
final double sectionsSpace;
|
||||
|
||||
/// 圆心内容,例如 Text('68%')
|
||||
final Widget? center;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final valid = slices.where((s) => s.value.isFinite && s.value > 0).toList();
|
||||
// 无有效数据时退化为单段灰环,避免空数据渲染问题
|
||||
final effective = valid.isEmpty
|
||||
? const [DonutSlice(value: 1, color: Color(0xFFE0C9A6))]
|
||||
: valid;
|
||||
|
||||
final ringRadius = (size / 2 - centerSpaceRadius).clamp(8.0, double.infinity);
|
||||
|
||||
return SizedBox(
|
||||
width: size,
|
||||
height: size,
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
PieChart(
|
||||
PieChartData(
|
||||
sections: effective
|
||||
.map((s) => PieChartSectionData(
|
||||
value: s.value,
|
||||
color: s.color,
|
||||
radius: ringRadius,
|
||||
showTitle: false,
|
||||
))
|
||||
.toList(),
|
||||
centerSpaceRadius: centerSpaceRadius,
|
||||
sectionsSpace: sectionsSpace,
|
||||
),
|
||||
),
|
||||
?center,
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 加载态
|
||||
class LoadingView extends StatelessWidget {
|
||||
const LoadingView({super.key, this.tip = '加载中...', this.padding = const EdgeInsets.all(40)});
|
||||
|
||||
final String tip;
|
||||
final EdgeInsetsGeometry padding;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: padding,
|
||||
child: Column(
|
||||
children: [
|
||||
const SizedBox(
|
||||
width: 26,
|
||||
height: 26,
|
||||
child: CircularProgressIndicator(strokeWidth: 2.5, color: Color(0xFFC8956E)),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(tip, style: const TextStyle(fontSize: 13, color: Color(0xFF8D6E63))),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 错误态(可带重试)
|
||||
class ErrorView extends StatelessWidget {
|
||||
const ErrorView({super.key, required this.message, this.onRetry});
|
||||
|
||||
final String message;
|
||||
final VoidCallback? onRetry;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withValues(alpha: 0.85),
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
const Text('😵', style: TextStyle(fontSize: 30)),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
message,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(fontSize: 13, color: Color(0xFF8D6E63)),
|
||||
),
|
||||
if (onRetry != null) ...[
|
||||
const SizedBox(height: 12),
|
||||
OutlinedButton(
|
||||
onPressed: onRetry,
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: const Color(0xFFC8956E),
|
||||
side: const BorderSide(color: Color(0xFFC8956E)),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
|
||||
),
|
||||
child: const Text('重试'),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 空状态
|
||||
class EmptyView extends StatelessWidget {
|
||||
const EmptyView({
|
||||
super.key,
|
||||
this.icon = '📭',
|
||||
required this.title,
|
||||
this.tip,
|
||||
this.padding = const EdgeInsets.symmetric(vertical: 40),
|
||||
});
|
||||
|
||||
final String icon;
|
||||
final String title;
|
||||
final String? tip;
|
||||
final EdgeInsetsGeometry padding;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: padding,
|
||||
child: Column(
|
||||
children: [
|
||||
Text(icon, style: const TextStyle(fontSize: 44)),
|
||||
const SizedBox(height: 10),
|
||||
Text(title, style: const TextStyle(fontSize: 15, color: Color(0xFF5D4037))),
|
||||
if (tip != null) ...[
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
tip!,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(fontSize: 12.5, color: Color(0xFFBCAAA4)),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user