void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MultiProvider(providers: [
ChangeNotifierProvider(create: (_) => UserProvider()),
], child: const MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
// Добавьте AppStateObserver в качестве наблюдателя
WidgetsBinding.instance!.addObserver(AppStateObserver());
}
@override
void dispose() {
// Удалите AppStateObserver при завершении работы приложения
WidgetsBinding.instance!.removeObserver(AppStateObserver());
super.dispose();
}
import 'package:flutter/material.dart';
class AppStateObserver with WidgetsBindingObserver {
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
// Приложение свернуто
print('Приложение свернуто');
} else if (state == AppLifecycleState.resumed) {
// Приложение развернуто
print('Приложение развернуто');
}
}
}