import 'package:flutter/material.dart'; import '../../../../core/theme/app_color_scheme.dart'; import '../../../../core/theme/app_spacing.dart'; import '../../../../core/theme/app_theme.dart'; /// 金額輸入框組件(含超額提示) /// /// 設計稿:bg-tertiary,圓角md,高48。 /// 輸入金額超過可用 USDT 餘額時顯示警告提示。 class AmountInput extends StatefulWidget { final TextEditingController amountController; final String maxAmount; final bool isBuy; final Color actionColor; final VoidCallback onChanged; const AmountInput({ super.key, required this.amountController, required this.maxAmount, required this.isBuy, required this.actionColor, required this.onChanged, }); @override State createState() => _AmountInputState(); } class _AmountInputState extends State { bool _isExceeded = false; void _checkLimit() { final input = double.tryParse(widget.amountController.text) ?? 0; final max = double.tryParse(widget.maxAmount) ?? 0; final exceeded = widget.isBuy && input > max && max > 0 && input > 0; if (exceeded != _isExceeded) { setState(() => _isExceeded = exceeded); } widget.onChanged(); } @override void initState() { super.initState(); widget.amountController.addListener(_checkLimit); } @override void dispose() { widget.amountController.removeListener(_checkLimit); super.dispose(); } @override Widget build(BuildContext context) { final colorScheme = Theme.of(context).colorScheme; final warningColor = AppColorScheme.warning; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( height: 48, decoration: BoxDecoration( color: colorScheme.surfaceContainerHighest.withOpacity(0.5), // 調整透明度 borderRadius: BorderRadius.circular(AppRadius.md), ), child: TextField( controller: widget.amountController, keyboardType: const TextInputType.numberWithOptions(decimal: true), onChanged: (_) => _checkLimit(), style: AppTextStyles.numberMedium(context).copyWith( fontWeight: FontWeight.w400, ), decoration: InputDecoration( hintText: '請輸入金額', hintStyle: AppTextStyles.numberMedium(context).copyWith( fontWeight: FontWeight.w400, color: colorScheme.onSurfaceVariant.withOpacity(0.5), ), border: InputBorder.none, contentPadding: const EdgeInsets.symmetric( horizontal: AppSpacing.md, ), ), ), ), if (_isExceeded) Padding( padding: EdgeInsets.only(top: AppSpacing.xs), child: Row( children: [ Icon(Icons.error_outline, size: 13, color: warningColor), SizedBox(width: AppSpacing.xs), Text( '超出可用USDT餘額', style: AppTextStyles.bodySmall(context).copyWith( color: warningColor, ), ), ], ), ), ], ); } }