Files
monisuo/flutter_monisuo/lib/ui/pages/asset/transfer_page.dart
sion123 dc6a8afc9a docs: 删除过时的功能规格文档
feat(ui): 重构资产页面UI,移除shadcn_ui依赖并简化设计

- 删除三个过时的功能规格文档(apply-new-styles.md、bottom-nav-labels.md、theme-dynamic-colors.md)
- 重构充值页面(deposit_page.dart):移除shadcn_ui依赖,简化表单验证和UI设计,使用动态主题颜色
- 重构划转页面(transfer_page.dart):移除复杂动画和shadcn_ui依赖,简化UI布局和交互逻辑
- 重构提现页面(withdraw_page.dart):移除shadcn_ui依赖,简化表单验证和网络选择器
- 重构我的页面相关组件:统一使用动态主题颜色,简化菜单项设计和KYC状态显示
- 所有页面现在使用Theme.of(context)获取动态颜色,支持明暗主题切换
- 移除硬编码的颜色引用,提高代码可维护性和主题一致性
2026-04-08 01:47:51 +08:00

565 lines
17 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:lucide_icons_flutter/lucide_icons.dart';
import '../../../core/theme/app_theme.dart';
import '../../../core/theme/app_spacing.dart';
import '../../../providers/asset_provider.dart';
import '../../../data/models/account_models.dart';
/// 划转页面
class TransferPage extends StatefulWidget {
const TransferPage({super.key});
@override
State<TransferPage> createState() => _TransferPageState();
}
class _TransferPageState extends State<TransferPage> {
final _amountController = TextEditingController();
final _focusNode = FocusNode();
int _direction = 1; // 1: 资金→交易, 2: 交易→资金
bool _isLoading = false;
@override
void initState() {
super.initState();
_amountController.addListener(() => setState(() {}));
WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<AssetProvider>().refreshAll(force: true);
});
}
@override
void dispose() {
_amountController.dispose();
_focusNode.dispose();
super.dispose();
}
// ============================================
// 数据访问
// ============================================
String get _fundBalance {
try {
final provider = context.read<AssetProvider>();
final balance = provider.fundAccount?.balance ??
provider.overview?.fundBalance ??
'0.00';
return _formatBalance(balance);
} catch (e) {
return '0.00';
}
}
String get _tradeUsdtBalance {
try {
final provider = context.read<AssetProvider>();
if (provider.tradeAccounts.isEmpty) return '0.00';
final usdtHolding = provider.tradeAccounts.firstWhere(
(t) => t.coinCode.toUpperCase() == 'USDT',
orElse: () => AccountTrade(
id: 0,
userId: 0,
coinCode: 'USDT',
quantity: '0',
avgPrice: '1',
totalCost: '0',
currentValue: '0',
profit: '0',
profitRate: 0,
),
);
return _formatBalance(usdtHolding.quantity);
} catch (e) {
return '0.00';
}
}
String get _availableBalance =>
_direction == 1 ? _fundBalance : _tradeUsdtBalance;
String get _fromLabel => _direction == 1 ? '资金账户' : '交易账户';
String get _toLabel => _direction == 1 ? '交易账户' : '资金账户';
String get _fromBalance => _direction == 1 ? _fundBalance : _tradeUsdtBalance;
String get _toBalance => _direction == 1 ? _tradeUsdtBalance : _fundBalance;
// ============================================
// 业务逻辑
// ============================================
Future<void> _doTransfer() async {
final amount = _amountController.text;
final available = double.tryParse(_availableBalance) ?? 0;
final transferAmount = double.tryParse(amount) ?? 0;
if (transferAmount <= 0) {
_showSnackBar('请输入有效的划转金额');
return;
}
if (transferAmount > available) {
_showSnackBar('余额不足');
return;
}
setState(() => _isLoading = true);
try {
final response = await context.read<AssetProvider>().transfer(
direction: _direction,
amount: amount,
);
if (mounted) {
if (response.success) {
_amountController.clear();
_showSnackBar('划转成功');
await Future.delayed(const Duration(milliseconds: 500));
if (mounted) Navigator.of(context).pop(true);
} else {
_showSnackBar(response.message ?? '划转失败');
}
}
} finally {
if (mounted) setState(() => _isLoading = false);
}
}
void _showSnackBar(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
);
}
void _setQuickAmount(double percent) {
final available = double.tryParse(_availableBalance) ?? 0;
final amount = available * percent;
_amountController.text = amount
.toStringAsFixed(8)
.replaceAll(RegExp(r'\.?0+$'), '');
HapticFeedback.selectionClick();
}
void _toggleDirection() {
HapticFeedback.mediumImpact();
setState(() => _direction = _direction == 1 ? 2 : 1);
}
// ============================================
// 构建 UI
// ============================================
@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
return Scaffold(
backgroundColor: colorScheme.surface,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
scrolledUnderElevation: 0,
leading: IconButton(
icon: Icon(LucideIcons.arrowLeft,
color: colorScheme.onSurface, size: 20),
onPressed: () => Navigator.of(context).pop(),
),
title: Text(
'账户划转',
style: AppTextStyles.headlineLarge(context).copyWith(
fontWeight: FontWeight.w600,
),
),
centerTitle: true,
),
body: Consumer<AssetProvider>(
builder: (context, provider, _) {
return SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(
AppSpacing.md, AppSpacing.xs, AppSpacing.md, AppSpacing.xl),
child: Column(
children: [
_buildTransferCard(colorScheme),
const SizedBox(height: AppSpacing.md),
_buildAmountSection(colorScheme),
const SizedBox(height: AppSpacing.md),
_buildTips(colorScheme),
const SizedBox(height: AppSpacing.xl),
_buildConfirmButton(colorScheme),
const SizedBox(height: AppSpacing.lg),
],
),
);
},
),
);
}
// ============================================
// 划转方向卡片
// ============================================
Widget _buildTransferCard(ColorScheme colorScheme) {
return Container(
width: double.infinity,
decoration: BoxDecoration(
color: colorScheme.surface,
borderRadius: BorderRadius.circular(20),
border: Border.all(
color: colorScheme.outlineVariant.withValues(alpha: 0.5),
width: 1,
),
),
child: Stack(
clipBehavior: Clip.none,
children: [
Column(
children: [
// From
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
'',
style: AppTextStyles.bodySmall(context).copyWith(
color: colorScheme.onSurfaceVariant,
),
),
const SizedBox(width: 8),
Text(
_fromLabel,
style:
AppTextStyles.bodyLarge(context).copyWith(
fontWeight: FontWeight.w500,
),
),
],
),
Text(
_fromBalance,
style: AppTextStyles.bodyLarge(context).copyWith(
fontWeight: FontWeight.w600,
),
),
],
),
),
Divider(
height: 1,
thickness: 1,
color: colorScheme.outlineVariant.withValues(alpha: 0.5),
),
// To
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
'',
style: AppTextStyles.bodySmall(context).copyWith(
color: colorScheme.onSurfaceVariant,
),
),
const SizedBox(width: 8),
Text(
_toLabel,
style:
AppTextStyles.bodyLarge(context).copyWith(
fontWeight: FontWeight.w500,
),
),
],
),
Text(
_toBalance,
style: AppTextStyles.bodyLarge(context).copyWith(
fontWeight: FontWeight.w600,
),
),
],
),
),
],
),
// 交换按钮 - 右侧贴分割线
Positioned(
right: 12,
top: 20,
child: GestureDetector(
onTap: _toggleDirection,
child: Container(
width: 28,
height: 28,
decoration: BoxDecoration(
color: colorScheme.surface,
shape: BoxShape.circle,
border: Border.all(
color: colorScheme.outlineVariant,
width: 1,
),
),
child: Icon(
LucideIcons.arrowUpDown,
size: 14,
color: colorScheme.onSurfaceVariant,
),
),
),
),
],
),
);
}
// ============================================
// 金额输入区域
// ============================================
Widget _buildAmountSection(ColorScheme colorScheme) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 标题行
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'划转金额',
style: AppTextStyles.bodyMedium(context).copyWith(
color: colorScheme.onSurfaceVariant,
fontWeight: FontWeight.w500,
),
),
GestureDetector(
onTap: () => _setQuickAmount(1.0),
child: Text(
'全部',
style: AppTextStyles.bodyMedium(context).copyWith(
color: colorScheme.secondary,
fontWeight: FontWeight.w500,
),
),
),
],
),
const SizedBox(height: 12),
// 金额输入框
Container(
width: double.infinity,
height: 52,
decoration: BoxDecoration(
color: colorScheme.surface,
borderRadius: BorderRadius.circular(14),
border: Border.all(
color: _focusNode.hasFocus
? colorScheme.secondary
: colorScheme.outlineVariant.withValues(alpha: 0.5),
width: 1,
),
),
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
children: [
Expanded(
child: TextField(
controller: _amountController,
focusNode: _focusNode,
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'^\d*\.?\d{0,8}')),
],
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w600,
color: colorScheme.onSurface,
),
decoration: InputDecoration(
hintText: '0.00',
hintStyle: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w600,
color: colorScheme.onSurfaceVariant.withValues(alpha: 0.3),
),
border: InputBorder.none,
contentPadding: EdgeInsets.zero,
isDense: true,
),
onChanged: (_) => setState(() {}),
),
),
Text(
'USDT',
style: AppTextStyles.bodyMedium(context).copyWith(
color: colorScheme.onSurfaceVariant,
fontWeight: FontWeight.w500,
),
),
],
),
),
const SizedBox(height: 10),
// 可用余额
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'可用余额',
style: AppTextStyles.bodySmall(context).copyWith(
color: colorScheme.onSurfaceVariant,
),
),
Text(
'$_availableBalance USDT',
style: AppTextStyles.bodySmall(context).copyWith(
color: colorScheme.onSurfaceVariant,
fontWeight: FontWeight.w500,
),
),
],
),
const SizedBox(height: 12),
// 百分比按钮
Row(
children: [
_buildPercentButton('25%', 0.25, colorScheme),
const SizedBox(width: 8),
_buildPercentButton('50%', 0.50, colorScheme),
const SizedBox(width: 8),
_buildPercentButton('75%', 0.75, colorScheme),
const SizedBox(width: 8),
_buildPercentButton('100%', 1.0, colorScheme),
],
),
],
);
}
Widget _buildPercentButton(
String label, double percent, ColorScheme colorScheme) {
return Expanded(
child: GestureDetector(
onTap: () => _setQuickAmount(percent),
child: Container(
height: 34,
decoration: BoxDecoration(
color: colorScheme.surfaceContainerHighest.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(8),
),
child: Center(
child: Text(
label,
style: AppTextStyles.bodySmall(context).copyWith(
color: colorScheme.onSurfaceVariant,
fontWeight: FontWeight.w500,
),
),
),
),
),
);
}
// ============================================
// 提示文字
// ============================================
Widget _buildTips(ColorScheme colorScheme) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
Icon(LucideIcons.info,
size: 13, color: colorScheme.onSurfaceVariant),
const SizedBox(width: 6),
Text(
'划转即时到账,无需手续费',
style: AppTextStyles.bodySmall(context).copyWith(
color: colorScheme.onSurfaceVariant,
),
),
],
),
);
}
// ============================================
// 确认按钮
// ============================================
Widget _buildConfirmButton(ColorScheme colorScheme) {
final hasAmount = _amountController.text.isNotEmpty &&
double.tryParse(_amountController.text) != null &&
double.parse(_amountController.text) > 0;
return SizedBox(
width: double.infinity,
height: 48,
child: ElevatedButton(
onPressed: _isLoading ? null : _doTransfer,
style: ElevatedButton.styleFrom(
backgroundColor: hasAmount
? colorScheme.onSurface
: colorScheme.surfaceContainerHighest,
foregroundColor: hasAmount
? colorScheme.surface
: colorScheme.onSurfaceVariant,
disabledBackgroundColor: colorScheme.surfaceContainerHighest,
disabledForegroundColor: colorScheme.onSurfaceVariant,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14),
),
),
child: _isLoading
? SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
colorScheme.onSurfaceVariant,
),
),
)
: Text(
'确认划转',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
),
),
),
);
}
// ============================================
// Helpers
// ============================================
String _formatBalance(String balance) {
final val = double.tryParse(balance);
if (val == null) return '0.00';
return val.toStringAsFixed(2);
}
}