账户同步服务 - Flutter与OpenHarmony协作
·
更新概述
v1.28.0 版本为 OpenHarmony 钱包应用增加了多账户支持功能。用户现在可以创建和管理多个账户(如现金、银行卡、支付宝等),每个账户独立记录交易和余额。通过余额卡片上的账户选择器,用户可以轻松切换不同账户查看相应的财务数据。

核心功能更新
1. 账户模型
Account 类
/// 账户模型
class Account {
final String id;
final String name;
final String type;
final Color color;
final IconData icon;
final double initialBalance;
Account({
required this.id,
required this.name,
required this.type,
required this.color,
required this.icon,
this.initialBalance = 0.0,
});
}
说明:
id: 账户唯一标识name: 账户名称(如"工资卡"、“现金”)type: 账户类型(如"银行卡"、“现金”)color: 账户主题色icon: 账户图标initialBalance: 初始余额
2. 交易模型更新
Transaction 类新增字段
class Transaction {
// ... 其他字段
final String accountId; // 新增:所属账户ID
Transaction({
// ... 其他参数
required this.accountId, // 必需参数
});
}
说明:
- 每笔交易都关联到特定账户
- 支持跨账户交易记录
- 保持数据完整性
3. 账户选择器
_showAccountSelector 方法
/// 显示账户选择器
void _showAccountSelector() {
showModalBottomSheet(
context: context,
builder: (context) => Container(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'选择账户',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
..._accounts.map((account) => ListTile(
leading: Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: account.color.withOpacity(0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(account.icon, color: account.color),
),
title: Text(account.name),
subtitle: Text(account.type),
trailing: _selectedAccountId == account.id
? Icon(Icons.check, color: account.color)
: null,
onTap: () {
setState(() {
_selectedAccountId = account.id;
_applyFilters();
});
Navigator.pop(context);
},
)),
],
),
),
);
}
说明:
- 底部弹窗显示所有账户
- 每个账户显示图标、名称、类型
- 当前选中账户显示勾选标记
- 点击切换账户并刷新数据
UI 变化
余额卡片更新
┌─────────────────────────────────┐
│ 早上好 💳 默认账户 ▼ 👁️ │
│ │
│ ¥5,000.00 │
│ │
│ 收入: ¥5,000 支出: ¥345 │
└─────────────────────────────────┘
账户选择器
┌─────────────────────────────────┐
│ 选择账户 │
│ │
│ 💳 默认账户 ✓ │
│ 现金 │
│ │
│ 🏦 工资卡 │
│ 银行卡 │
│ │
│ 📱 支付宝 │
│ 电子钱包 │
└─────────────────────────────────┘
使用场景
场景 1:多账户管理
- 用户有现金、银行卡、支付宝等多个账户
- 点击余额卡片上的账户名称
- 选择要查看的账户
- 查看该账户的余额和交易记录
场景 2:分账记录
- 用户在不同账户间进行交易
- 添加交易时自动关联到当前选中账户
- 每个账户独立计算余额
- 统计数据按账户分别显示
场景 3:账户切换
- 用户想查看工资卡的收支情况
- 点击账户选择器
- 选择"工资卡"账户
- 界面自动更新显示工资卡的数据
功能特点
1. 独立核算
- 每个账户独立计算余额
- 交易记录按账户分类
- 统计数据分账户显示
2. 灵活切换
- 一键切换账户视图
- 实时更新数据显示
- 保持用户操作连续性
3. 视觉区分
- 每个账户有独特的颜色和图标
- 清晰的视觉标识
- 易于识别和区分
4. 数据完整性
- 所有交易都关联到账户
- 支持历史数据迁移
- 保证数据一致性
版本对比
| 功能 | v1.27.0 | v1.28.0 |
|---|---|---|
| 统计汇总卡片 | ✅ | ✅ |
| 多账户支持 | ❌ | ✅ |
| 账户切换 | ❌ | ✅ |
| 分账记录 | ❌ | ✅ |
技术实现
1. 账户管理
late List<Account> _accounts;
late String _selectedAccountId;
/// 获取当前选中账户
Account get _currentAccount {
return _accounts.firstWhere((account) => account.id == _selectedAccountId);
}
2. 余额计算
/// 计算当前账户余额
double get _currentBalance {
double balance = _currentAccount.initialBalance;
final accountTransactions = _transactions.where((t) => t.accountId == _selectedAccountId);
for (var transaction in accountTransactions) {
if (transaction.type == TransactionType.income) {
balance += transaction.amount;
} else {
balance -= transaction.amount;
}
}
return balance;
}
3. 数据筛选
/// 应用搜索和筛选
void _applyFilters() {
_filteredTransactions = _transactions.where((t) {
final matchesAccount = t.accountId == _selectedAccountId;
final matchesSearch = t.title.toLowerCase().contains(_searchQuery.toLowerCase());
final matchesCategory = _selectedCategory == null || t.category == _selectedCategory;
final matchesDateRange = _matchesDateRange(t.date);
final matchesAmountRange = _matchesAmountRange(t.amount);
return matchesAccount && matchesSearch && matchesCategory && matchesDateRange && matchesAmountRange;
}).toList();
}
设计亮点
1. 用户体验
- 账户切换操作简单直观
- 数据更新实时响应
- 视觉反馈清晰明确
2. 数据架构
- 账户和交易分离设计
- 支持扩展更多账户类型
- 保持向后兼容性
3. 界面设计
- 账户信息显示在显眼位置
- 选择器采用底部弹窗设计
- 保持整体设计风格一致
账户类型示例
| 账户类型 | 图标 | 颜色 | 用途 |
|---|---|---|---|
| 现金 | 💰 | 绿色 | 日常现金支出 |
| 银行卡 | 🏦 | 蓝色 | 工资收入、大额支出 |
| 信用卡 | 💳 | 红色 | 透支消费 |
| 支付宝 | 📱 | 蓝色 | 移动支付 |
| 微信 | 💬 | 绿色 | 社交支付 |
数据迁移
现有数据处理
- 自动为现有交易分配默认账户
- 保持原有功能完全兼容
- 用户可手动调整交易所属账户
默认账户
if (_accounts.isEmpty) {
// 创建默认账户
_accounts.add(Account(
id: 'default',
name: '默认账户',
type: '现金',
color: Colors.blue,
icon: Icons.account_balance_wallet,
initialBalance: 0.0,
));
}
欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐

所有评论(0)