Switch On and Off the Flash

This Article is posted by seven.srikanth at 20-08-2018 11:05:47

Below example will help you to Switch On and Off the Flash using flutter on  Android Devices. This example is not tested on IOS devices.

Here is the code,

File: pubspec.yaml

name: flutterexample
description: A new Flutter project.

lamp: ^0.0.5
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2

sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.

# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true

# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.io/assets-and-images/#resolution-aware.

# For details regarding adding assets from package dependencies, see
# https://flutter.io/assets-and-images/#from-packages

# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
# For details regarding fonts from package dependencies,
# see https://flutter.io/custom-fonts/#from-packages

File: \android\app\src\main\AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
<uses-permission android:name="android.permission.INTERNET"/>

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
android:value="true" />
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>

File: main.dart

import 'package:flutter/material.dart';
import 'package:lamp/lamp.dart';
import 'dart:async';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
_MyAppState createState() => new _MyAppState();

class _MyAppState extends State<MyApp> {
bool _hasFlash = false;
bool _isOn = false;
double _intensity = 1.0;

initState() {

initPlatformState() async {
bool hasFlash = await Lamp.hasLamp;
print("Device has flash ? $hasFlash");
setState(() { _hasFlash = hasFlash; });

Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(primarySwatch: Colors.pink),
home: new Scaffold(
appBar: new AppBar(title: new Text('Lamp plugin example')),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text('Device has flash: $_hasFlash\n Flash is on: $_isOn'),
new Slider(value: _intensity, onChanged: _isOn ? _intensityChanged : null),
new RaisedButton(onPressed: () async => await Lamp.flash(new Duration(seconds: 2)), child: new Text("Flash for 2 seconds"))
floatingActionButton: new FloatingActionButton(
child: new Icon(_isOn ? Icons.flash_off : Icons.flash_on),
onPressed: _turnFlash),

Future _turnFlash() async {
_isOn ? Lamp.turnOff() : Lamp.turnOn(intensity: _intensity);
var f = await Lamp.hasLamp;
_hasFlash = f;
_isOn = !_isOn;

_intensityChanged(double intensity) {
Lamp.turnOn(intensity : intensity);
_intensity = intensity;


Once the app is deployed into Mobile, you need to ensure that Camera permissions are given by going into Setting.

Below is how the app looks like,

Source of this example: https://pub.dartlang.org/packages/lamp 


Tags: Switch On and Off the Flash; Flutter examples; Flutter Flash Light Example; Flutter Torch;

Login to comment.
Recent Comments

chirag.intersoft at 15-01-2019

Its not working on any android devices at all. Can you please help me out of this ?

Login to Reply.

Recent Articles

How to deploy flutter web site on GitHub using peanut

Posted by nikhilchaudhary12155 on 13 Oct 2019 Article

AnimatedPositioned Widget Example in Flutter

Posted by seven.srikanth on 10 Oct 2019 Article

Flutter Open-Source and directory lists

Posted by nishantkotatech on 09 Oct 2019 Article

Flutter Travelling Couple Leading Page - Flutter Web Series 3

Posted by nikhilchaudhary12155 on 05 Oct 2019 Article

© 2018 - Fluttercentral | Email to me - Seven.srikanth@gmail.com