is_lock_screen2 2.0.1 copy "is_lock_screen2: ^2.0.1" to clipboard
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.

example/lib/main.dart

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 ↓================');
  }
}
6
likes
150
points
5.18k
downloads

Publisher

unverified uploader

Weekly Downloads

Detects if device is in lock screen. Useful for determining whether app entered background due to locking screen or leaving app.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on is_lock_screen2