import 'package:flutter/material.dart'; import '../data/models/coin.dart'; import '../data/services/market_service.dart'; /// 行情状态管理 class MarketProvider extends ChangeNotifier { final MarketService _marketService; List _allCoins = []; bool _isLoading = false; String? _error; bool _coinsLoaded = false; MarketProvider(this._marketService); // Getters List get allCoins => _allCoins; bool get isLoading => _isLoading; String? get error => _error; /// BTC 和 ETH(上半区展示) List get featuredCoins => _allCoins.where((c) => c.code == 'BTC' || c.code == 'ETH').toList(); /// 排除 BTC、ETH、USDT 的代币列表(下半区展示) List get otherCoins => _allCoins .where((c) => !{'BTC', 'ETH', 'USDT'}.contains(c.code)) .toList(); /// 加载币种列表 Future loadCoins({bool force = false}) async { if (_coinsLoaded && !force && _allCoins.isNotEmpty) { return; } _isLoading = true; _error = null; notifyListeners(); try { final response = await _marketService.getCoinList(); if (response.success) { _allCoins = response.data ?? []; _coinsLoaded = true; } else { _error = response.message; } } catch (e) { _error = '加载失败: $e'; } _isLoading = false; notifyListeners(); } /// 根据代码获取币种 Coin? getCoinByCode(String code) { try { return _allCoins.firstWhere((c) => c.code == code); } catch (_) { return null; } } /// 刷新 Future refresh() async { await loadCoins(force: true); } /// 重置加载状态(用于退出登录时) void resetLoadState() { _coinsLoaded = false; _allCoins = []; _error = null; notifyListeners(); } }