This commit is contained in:
sion
2026-03-28 18:57:45 +08:00
parent 16272bcf18
commit 43a586c866
3 changed files with 260 additions and 257 deletions

View File

@@ -7,26 +7,28 @@ class MarketProvider extends ChangeNotifier {
final MarketService _marketService;
List<Coin> _allCoins = [];
List<Coin> _filteredCoins = [];
String _activeTab = 'all';
String _searchKeyword = '';
bool _isLoading = false;
String? _error;
bool _coinsLoaded = false; // 标记是否已加载
bool _coinsLoaded = false;
MarketProvider(this._marketService);
// Getters
List<Coin> get coins => _filteredCoins;
List<Coin> get allCoins => _allCoins;
bool get isLoading => _isLoading;
String? get error => _error;
String get activeTab => _activeTab;
String get searchKeyword => _searchKeyword;
/// BTC 和 ETH上半区展示
List<Coin> get featuredCoins =>
_allCoins.where((c) => c.code == 'BTC' || c.code == 'ETH').toList();
/// 排除 BTC、ETH、USDT 的代币列表(下半区展示)
List<Coin> get otherCoins => _allCoins
.where((c) => !{'BTC', 'ETH', 'USDT'}.contains(c.code))
.toList();
/// 加载币种列表
Future<void> loadCoins({bool force = false}) async {
// 如果已经加载过且不是强制刷新,则跳过
if (_coinsLoaded && !force && _allCoins.isNotEmpty) {
return;
}
@@ -40,7 +42,6 @@ class MarketProvider extends ChangeNotifier {
if (response.success) {
_allCoins = response.data ?? [];
_filterCoins();
_coinsLoaded = true;
} else {
_error = response.message;
@@ -53,49 +54,6 @@ class MarketProvider extends ChangeNotifier {
notifyListeners();
}
/// 设置分类标签
void setTab(String tab) {
_activeTab = tab;
_filterCoins();
notifyListeners();
}
/// 搜索
void search(String keyword) {
_searchKeyword = keyword;
_filterCoins();
notifyListeners();
}
/// 清除搜索
void clearSearch() {
_searchKeyword = '';
_filterCoins();
notifyListeners();
}
/// 筛选币种
void _filterCoins() {
List<Coin> result = List.from(_allCoins);
// 按分类筛选
if (_activeTab == 'realtime') {
result = result.where((c) => c.isRealtime).toList();
} else if (_activeTab == 'hot') {
result = result.take(6).toList();
}
// 按关键词筛选
if (_searchKeyword.isNotEmpty) {
final kw = _searchKeyword.toLowerCase();
result = result.where((c) =>
c.code.toLowerCase().contains(kw) ||
c.name.toLowerCase().contains(kw)).toList();
}
_filteredCoins = result;
}
/// 根据代码获取币种
Coin? getCoinByCode(String code) {
try {
@@ -114,7 +72,6 @@ class MarketProvider extends ChangeNotifier {
void resetLoadState() {
_coinsLoaded = false;
_allCoins = [];
_filteredCoins = [];
_error = null;
notifyListeners();
}