rwadurian/frontend/mobile-app/lib/features/trading/presentation/pages/trading_page.dart

387 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
/// 结算币种枚举
enum SettlementCurrency { bnb, og, usdt, dst }
/// 交易页面 - 显示可结算收益和交易功能
/// 支持一键结算和DST转USDT
class TradingPage extends ConsumerStatefulWidget {
const TradingPage({super.key});
@override
ConsumerState<TradingPage> createState() => _TradingPageState();
}
class _TradingPageState extends ConsumerState<TradingPage> {
// 当前选中的结算币种
SettlementCurrency _selectedCurrency = SettlementCurrency.usdt;
// 模拟数据
final double _settleableAmount = 1234.56;
final double _dstBalance = 5678.90;
/// 选择结算币种
void _selectCurrency(SettlementCurrency currency) {
setState(() {
_selectedCurrency = currency;
});
}
/// 一键结算
void _onSettlement() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('确认结算'),
content: Text(
'确定将 ${_formatNumber(_settleableAmount)} USDT 结算为 ${_getCurrencyName(_selectedCurrency)} 吗?',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('取消'),
),
TextButton(
onPressed: () {
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('结算成功'),
backgroundColor: Color(0xFFD4AF37),
),
);
},
child: const Text('确认'),
),
],
),
);
}
/// 卖出DST转换为USDT
void _onSellDst() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('卖出 DST'),
content: Text(
'确定将 ${_formatNumber(_dstBalance)} DST 转换为 USDT 吗?',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('取消'),
),
TextButton(
onPressed: () {
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('转换成功'),
backgroundColor: Color(0xFFD4AF37),
),
);
},
child: const Text('确认'),
),
],
),
);
}
/// 格式化数字(添加千分位)
String _formatNumber(double number) {
final parts = number.toStringAsFixed(2).split('.');
final intPart = parts[0].replaceAllMapped(
RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'),
(Match m) => '${m[1]},',
);
return '$intPart.${parts[1]}';
}
/// 获取币种名称
String _getCurrencyName(SettlementCurrency currency) {
switch (currency) {
case SettlementCurrency.bnb:
return 'BNB';
case SettlementCurrency.og:
return 'OG';
case SettlementCurrency.usdt:
return 'USDT';
case SettlementCurrency.dst:
return 'DST';
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Container(
width: double.infinity,
height: double.infinity,
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xFFFFF5E6),
Color(0xFFFFE4B5),
],
),
),
child: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
// 顶部标题栏
_buildAppBar(),
const SizedBox(height: 16),
// 可结算收益卡片
_buildSettleableCard(),
const SizedBox(height: 24),
// 一键结算按钮
_buildSettlementButton(),
const SizedBox(height: 16),
// 结算币种选择
_buildCurrencySelector(),
const SizedBox(height: 24),
// 分隔线
_buildDivider(),
const SizedBox(height: 24),
// 卖出DST按钮
_buildSellDstButton(),
const SizedBox(height: 8),
// DST余额显示
_buildDstBalance(),
],
),
),
),
),
);
}
/// 构建顶部标题栏
Widget _buildAppBar() {
return Container(
height: 56,
padding: const EdgeInsets.symmetric(horizontal: 16),
child: const Center(
child: Text(
'交易',
style: TextStyle(
fontSize: 18,
fontFamily: 'Inter',
fontWeight: FontWeight.w700,
height: 1.25,
letterSpacing: -0.27,
color: Color(0xFF5D4037),
),
),
),
);
}
/// 构建可结算收益卡片
Widget _buildSettleableCard() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: const Color(0x1A8B5A2B),
borderRadius: BorderRadius.circular(12),
),
child: Column(
children: [
const Text(
'可结算收益',
style: TextStyle(
fontSize: 14,
fontFamily: 'Inter',
height: 1.5,
color: Color(0xCC8B5A2B),
),
),
const SizedBox(height: 8),
Text(
'${_formatNumber(_settleableAmount)} USDT',
style: const TextStyle(
fontSize: 32,
fontFamily: 'Inter',
fontWeight: FontWeight.w700,
height: 1.25,
letterSpacing: -0.8,
color: Color(0xFF5D4037),
),
),
],
),
),
);
}
/// 构建一键结算按钮
Widget _buildSettlementButton() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: GestureDetector(
onTap: _onSettlement,
child: Container(
width: double.infinity,
height: 56,
decoration: BoxDecoration(
color: const Color(0xFFD4AF37),
borderRadius: BorderRadius.circular(12),
),
child: const Center(
child: Text(
'一键结算',
style: TextStyle(
fontSize: 16,
fontFamily: 'Inter',
fontWeight: FontWeight.w700,
height: 1.5,
letterSpacing: 0.24,
color: Colors.white,
),
),
),
),
),
);
}
/// 构建结算币种选择器
Widget _buildCurrencySelector() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
const Text(
'结算的币种',
style: TextStyle(
fontSize: 14,
fontFamily: 'Inter',
fontWeight: FontWeight.w700,
height: 1.5,
letterSpacing: 0.21,
color: Color(0xCC8B5A2B),
),
),
const SizedBox(height: 12),
Container(
padding: const EdgeInsets.all(6),
decoration: BoxDecoration(
color: const Color(0x1A8B5A2B),
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
_buildCurrencyChip(SettlementCurrency.bnb, 'BNB'),
const SizedBox(width: 8),
_buildCurrencyChip(SettlementCurrency.og, 'OG'),
const SizedBox(width: 8),
_buildCurrencyChip(SettlementCurrency.usdt, 'USDT'),
const SizedBox(width: 8),
_buildCurrencyChip(SettlementCurrency.dst, 'DST'),
],
),
),
],
),
);
}
/// 构建币种选择标签
Widget _buildCurrencyChip(SettlementCurrency currency, String label) {
final isSelected = _selectedCurrency == currency;
return Expanded(
child: GestureDetector(
onTap: () => _selectCurrency(currency),
child: Container(
height: 40,
decoration: BoxDecoration(
color: isSelected ? const Color(0xFFD4AF37) : Colors.transparent,
borderRadius: BorderRadius.circular(8),
),
child: Center(
child: Text(
label,
style: TextStyle(
fontSize: 14,
fontFamily: 'Inter',
fontWeight: isSelected ? FontWeight.w700 : FontWeight.w600,
height: 1.43,
color: isSelected ? Colors.white : const Color(0xCC5D4037),
),
),
),
),
),
);
}
/// 构建分隔线
Widget _buildDivider() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Container(
height: 1,
color: const Color(0x1A8B5A2B),
),
);
}
/// 构建卖出DST按钮
Widget _buildSellDstButton() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: GestureDetector(
onTap: _onSellDst,
child: Container(
width: double.infinity,
height: 56,
decoration: BoxDecoration(
color: const Color(0x338B5A2B),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: const Color(0x808B5A2B),
width: 1,
),
),
child: const Center(
child: Text(
'卖出 DST 转换为 USDT',
style: TextStyle(
fontSize: 16,
fontFamily: 'Inter',
fontWeight: FontWeight.w700,
height: 1.5,
letterSpacing: 0.24,
color: Color(0xFF8B5A2B),
),
),
),
),
),
);
}
/// 构建DST余额显示
Widget _buildDstBalance() {
return Text(
'DST 余额: ${_formatNumber(_dstBalance)}',
style: const TextStyle(
fontSize: 14,
fontFamily: 'Inter',
height: 1.5,
color: Color(0x995D4037),
),
);
}
}