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:

dependencies:
  flutter:
    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{
  @override
  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

  @override
  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;
       });
    });
    super.initState();
  }

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

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

              Container( 
                  child: FlatButton.icon( 
                    onPressed: (){
                        if(isturnon){
                              //if light is on, then turn off
                              Flashlight.lightOff();
                              setState(() {
                                  isturnon = false;
                                  flashicon = Icons.flash_off;
                                  flashbtncolor = Colors.deepOrangeAccent;                      
                              });  
                        }else{
                          //if light is off, then turn on.
                              Flashlight.lightOn(); 
                              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),
                           ),

                  )
              )

         ],);
    }else{
        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...