is_lock_screen2 2.0.1 is_lock_screen2: ^2.0.1 copied to clipboard
Detects if device is in lock screen. Useful for determining whether app entered background due to locking screen or leaving app.
import 'package:flutter/material.dart';
import 'package:is_lock_screen2/is_lock_screen2.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.inactive) {
log('app inactive, is lock screen: ${await isLockScreen()}');
} else if (state == AppLifecycleState.resumed) {
log('app resumed');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text(
'Test by changing app lifecycle (locking device / exiting app). \nResult will be print in console.'),
),
),
);
}
log(String msg) {
print('============= ↓ start ↓================');
print("= $msg ");
print('============= ↓ end ↓================');
}
}