Create a Torch Flashlight Application for Android/Ios with FLutter Dart

This Article is posted by theindianappguy at 11/8/2019 2:30:21 PM



In this tutorial, you are going to learn how to create a Torch Flashlight application for Android And Ios with Flutter on Android Studio. 
Creating the User Interface
I will just be adding Text with Gesture Detector when the user clicks on it we will turn on light if not already and turn off if it is on, for this we are using 
Torch Flutter Plugin: https://pub.dev/packages/torch
PS : not tested on IOS device 
Here is the code :
 
--> pubspec.yaml (adding torch plugin)
dependencies:
  flutter:
    sdk: flutter
  torch: ^0.1.1
-- > main.dart
import 'package:flash_light_app/Home.dart';
import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      //initialRoute: '/main',
      routes: {
        '/': (context) => Home(),
      },
    ));
-- > Home.dart
import 'package:flutter/material.dart';
import 'package:torch/torch.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  bool _isVisible = true;

  void showToast() {
    setState(() {
      _isVisible = !_isVisible;
    });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              GestureDetector(
                onTap: (){
                  if(_isVisible){
                    _isVisible = !_isVisible;
                    showToast;
                    Torch.turnOn();
                  }else{
                    _isVisible = !_isVisible;
                    showToast;
                    Torch.turnOff();
                  }
                },
                child: Visibility(
                  child: Text(
                    'Turn On/Off', style: TextStyle(fontSize: 16.0, backgroundColor: Colors.black, color: Colors.white),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Tags: flutter, flutter flashlight app, Create a Torch Flashlight Application for Android with Flutter








0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.


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