From 25ffb755c1a6096b07d14fd2dfb292442faec989 Mon Sep 17 00:00:00 2001 From: Yandrik Date: Sun, 10 Nov 2024 12:11:40 +0100 Subject: [PATCH] feat: improved refresh logic --- lib/main.dart | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index aba911f..308723a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -70,19 +70,52 @@ class MainPage extends StatefulWidget { createState() => _MainPageState(); } -class _MainPageState extends State { +class _MainPageState extends State with WidgetsBindingObserver { int _currentIndex = 0; final List _pages = [ const MapPage(), - const SettingsPage(), // Assume this is your settings page widget + const SettingsPage(), ]; + // Variable to keep track of the last lifecycle state + AppLifecycleState? _lastLifecycleState; + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addObserver(this); + } + + @override + void dispose() { + WidgetsBinding.instance.removeObserver(this); + super.dispose(); + } + void _onItemTapped(int index) { setState(() { _currentIndex = index; }); } + // Listen for app lifecycle changes + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + // Check if the app is transitioning from paused to resumed + print('DEBUG: lifecycle state changed from $_lastLifecycleState to $state'); + if (_lastLifecycleState == AppLifecycleState.paused && + (state == AppLifecycleState.resumed || + state == AppLifecycleState.inactive || + state == AppLifecycleState.detached || + state == AppLifecycleState.hidden)) { + print('DEBUG: app is unpaused, triggering refresh'); + // Trigger the refresh when the app is "unpaused" + context.read().triggerRefresh(); + } + // Update the last lifecycle state + _lastLifecycleState = state; + } + @override Widget build(BuildContext context) { return Scaffold(