Files
monisuo/flutter_monisuo/lib/ui/components/trade_button.dart

123 lines
3.0 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import '../../core/theme/app_color_scheme.dart';
/// 交易按钮组件 - 买入/卖出按钮
class TradeButton extends StatelessWidget {
final bool isBuy;
final String? coinCode;
final VoidCallback? onPressed;
final bool isLoading;
final bool fullWidth;
const TradeButton({
super.key,
this.isBuy = true,
this.coinCode,
this.onPressed,
this.isLoading = false,
this.fullWidth = false,
});
/// 买入按钮
const TradeButton.buy({
super.key,
this.coinCode,
this.onPressed,
this.isLoading = false,
this.fullWidth = false,
}) : isBuy = true;
/// 卖出按钮
const TradeButton.sell({
super.key,
this.coinCode,
this.onPressed,
this.isLoading = false,
this.fullWidth = false,
}) : isBuy = false;
@override
Widget build(BuildContext context) {
final color = isBuy ? AppColorScheme.up : AppColorScheme.down;
final text = isBuy ? '买入${coinCode != null ? ' $coinCode' : ''}' : '卖出${coinCode != null ? ' $coinCode' : ''}';
final icon = isBuy ? LucideIcons.arrowDownToLine : LucideIcons.arrowUpFromLine;
final button = ShadButton(
backgroundColor: color,
onPressed: isLoading ? null : onPressed,
child: Row(
mainAxisSize: fullWidth ? MainAxisSize.max : MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (isLoading)
const SizedBox.square(
dimension: 16,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
else
Icon(icon, size: 18, color: Colors.white),
const SizedBox(width: 8),
Text(
text,
style: const TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
],
),
);
if (fullWidth) {
return SizedBox(width: double.infinity, child: button);
}
return button;
}
}
/// 交易按钮组 - 同时显示买入和卖出按钮
class TradeButtonGroup extends StatelessWidget {
final String? coinCode;
final VoidCallback? onBuyPressed;
final VoidCallback? onSellPressed;
final bool isBuyLoading;
final bool isSellLoading;
const TradeButtonGroup({
super.key,
this.coinCode,
this.onBuyPressed,
this.onSellPressed,
this.isBuyLoading = false,
this.isSellLoading = false,
});
@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child: TradeButton.buy(
coinCode: coinCode,
onPressed: onBuyPressed,
isLoading: isBuyLoading,
),
),
const SizedBox(width: 12),
Expanded(
child: TradeButton.sell(
coinCode: coinCode,
onPressed: onSellPressed,
isLoading: isSellLoading,
),
),
],
);
}
}