proximity_sensor 1.3.8 copy "proximity_sensor: ^1.3.8" to clipboard
proximity_sensor: ^1.3.8 copied to clipboard

simple and easy to use flutter plugin package for proximity sensor (only)

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' as foundation;
import 'dart:async';
import 'package:proximity_sensor/proximity_sensor.dart';

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isNear = false;
  late StreamSubscription<dynamic> _streamSubscription;

  @override
  void initState() {
    super.initState();
    listenSensor();
  }

  @override
  void dispose() {
    super.dispose();
    _streamSubscription.cancel();
  }

  Future<void> listenSensor() async {
    FlutterError.onError = (FlutterErrorDetails details) {
      if (foundation.kDebugMode) {
        FlutterError.dumpErrorToConsole(details);
      }
    };

    // -------------------------------------------------- <ANDROID ONLY>
    // NOTE: The following calls only work on Android. Otherwise, nothing happens.
    // You only need to make this call if you want to turn off the screen.
    // Add below permission in your AndroidManifest.xml file.
    //     <uses-permission android:name="android.permission.WAKE_LOCK"/>
    await ProximitySensor.setProximityScreenOff(true)
        .onError((error, stackTrace) {
      if (foundation.kDebugMode) {
        debugPrint("could not enable screen off functionality");
      }
      return null;
    });
    // -------------------------------------------------- <ANDROID ONLY>

    _streamSubscription = ProximitySensor.events.listen((int event) {
      setState(() {
        if (foundation.kDebugMode) {
          debugPrint("sensor event = $event");
        }
        _isNear = (event > 0) ? true : false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Proximity Sensor Example'),
        ),
        body: Center(
          child: Text('proximity sensor, is near ?  $_isNear\n'),
        ),
      ),
    );
  }
}

////////////////////////////////////////////////////////////////////////////////
void main() {
  runApp(MyApp());
}
42
likes
160
points
9.55k
downloads

Publisher

unverified uploader

Weekly Downloads

simple and easy to use flutter plugin package for proximity sensor (only)

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on proximity_sensor