Files
monisuo/flutter_monisuo/lib/data/models/coin.dart

107 lines
2.6 KiB
Dart
Raw Normal View History

/// 币种模型
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;
}