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