111
This commit is contained in:
@@ -27,18 +27,23 @@ class MainPage extends StatefulWidget {
|
||||
State<MainPage> createState() => MainPageState();
|
||||
}
|
||||
|
||||
class MainPageState extends State<MainPage> {
|
||||
class MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
||||
int _currentIndex = 0;
|
||||
final Set<int> _loadedPages = {0};
|
||||
String? _tradeCoinCode; // 交易頁面選中的幣種代碼
|
||||
late final List<Widget> _pages;
|
||||
bool _isAppVisible = true;
|
||||
|
||||
// 防抖:記錄上次刷新時間,同一 Tab 500ms 內不重複刷新
|
||||
final Map<int, DateTime> _lastRefreshTime = {};
|
||||
|
||||
/// 当前页面是否可见(供子页面判断是否需要轮询)
|
||||
bool isPageVisible(int pageIndex) => _currentIndex == pageIndex && _isAppVisible;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_pages = [
|
||||
const HomePage(),
|
||||
const MarketPage(),
|
||||
@@ -48,6 +53,17 @@ class MainPageState extends State<MainPage> {
|
||||
];
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
_isAppVisible = state == AppLifecycleState.resumed;
|
||||
}
|
||||
|
||||
void _onTabChanged(int index) {
|
||||
final wasLoaded = _loadedPages.contains(index);
|
||||
setState(() {
|
||||
|
||||
Reference in New Issue
Block a user