524 lines
16 KiB
Dart
524 lines
16 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_theme_extension.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();
|
|
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';
|
|
}
|
|
}
|
|
|
|
/// 獲取交易賬戶 USDT 餘額(安全檢查)
|
|
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('劃轉成功');
|
|
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)),
|
|
);
|
|
}
|
|
|
|
/// 設置快捷百分比金額
|
|
void _setQuickAmount(double percent) {
|
|
final available = double.tryParse(_availableBalance) ?? 0;
|
|
final amount = available * percent;
|
|
_amountController.text = amount.toStringAsFixed(8).replaceAll(RegExp(r'\.?0+$'), '');
|
|
}
|
|
|
|
/// 切換方向
|
|
void _toggleDirection() {
|
|
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: colorScheme.surfaceContainer,
|
|
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),
|
|
),
|
|
centerTitle: true,
|
|
),
|
|
body: Consumer<AssetProvider>(
|
|
builder: (context, provider, _) {
|
|
return SingleChildScrollView(
|
|
padding: const EdgeInsets.fromLTRB(AppSpacing.md, AppSpacing.md, AppSpacing.md, AppSpacing.xl),
|
|
child: Column(
|
|
children: [
|
|
_buildTransferDirectionCard(),
|
|
const SizedBox(height: AppSpacing.lg),
|
|
_buildAmountSection(),
|
|
const SizedBox(height: AppSpacing.lg),
|
|
_buildTipsCard(),
|
|
const SizedBox(height: AppSpacing.lg),
|
|
_buildConfirmButton(),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
// ============================================
|
|
// Transfer direction card
|
|
// ============================================
|
|
|
|
Widget _buildTransferDirectionCard() {
|
|
final colorScheme = Theme.of(context).colorScheme;
|
|
// 金色主題感知: Dark #D4AF37 (primary), Light #F59E0B (secondary)
|
|
final goldAccent = colorScheme.brightness == Brightness.dark
|
|
? colorScheme.primary
|
|
: colorScheme.secondary;
|
|
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.all(AppSpacing.lg),
|
|
decoration: BoxDecoration(
|
|
color: colorScheme.surfaceContainer,
|
|
borderRadius: BorderRadius.circular(AppRadius.xl),
|
|
border: Border.all(color: colorScheme.outlineVariant.withValues(alpha: 0.6)),
|
|
),
|
|
child: Column(
|
|
children: [
|
|
// Source account
|
|
_animatedSwitcher(
|
|
key: 'src-$_direction',
|
|
beginOffset: const Offset(0, -1),
|
|
child: _buildAccountRow(
|
|
label: '從',
|
|
accountName: _fromLabel,
|
|
balance: _fromBalance,
|
|
),
|
|
),
|
|
|
|
// Swap button with gold accent
|
|
GestureDetector(
|
|
onTap: _toggleDirection,
|
|
child: Container(
|
|
width: 40,
|
|
height: 40,
|
|
margin: const EdgeInsets.symmetric(vertical: AppSpacing.md),
|
|
decoration: BoxDecoration(
|
|
// 橢圓形半透明金色背景
|
|
color: goldAccent.withValues(alpha: 0.15),
|
|
shape: BoxShape.circle,
|
|
border: Border.all(
|
|
color: goldAccent.withValues(alpha: 0.3),
|
|
width: 1.5,
|
|
),
|
|
),
|
|
child: Center(
|
|
child: Icon(
|
|
LucideIcons.arrowUpDown,
|
|
size: 20,
|
|
color: goldAccent,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
// Destination account
|
|
_animatedSwitcher(
|
|
key: 'dst-$_direction',
|
|
beginOffset: const Offset(0, 1),
|
|
child: _buildAccountRow(
|
|
label: '到',
|
|
accountName: _toLabel,
|
|
balance: _toBalance,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 統一的 AnimatedSwitcher 構造
|
|
Widget _animatedSwitcher({
|
|
required String key,
|
|
required Offset beginOffset,
|
|
required Widget child,
|
|
}) {
|
|
return AnimatedSwitcher(
|
|
duration: const Duration(milliseconds: 300),
|
|
switchInCurve: Curves.easeInOut,
|
|
switchOutCurve: Curves.easeInOut,
|
|
transitionBuilder: (widget, animation) {
|
|
return SlideTransition(
|
|
position: Tween<Offset>(begin: beginOffset, end: Offset.zero).animate(animation),
|
|
child: FadeTransition(opacity: animation, child: widget),
|
|
);
|
|
},
|
|
child: KeyedSubtree(key: ValueKey(key), child: child),
|
|
);
|
|
}
|
|
|
|
/// Single account row inside the direction card
|
|
Widget _buildAccountRow({
|
|
required String label,
|
|
required String accountName,
|
|
required String balance,
|
|
}) {
|
|
final colorScheme = Theme.of(context).colorScheme;
|
|
|
|
return SizedBox(
|
|
width: double.infinity,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(label, style: AppTextStyles.bodySmall(context)),
|
|
const SizedBox(height: AppSpacing.sm),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Icon(
|
|
label == '從' ? LucideIcons.wallet : LucideIcons.trendingUp,
|
|
size: 18,
|
|
color: colorScheme.onSurfaceVariant,
|
|
),
|
|
const SizedBox(width: AppSpacing.sm + 2),
|
|
Text(accountName, style: AppTextStyles.headlineMedium(context)),
|
|
],
|
|
),
|
|
Text(
|
|
'\u00A5 ${_formatBalance(balance)}',
|
|
style: AppTextStyles.headlineMedium(context),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// ============================================
|
|
// Amount input section
|
|
// ============================================
|
|
|
|
Widget _buildAmountSection() {
|
|
final colorScheme = Theme.of(context).colorScheme;
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// Label row: "劃轉金額" + "全部劃轉"
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text('劃轉金額', style: AppTextStyles.headlineSmall(context).copyWith(color: colorScheme.onSurfaceVariant)),
|
|
GestureDetector(
|
|
onTap: () => _setQuickAmount(1.0),
|
|
child: Text('全部劃轉', style: AppTextStyles.labelLarge(context).copyWith(
|
|
color: colorScheme.secondary,
|
|
fontWeight: FontWeight.w600,
|
|
)),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
|
|
// Amount input field
|
|
GestureDetector(
|
|
onTap: () => _focusNode.requestFocus(),
|
|
child: Container(
|
|
width: double.infinity,
|
|
height: 56,
|
|
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.md),
|
|
decoration: BoxDecoration(
|
|
color: colorScheme.surfaceContainerHigh,
|
|
borderRadius: BorderRadius.circular(AppRadius.lg),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _amountController,
|
|
focusNode: _focusNode,
|
|
keyboardType: const TextInputType.numberWithOptions(decimal: true),
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,8}')),
|
|
],
|
|
style: AppTextStyles.numberLarge(context),
|
|
decoration: InputDecoration(
|
|
hintText: '0.00',
|
|
hintStyle: AppTextStyles.numberLarge(context).copyWith(
|
|
color: context.appColors.onSurfaceMuted,
|
|
),
|
|
border: InputBorder.none,
|
|
contentPadding: EdgeInsets.zero,
|
|
isDense: true,
|
|
),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(left: AppSpacing.sm),
|
|
child: Text('USDT', style: AppTextStyles.headlineMedium(context).copyWith(
|
|
fontWeight: FontWeight.w400,
|
|
color: context.appColors.onSurfaceMuted,
|
|
)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
|
|
// Percent buttons
|
|
Row(
|
|
children: [0.25, 0.50, 0.75, 1.0].asMap().entries.map((entry) {
|
|
final index = entry.key;
|
|
final percent = entry.value;
|
|
final label = '${(percent * 100).toInt()}%';
|
|
return Expanded(
|
|
child: Padding(
|
|
padding: EdgeInsets.only(left: index > 0 ? AppSpacing.sm : 0),
|
|
child: _buildPercentButton(label, percent),
|
|
),
|
|
);
|
|
}).toList(),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildPercentButton(String label, double percent) {
|
|
final colorScheme = Theme.of(context).colorScheme;
|
|
|
|
return GestureDetector(
|
|
onTap: () => _setQuickAmount(percent),
|
|
child: Container(
|
|
height: 36,
|
|
decoration: BoxDecoration(
|
|
color: colorScheme.surfaceContainerHigh,
|
|
borderRadius: BorderRadius.circular(AppRadius.sm),
|
|
),
|
|
child: Center(
|
|
child: Text(label, style: AppTextStyles.headlineSmall(context)),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// ============================================
|
|
// Tips card & Confirm button
|
|
// ============================================
|
|
|
|
Widget _buildTipsCard() {
|
|
final upColor = context.appColors.up;
|
|
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.md, vertical: 12),
|
|
decoration: BoxDecoration(
|
|
color: context.appColors.upBackground,
|
|
borderRadius: BorderRadius.circular(AppRadius.lg),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Icon(LucideIcons.info, size: 16, color: upColor),
|
|
const SizedBox(width: AppSpacing.sm),
|
|
Expanded(
|
|
child: Text(
|
|
'劃轉即時到賬,無需手續費',
|
|
style: AppTextStyles.bodyMedium(context).copyWith(color: upColor),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildConfirmButton() {
|
|
final colorScheme = Theme.of(context).colorScheme;
|
|
// 金色主題感知: Dark #D4AF37 (primary), Light #F59E0B (secondary)
|
|
final goldAccent = colorScheme.brightness == Brightness.dark
|
|
? colorScheme.primary
|
|
: colorScheme.secondary;
|
|
|
|
return SizedBox(
|
|
width: double.infinity,
|
|
height: 52,
|
|
child: GestureDetector(
|
|
onTap: _isLoading ? null : _doTransfer,
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
color: goldAccent, // 使用金色 #F59E0B
|
|
borderRadius: BorderRadius.circular(AppRadius.lg),
|
|
),
|
|
child: Center(
|
|
child: _isLoading
|
|
? const SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
|
|
),
|
|
)
|
|
: const Text(
|
|
'確認劃轉',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// ============================================
|
|
// Helpers
|
|
// ============================================
|
|
|
|
String _formatBalance(String balance) {
|
|
final val = double.tryParse(balance);
|
|
if (val == null) return '0.00';
|
|
return val.toStringAsFixed(2);
|
|
}
|
|
}
|