Add transfer tracking and secure admin access

This commit is contained in:
2026-07-26 11:57:57 +08:00
parent 0738953e6d
commit 7df25edd96
111 changed files with 6379 additions and 1934 deletions
+91 -11
View File
@@ -18,7 +18,9 @@ class AddPage extends StatefulWidget {
class _AddPageState extends State<AddPage> {
final _noteCtrl = TextEditingController();
final _counterpartyCtrl = TextEditingController();
String _tab = 'expense';
String _transferDirection = 'out';
final Map<String, List<CategoryItem>> _categoriesByType = {
'expense': <CategoryItem>[],
'income': <CategoryItem>[],
@@ -26,18 +28,34 @@ class _AddPageState extends State<AddPage> {
final Map<String, CategoryItem?> _selectedByType = {
'expense': null,
'income': null,
'transfer_out': null,
'transfer_in': null,
};
String _amount = '0';
String? _paymentMethod;
DateTime _occurredAt = ShanghaiTime.now;
bool _saving = false;
bool _loadingCategories = true;
String get _categoryType => _tab == 'transfer'
? _transferDirection == 'in'
? 'income'
: 'expense'
: _tab;
String get _selectionKey =>
_tab == 'transfer' ? 'transfer_$_transferDirection' : _tab;
List<CategoryItem> get _categories =>
_categoriesByType[_tab] ?? const <CategoryItem>[];
_categoriesByType[_categoryType] ?? const <CategoryItem>[];
CategoryItem? get _selected => _selectedByType[_tab];
CategoryItem? get _selected => _selectedByType[_selectionKey];
Color get _activeColor => _tab == 'income' ? AppTheme.primary : AppTheme.red;
Color get _activeColor =>
_tab == 'income' || _tab == 'transfer' && _transferDirection == 'in'
? AppTheme.primary
: _tab == 'transfer'
? AppTheme.orange
: AppTheme.red;
@override
void initState() {
@@ -48,6 +66,7 @@ class _AddPageState extends State<AddPage> {
@override
void dispose() {
_noteCtrl.dispose();
_counterpartyCtrl.dispose();
super.dispose();
}
@@ -68,6 +87,12 @@ class _AddPageState extends State<AddPage> {
if (_selectedByType['income'] == null && results[1].isNotEmpty) {
_selectedByType['income'] = results[1].first;
}
_selectedByType['transfer_out'] ??= results[0].isEmpty
? null
: results[0].first;
_selectedByType['transfer_in'] ??= results[1].isEmpty
? null
: results[1].first;
});
} catch (error) {
if (mounted) {
@@ -85,6 +110,11 @@ class _AddPageState extends State<AddPage> {
setState(() => _tab = tab);
}
void _switchTransferDirection(String direction) {
if (_transferDirection == direction) return;
setState(() => _transferDirection = direction);
}
void _pressKey(String key) {
setState(() {
if (key == 'delete') {
@@ -123,6 +153,11 @@ class _AddPageState extends State<AddPage> {
await TxApi.create(
categoryId: _selected!.id,
type: _tab,
transferDirection: _tab == 'transfer' ? _transferDirection : null,
counterparty:
_tab == 'transfer' && _counterpartyCtrl.text.trim().isNotEmpty
? _counterpartyCtrl.text.trim()
: null,
amount: amount,
note: _noteCtrl.text.trim().isEmpty ? null : _noteCtrl.text.trim(),
paymentMethod: _paymentMethod,
@@ -152,6 +187,17 @@ class _AddPageState extends State<AddPage> {
if (value != null) setState(() => _noteCtrl.text = value);
}
Future<void> _editCounterparty() async {
final value = await showJzTextInputSheet(
context,
title: '转账对方',
label: '姓名或备注名',
initialValue: _counterpartyCtrl.text,
maxLength: 40,
);
if (value != null) setState(() => _counterpartyCtrl.text = value);
}
Future<void> _pickOccurredAt() async {
final value = await showJzDateTimeSheet(
context,
@@ -201,6 +247,20 @@ class _AddPageState extends State<AddPage> {
return Column(
children: [
_buildTypeSelector(),
if (_tab == 'transfer') ...[
const SizedBox(height: 6),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 72),
child: JzSegmentedControl<String>(
value: _transferDirection,
options: const [
JzOption(value: 'out', label: '转出'),
JzOption(value: 'in', label: '转入'),
],
onChanged: _switchTransferDirection,
),
),
],
SizedBox(height: 4),
Expanded(
child: AnimatedSwitcher(
@@ -210,7 +270,9 @@ class _AddPageState extends State<AddPage> {
transitionBuilder: (child, animation) {
final offset = _tab == 'expense'
? const Offset(-0.04, 0)
: const Offset(0.04, 0);
: _tab == 'income'
? const Offset(0.04, 0)
: Offset.zero;
return FadeTransition(
opacity: animation,
child: SlideTransition(
@@ -244,7 +306,7 @@ class _AddPageState extends State<AddPage> {
Widget _buildTypeSelector() {
return Container(
width: 220,
width: 300,
height: 38,
margin: const EdgeInsets.only(top: 4),
padding: const EdgeInsets.all(3),
@@ -258,11 +320,16 @@ class _AddPageState extends State<AddPage> {
AnimatedAlign(
duration: const Duration(milliseconds: 260),
curve: Curves.easeOutCubic,
alignment: _tab == 'expense'
? Alignment.centerLeft
: Alignment.centerRight,
alignment: Alignment(
_tab == 'expense'
? -1
: _tab == 'income'
? 1
: 0,
0,
),
child: Container(
width: constraints.maxWidth / 2,
width: constraints.maxWidth / 3,
height: constraints.maxHeight,
decoration: BoxDecoration(
color: _activeColor,
@@ -271,7 +338,11 @@ class _AddPageState extends State<AddPage> {
),
),
Row(
children: [_segment('支出', 'expense'), _segment('收入', 'income')],
children: [
_segment('支出', 'expense'),
_segment('转账', 'transfer'),
_segment('收入', 'income'),
],
),
],
),
@@ -315,7 +386,8 @@ class _AddPageState extends State<AddPage> {
final selected = category.id == _selected?.id;
return InkWell(
borderRadius: BorderRadius.circular(16),
onTap: () => setState(() => _selectedByType[_tab] = category),
onTap: () =>
setState(() => _selectedByType[_selectionKey] = category),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
@@ -423,6 +495,14 @@ class _AddPageState extends State<AddPage> {
label: _noteCtrl.text.isEmpty ? '备注' : _noteCtrl.text,
onTap: _editNote,
),
if (_tab == 'transfer')
_metaChip(
icon: Icons.person_outline_rounded,
label: _counterpartyCtrl.text.isEmpty
? '转账对方'
: _counterpartyCtrl.text,
onTap: _editCounterparty,
),
_metaChip(
icon: Icons.schedule_rounded,
label: dateLabel,
+4 -4
View File
@@ -174,7 +174,7 @@ class _ParseSheetState extends State<_ParseSheet> {
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w700,
color: d.type == 'income'
color: d.isIncome
? AppTheme.primary
: AppTheme.red,
),
@@ -189,18 +189,18 @@ class _ParseSheetState extends State<_ParseSheet> {
),
decoration: BoxDecoration(
color:
(d.type == 'income'
(d.isIncome
? AppTheme.primary
: AppTheme.red)
.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(5),
),
child: Text(
d.type == 'income' ? '收入' : '支出',
d.typeLabel,
style: TextStyle(
fontSize: 9.5,
fontWeight: FontWeight.w700,
color: d.type == 'income'
color: d.isIncome
? AppTheme.primary
: AppTheme.red,
),
@@ -135,12 +135,18 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
final type = switch (json['type']?.toString().toLowerCase()) {
'income' => 'income',
'expense' => 'expense',
'transfer' => 'transfer',
_ => 'unknown',
};
final rawTransferDirection = json['transferDirection']?.toString();
final transferDirection =
rawTransferDirection == 'in' || rawTransferDirection == 'out'
? rawTransferDirection!
: 'out';
final categoryId = (json['categoryId'] as num?)?.toInt();
final categoryName = json['categoryName']?.toString() ?? '其他';
final categoryIcon = json['categoryIcon']?.toString() ?? 'tag';
final categories = _categoriesFor(type);
final categories = _categoriesFor(type, transferDirection);
final exists = categories.any((category) => category.id == categoryId);
if (type != 'unknown' && !exists && categoryId != null) {
categories.add(
@@ -148,7 +154,11 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
'id': categoryId,
'name': categoryName,
'iconKey': categoryIcon,
'type': type,
'type': type == 'transfer'
? transferDirection == 'in'
? 'income'
: 'expense'
: type,
'sortOrder': 999,
'isCustom': false,
}),
@@ -172,6 +182,8 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
occurredAt: parsedOccurredAt == null
? ShanghaiTime.now
: ShanghaiTime.toCivil(parsedOccurredAt),
transferDirection: transferDirection,
counterparty: json['counterparty']?.toString() ?? '',
);
}
@@ -189,10 +201,11 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
);
}
List<CategoryItem> _categoriesFor(String type) {
List<CategoryItem> _categoriesFor(String type, [String direction = 'out']) {
return switch (type) {
'income' => _incomeCategories,
'expense' => _expenseCategories,
'transfer' => direction == 'in' ? _incomeCategories : _expenseCategories,
_ => <CategoryItem>[],
};
}
@@ -203,7 +216,7 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
void _changeType(_ScreenshotDraft draft, String type) {
if (draft.type == type) return;
final categories = _categoriesFor(type);
final categories = _categoriesFor(type, draft.transferDirection);
setState(() {
draft.type = type;
draft.included = true;
@@ -211,6 +224,15 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
});
}
void _changeTransferDirection(_ScreenshotDraft draft, String direction) {
if (draft.transferDirection == direction) return;
final categories = _categoriesFor('transfer', direction);
setState(() {
draft.transferDirection = direction;
draft.categoryId = categories.isEmpty ? null : categories.first.id;
});
}
Future<void> _pickOccurredAt(_ScreenshotDraft draft) async {
FocusScope.of(context).unfocus();
final value = await showJzDateTimeSheet(
@@ -226,7 +248,7 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
}
Future<void> _selectCategory(_ScreenshotDraft draft, int billIndex) async {
final categories = _categoriesFor(draft.type);
final categories = _categoriesFor(draft.type, draft.transferDirection);
if (categories.isEmpty) {
_showMessage('当前收支类型暂无可选分类');
return;
@@ -308,8 +330,10 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
for (var index = 0; index < selected.length; index++) {
final draft = selected[index];
if (draft.type != 'income' && draft.type != 'expense') {
_showMessage('${_drafts.indexOf(draft) + 1} 笔请先确认收入或支出');
if (draft.type != 'income' &&
draft.type != 'expense' &&
draft.type != 'transfer') {
_showMessage('${_drafts.indexOf(draft) + 1} 笔请先确认账单类型');
return;
}
if ((double.tryParse(draft.amountController.text) ?? 0) <= 0) {
@@ -343,6 +367,14 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
source: 'screenshot',
sourceText: '截屏识别',
occurredAt: draft.occurredAt,
transferDirection: draft.type == 'transfer'
? draft.transferDirection
: null,
counterparty:
draft.type == 'transfer' &&
draft.counterpartyController.text.trim().isNotEmpty
? draft.counterpartyController.text.trim()
: null,
);
draft.saved = true;
savedAny = true;
@@ -540,7 +572,7 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
}
Widget _buildDraftCard(_ScreenshotDraft draft, int index) {
final categories = _categoriesFor(draft.type);
final categories = _categoriesFor(draft.type, draft.transferDirection);
final selectedCategory = categories
.where((category) => category.id == draft.categoryId)
.firstOrNull;
@@ -583,6 +615,8 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
label: switch (draft.type) {
'income' => '收入',
'expense' => '支出',
'transfer' =>
draft.transferDirection == 'in' ? '转入' : '转出',
_ => '待确认',
},
color: draft.type == 'unknown'
@@ -629,6 +663,15 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
),
),
SizedBox(width: 8),
Expanded(
child: _TypeButton(
label: '转账',
selected: draft.type == 'transfer',
color: AppTheme.orange,
onTap: () => _changeType(draft, 'transfer'),
),
),
SizedBox(width: 8),
Expanded(
child: _TypeButton(
label: '收入',
@@ -639,6 +682,37 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
),
],
),
if (draft.type == 'transfer') ...[
SizedBox(height: 10),
Row(
children: [
Expanded(
child: _TypeButton(
label: '转出',
selected: draft.transferDirection == 'out',
color: AppTheme.orange,
onTap: () =>
_changeTransferDirection(draft, 'out'),
),
),
SizedBox(width: 8),
Expanded(
child: _TypeButton(
label: '转入',
selected: draft.transferDirection == 'in',
color: AppTheme.primary,
onTap: () =>
_changeTransferDirection(draft, 'in'),
),
),
],
),
SizedBox(height: 10),
TextField(
controller: draft.counterpartyController,
decoration: InputDecoration(labelText: '转账对方'),
),
],
SizedBox(height: 10),
_CategoryField(
category: selectedCategory,
@@ -715,6 +789,7 @@ class _ScreenshotParseSheetState extends State<ScreenshotParseSheet> {
class _ScreenshotDraft {
String type;
String transferDirection;
int? categoryId;
bool included;
bool saved = false;
@@ -722,6 +797,7 @@ class _ScreenshotDraft {
final TextEditingController amountController;
final TextEditingController noteController;
final TextEditingController paymentController;
final TextEditingController counterpartyController;
_ScreenshotDraft({
required this.type,
@@ -731,15 +807,19 @@ class _ScreenshotDraft {
required String note,
required String paymentMethod,
required this.occurredAt,
this.transferDirection = 'out',
String counterparty = '',
}) : included = included,
amountController = TextEditingController(text: amount),
noteController = TextEditingController(text: note),
paymentController = TextEditingController(text: paymentMethod);
paymentController = TextEditingController(text: paymentMethod),
counterpartyController = TextEditingController(text: counterparty);
void dispose() {
amountController.dispose();
noteController.dispose();
paymentController.dispose();
counterpartyController.dispose();
}
}