proximity_screen_lock 1.1.0 copy "proximity_screen_lock: ^1.1.0" to clipboard
proximity_screen_lock: ^1.1.0 copied to clipboard

A plugin that can be used to bind screen activation to proximity sensor

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:proximity_screen_lock/proximity_screen_lock.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var isActive = false;
  var isSupported = false;

  @override
  void initState() {
    super.initState();
    setProximitySensorActive();
    ProximityScreenLock.isProximityLockSupported()
        .then((isSupported) => setState(() => this.isSupported = isSupported));
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> setProximitySensorActive() async {
    try {
      await ProximityScreenLock.setActive(isActive);
    } catch (e) {
      debugPrint('Something went wrong: $e');
    }
  }

  void toggle() {
    setState(() => isActive = !isActive);
    setActive(isActive);
  }

  void setActive(bool value) {
    ProximityScreenLock.setActive(value);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(isSupported ? "Supported" : "Not supported", style: Theme.of(context).textTheme.bodyText1,),
              TextButton(
                onPressed: toggle,
                child: Text(
                  isActive ? 'De-activate' : 'Activate',
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}