torch_light 1.1.0 torch_light: ^1.1.0 copied to clipboard
A Flutter plugin to check if the device has a torch / flashlight, and to turn it on and off.
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:torch_light/torch_light.dart';
void main() {
runApp(TorchApp());
}
class TorchApp extends StatefulWidget {
@override
_TorchAppState createState() => _TorchAppState();
}
class _TorchAppState extends State<TorchApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: const [Locale('en', '')],
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
home: TorchController(),
);
}
}
class TorchController extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('torch_light example app'),
),
body: FutureBuilder<bool>(
future: _isTorchAvailable(context),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData && snapshot.data!) {
return Column(
children: [
Expanded(
child: Center(
child: ElevatedButton(
child: const Text('Enable torch'),
onPressed: () async {
_enableTorch(context);
},
),
),
),
Expanded(
child: Center(
child: ElevatedButton(
child: const Text('Disable torch'),
onPressed: () {
_disableTorch(context);
},
),
),
),
],
);
} else if (snapshot.hasData) {
return const Center(
child: Text('No torch available.'),
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
Future<bool> _isTorchAvailable(BuildContext context) async {
final scaffoldMessenger = ScaffoldMessenger.of(context);
try {
return await TorchLight.isTorchAvailable();
} on Exception catch (_) {
scaffoldMessenger.showSnackBar(
const SnackBar(
content: Text('Could not check if the device has an available torch'),
),
);
rethrow;
}
}
Future<void> _enableTorch(BuildContext context) async {
final scaffoldMessenger = ScaffoldMessenger.of(context);
try {
await TorchLight.enableTorch();
} on Exception catch (_) {
scaffoldMessenger.showSnackBar(
const SnackBar(
content: Text('Could not enable torch'),
),
);
}
}
Future<void> _disableTorch(BuildContext context) async {
final scaffoldMessenger = ScaffoldMessenger.of(context);
try {
await TorchLight.disableTorch();
} on Exception catch (_) {
scaffoldMessenger.showSnackBar(
const SnackBar(
content: Text('Could not disable torch'),
),
);
}
}
}