Files
monisuo/flutter_monisuo/lib/data/models/coin.dart
2026-04-07 01:05:05 +08:00

107 lines
2.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 幣種模型
class Coin {
final int id;
final String code;
final String name;
final String? icon;
final double price;
final double? priceUsd;
final double? priceCny;
final int priceType; // 1=實時價格, 2=管理員設置
final double change24h;
final double? high24h;
final double? low24h;
final double? volume24h;
final int status;
final int sort;
Coin({
required this.id,
required this.code,
required this.name,
this.icon,
required this.price,
this.priceUsd,
this.priceCny,
required this.priceType,
required this.change24h,
this.high24h,
this.low24h,
this.volume24h,
required this.status,
this.sort = 0,
});
factory Coin.fromJson(Map<String, dynamic> json) {
return Coin(
id: json['id'] as int? ?? 0,
code: json['code'] as String? ?? '',
name: json['name'] as String? ?? '',
icon: json['icon'] as String?,
price: (json['price'] as num?)?.toDouble() ?? 0,
priceUsd: (json['priceUsd'] as num?)?.toDouble(),
priceCny: (json['priceCny'] as num?)?.toDouble(),
priceType: json['priceType'] as int? ?? 1,
change24h: (json['change24h'] as num?)?.toDouble() ?? 0,
high24h: (json['high24h'] as num?)?.toDouble(),
low24h: (json['low24h'] as num?)?.toDouble(),
volume24h: (json['volume24h'] as num?)?.toDouble(),
status: json['status'] as int? ?? 1,
sort: json['sort'] as int? ?? 0,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'code': code,
'name': name,
'icon': icon,
'price': price,
'priceUsd': priceUsd,
'priceCny': priceCny,
'priceType': priceType,
'change24h': change24h,
'high24h': high24h,
'low24h': low24h,
'volume24h': volume24h,
'status': status,
'sort': sort,
};
}
/// 顯示圖標Unicode 符號)
String get displayIcon {
const icons = {
'BTC': '\u20BF',
'ETH': '\u039E',
'SOL': '\u25CD',
'USDT': '\u20AE',
'DOGE': '\uD83D\uDC15',
'XRP': '\u2715',
'BNB': '\u25B3',
'ADA': '\u25C6',
};
return icons[code] ?? '\u25CF';
}
/// 格式化價格顯示
String get formattedPrice {
if (price >= 1000) return price.toStringAsFixed(2);
if (price >= 1) return price.toStringAsFixed(4);
return price.toStringAsFixed(6);
}
/// 格式化漲跌幅
String get formattedChange {
final prefix = change24h >= 0 ? '+' : '';
return '$prefix${change24h.toStringAsFixed(2)}%';
}
/// 是否上漲
bool get isUp => change24h >= 0;
/// 是否為實時價格
bool get isRealtime => priceType == 1;
}