Switch On and Off the Flash

This Article is posted by seven.srikanth at 8/20/2018 11:05:47 AM



<p>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.</p> <p>Here is the code, File: pubspec.yaml color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, "Courier New", monospace; line-height: 19px; white-space: pre-wrap;](color: rgb(86, 156, 214);](name: color: rgb(206, 145, 120);](flutterexamplecolor: rgb(86, 156, 214);](description: color: rgb(206, 145, 120);](A new Flutter project. color: rgb(86, 156, 214);](dependencies: color: rgb(86, 156, 214);](lamp: color: rgb(206, 145, 120);](^0.0.5 color: rgb(86, 156, 214);](flutter: color: rgb(86, 156, 214);](sdk: color: rgb(206, 145, 120);](flutter<br /> color: rgb(96, 139, 78);](# The following adds the Cupertino Icons font to your application. color: rgb(96, 139, 78);](# Use with the CupertinoIcons class for iOS style icons. color: rgb(86, 156, 214);](cupertino_icons: color: rgb(206, 145, 120);](^0.1.2 color: rgb(86, 156, 214);](dev_dependencies: color: rgb(86, 156, 214);](flutter_test: color: rgb(86, 156, 214);](sdk: color: rgb(206, 145, 120);](flutter</p> <p>color: rgb(96, 139, 78);](# For information on the generic Dart part of this file, see thecolor: rgb(96, 139, 78);](# following page: <a href="https://www.dartlang.org/tools/pub/pubspec">https://www.dartlang.org/tools/pub/pubspec</a> color: rgb(96, 139, 78);](# The following section is specific to Flutter.color: rgb(86, 156, 214);](flutter: color: rgb(96, 139, 78);](# The following line ensures that the Material Icons font is color: rgb(96, 139, 78);](# included with your application, so that you can use the icons in color: rgb(96, 139, 78);](# the material Icons class. color: rgb(86, 156, 214);](uses-material-design: color: rgb(86, 156, 214);](true color: rgb(96, 139, 78);](# To add assets to your application, add an assets section, like this: color: rgb(96, 139, 78);](# assets: color: rgb(96, 139, 78);](# - images/a_dot_burr.jpeg color: rgb(96, 139, 78);](# - images/a_dot_ham.jpeg color: rgb(96, 139, 78);](# An image asset can refer to one or more resolution-specific "variants", see color: rgb(96, 139, 78);](# <a href="https://flutter.io/assets-and-images/#resolution-aware">https://flutter.io/assets-and-images/#resolution-aware</a>. color: rgb(96, 139, 78);](# For details regarding adding assets from package dependencies, see color: rgb(96, 139, 78);](# <a href="https://flutter.io/assets-and-images/#from-packages">https://flutter.io/assets-and-images/#from-packages</a> color: rgb(96, 139, 78);](# To add custom fonts to your application, add a fonts section here, color: rgb(96, 139, 78);](# in this "fluttersection. Each entry in this list should have a color: rgb(96, 139, 78);](# "familykey with the font family name, and a "fontskey with a color: rgb(96, 139, 78);](# list giving the asset and other descriptors for the font. For color: rgb(96, 139, 78);](# example: color: rgb(96, 139, 78);](# fonts: color: rgb(96, 139, 78);](# - family: Schyler color: rgb(96, 139, 78);](# fonts: color: rgb(96, 139, 78);](# - asset: fonts/Schyler-Regular.ttf color: rgb(96, 139, 78);](# - asset: fonts/Schyler-Italic.ttf color: rgb(96, 139, 78);](# style: italic color: rgb(96, 139, 78);](# - family: Trajan Pro color: rgb(96, 139, 78);](# fonts: color: rgb(96, 139, 78);](# - asset: fonts/TrajanPro.ttf color: rgb(96, 139, 78);](# - asset: fonts/TrajanPro_Bold.ttf color: rgb(96, 139, 78);](# weight: 700 color: rgb(96, 139, 78);](# color: rgb(96, 139, 78);](# For details regarding fonts from package dependencies, color: rgb(96, 139, 78);](# see <a href="https://flutter.io/custom-fonts/#from-packages">https://flutter.io/custom-fonts/#from-packages</a></p> <p>File: \android\app\src\main\AndroidManifest.xml color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, "Courier New", monospace; line-height: 19px; white-space: pre-wrap;](color: rgb(128, 128, 128);]( color: rgb(96, 139, 78);]( color: rgb(128, 128, 128);]( color: rgb(96, 139, 78);]( color: rgb(128, 128, 128);]( color: rgb(128, 128, 128);]( color: rgb(96, 139, 78);]( color: rgb(128, 128, 128);]( color: rgb(128, 128, 128);]( color: rgb(128, 128, 128);]( color: rgb(128, 128, 128);]( color: rgb(128, 128, 128);]( color: rgb(128, 128, 128);]( color: rgb(128, 128, 128);](color: rgb(128, 128, 128);](</p> <p _hasFlash="hasFlash;" _isOn="!_isOn;" _intensity="intensity;">File: main.dart color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, "Courier New", monospace; line-height: 19px; white-space: pre-wrap;](color: rgb(86, 156, 214);](import color: rgb(206, 145, 120);]('package:flutter/material.dart';color: rgb(86, 156, 214);](import color: rgb(206, 145, 120);]('package:lamp/lamp.dart';color: rgb(86, 156, 214);](import color: rgb(206, 145, 120);]('dart:async'; color: rgb(86, 156, 214);](void color: rgb(220, 220, 170);](main() => color: rgb(220, 220, 170);](runApp(color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](MyApp()); color: rgb(86, 156, 214);](class color: rgb(78, 201, 176);](MyApp color: rgb(86, 156, 214);](extends color: rgb(78, 201, 176);](StatefulWidget { color: rgb(86, 156, 214);](@override color: rgb(78, 201, 176);](_MyAppState color: rgb(220, 220, 170);](createState() => color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](_MyAppState();} color: rgb(86, 156, 214);](class color: rgb(78, 201, 176);](_MyAppState color: rgb(86, 156, 214);](extends color: rgb(78, 201, 176);](State { color: rgb(86, 156, 214);](bool _hasFlash = color: rgb(86, 156, 214);](false; color: rgb(86, 156, 214);](bool _isOn = color: rgb(86, 156, 214);](false; color: rgb(86, 156, 214);](double _intensity = color: rgb(181, 206, 168);](1.0; color: rgb(86, 156, 214);](@override color: rgb(220, 220, 170);](initState() { color: rgb(86, 156, 214);](super.color: rgb(220, 220, 170);](initState(); color: rgb(220, 220, 170);](initPlatformState(); } color: rgb(220, 220, 170);](initPlatformState() color: rgb(197, 134, 192);](async { color: rgb(86, 156, 214);](bool hasFlash = color: rgb(197, 134, 192);](await color: rgb(78, 201, 176);](Lamp.hasLamp; color: rgb(220, 220, 170);](print(color: rgb(206, 145, 120);]("Device has flash ? $color: rgb(156, 220, 254);](hasFlashcolor: rgb(206, 145, 120);]("); color: rgb(220, 220, 170);](setState(() ); } color: rgb(86, 156, 214);](@override color: rgb(78, 201, 176);](Widget color: rgb(220, 220, 170);](build(color: rgb(78, 201, 176);](BuildContext context) { color: rgb(197, 134, 192);](return color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](MaterialApp( theme: color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](ThemeData(primarySwatch: color: rgb(78, 201, 176);](Colors.pink), home: color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](Scaffold( appBar: color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](AppBar(title: color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](Text(color: rgb(206, 145, 120);]('Lamp plugin example')), body: color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](Center( child: color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](Column( mainAxisAlignment: color: rgb(78, 201, 176);](MainAxisAlignment.center, children: [ color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](Text(color: rgb(206, 145, 120);]('Device has flash: $color: rgb(156, 220, 254);](_hasFlashcolor: rgb(215, 186, 125);]( color: rgb(206, 145, 120);]( Flash is on: $color: rgb(156, 220, 254);](_isOncolor: rgb(206, 145, 120);]('), color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](Slider(value: _intensity, onChanged: _isOn ? _intensityChanged : color: rgb(86, 156, 214);](null), color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](RaisedButton(onPressed: () color: rgb(197, 134, 192);](async => color: rgb(197, 134, 192);](await color: rgb(78, 201, 176);](Lamp.color: rgb(220, 220, 170);](flash(color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](Duration(seconds: color: rgb(181, 206, 168);](2)), child: color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](Text(color: rgb(206, 145, 120);]("Flash for 2 seconds")) ]), ), floatingActionButton: color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](FloatingActionButton( child: color: rgb(197, 134, 192);](new color: rgb(78, 201, 176);](Icon(_isOn ? color: rgb(78, 201, 176);](Icons.flash_off : color: rgb(78, 201, 176);](Icons.flash_on), onPressed: _turnFlash), ), ); } color: rgb(78, 201, 176);](Future color: rgb(220, 220, 170);](_turnFlash() color: rgb(197, 134, 192);](async { _isOn ? color: rgb(78, 201, 176);](Lamp.color: rgb(220, 220, 170);](turnOff() : color: rgb(78, 201, 176);](Lamp.color: rgb(220, 220, 170);](turnOn(intensity: _intensity); color: rgb(86, 156, 214);](var f = color: rgb(197, 134, 192);](await color: rgb(78, 201, 176);](Lamp.hasLamp; color: rgb(220, 220, 170);](setState(()); } color: rgb(220, 220, 170);](_intensityChanged(color: rgb(86, 156, 214);](double intensity) { color: rgb(78, 201, 176);](Lamp.color: rgb(220, 220, 170);](turnOn(intensity : intensity); color: rgb(220, 220, 170);](setState(()); } }</p> <p>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, ![](https://www.fluttercentral.com/Uploads/eb9e0699-2adb-4001-9069-b40c32fa7994.jpgwidth="50%height="auto](</p> <p>Source of this example: <a href="https://pub.dartlang.org/packages/lamp">https://pub.dartlang.org/packages/lamp</a> Thanks.</p>


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








1 Comments
Login to comment.
Recent Comments

chirag.intersoft at 1/15/2019

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

Login to Reply.