torch_control 0.4.0 copy "torch_control: ^0.4.0" to clipboard
torch_control: ^0.4.0 copied to clipboard

A flutter plugin for controlling torch/flashlight/lamp with simple and clean API supporting Android and iOS.

torch_control #

A Flutter plugin for torch/flashlight/lamp control. It offers API for:

  • checking if torch is available
  • turning the torch on and off
  • toggling the torch
  • flashing the torch for a given duration

Platforms supported by the plugin:

  • Android old API levels 16 to 19 (4.1 to 4.4) via android.hardware.Camera
  • Android API levels 21 to 35 (5.+) via android.hardware.camera2
  • iOS (currently untested)

Install #

Add torch_control: 0.4.0 to your pubspec.yaml file

Set permissions for Android

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

Usage #

To check if a torch is available and ready to use:

await TorchControl.ready();

To turn on and off the torch:

TorchControl.turnOn();
TorchControl.turnOff();
TorchControl.turn(true);
TorchControl.turn(false);

To toggle the torch:

TorchControl.toggle();        // if is on it is turned off an vice versa

To check the torch state:

TorchControl.isOn();
TorchControl.isOff();
  • In the beginning the torch state is initialized to off. To ensure correctness of the on/off status you should call TorchControl.turnOff() (or TorchControl.turnOn()) somewhere at the beginning.

To flash the torch for a given duration:

TorchControl.flash(const Duration(seconds: 1));

Examples #

See the companion example for demonstration on how to use the plugin.

There are two of my apps in PlayStore that use this plugin:

Trivia #

Why yet another torch plugin? I needed a simple plugin for controlling the torch with the support for null safety, Dart 2, and a bit more flexible interface which was not readily available at the time. Moreover, I've never written a Flutter plugin as well as any Kotlin code before.

6
likes
160
points
34
downloads

Publisher

unverified uploader

Weekly Downloads

A flutter plugin for controlling torch/flashlight/lamp with simple and clean API supporting Android and iOS.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on torch_control