Switch ON or OFF Flashlight With Flutter

Are you making flashlight app or using flashlight feature in your torch application ? Then, see the example below to access flashlight features using Flutter. The output of the example are:

Add Flashlight Flutter package in your pubspec.yaml like below:

    sdk: flutter
  flashlight: ^0.1.1

This package have the following functions:

  • await Flashlight.hasFlashlight; // to check if there is flashlight
  • Flashlight.lightOn(); // to turn ON flashlight
  • Flashlight.lightOff(); // to turn OFF flashlight

See the example below in which it checks there is flashlight in your device, and applied TURN ON or TURN OFF feature using single button. Icon and color of flashlight button is changed on press.

import 'package:flashlight/flashlight.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class FlashLight extends StatefulWidget{
  State createState() {
    return _FlashLight();

class _FlashLight extends State{
  bool hasflashlight = false; //to set is there any flashlight ?
  bool isturnon = false; //to set if flash light is on or off
  IconData flashicon = Icons.flash_off; //icon for lashlight button
  Color flashbtncolor = Colors.deepOrangeAccent; //color for flash button

  void initState() {
    Future.delayed(Duration.zero,() async {
       //we use Future.delayed because there is async function inside it.
       bool istherelight = await Flashlight.hasFlashlight;
       setState(() {
          hasflashlight = istherelight;

  Widget build(BuildContext context) {
    return Scaffold(
      appBar:AppBar(title:Text("Flash Light")),
      body: Container( 
          alignment: Alignment.center,
          padding: EdgeInsets.all(40),
          //set width and height of outermost wrapper to 100%;

  Widget flashlightbutton(){
         return Column(children: [
              Text("Your device has flash light."),
              Text(isturnon ? "Flash is ON" : 'Flash is OFF'),

                  child: FlatButton.icon( 
                    onPressed: (){
                              //if light is on, then turn off
                              setState(() {
                                  isturnon = false;
                                  flashicon = Icons.flash_off;
                                  flashbtncolor = Colors.deepOrangeAccent;                      
                          //if light is off, then turn on.
                              setState(() {
                                isturnon = true;
                                flashicon = Icons.flash_on;
                                flashbtncolor = Colors.greenAccent;                      
                    icon: Icon(flashicon, color: Colors.white,),
                    color: flashbtncolor, 
                    label: Text(isturnon ? 'TURN OFF' : 'TURN ON',
                                style: TextStyle(color: Colors.white),


        return Text("Your device do not have flash light.");


Use flashlight feature in your app like above in the example.

No any Comments on this Article

Please Wait...