我想在我的Flutter应用程序中使用设备的手电筒,在一定的时间间隔内打开和关闭它,我正在寻找一种方法来使用它。我在pub.dev上找到的所有软件包目前都不兼容dart 3,所以我想知道是否有其他软件包可以帮助我解决这个问题,或者我是否可以在没有软件包的情况下解决这个问题?谢啦,谢啦
gab6jxml1#
有几个软件包(与Dart 3兼容)可用于使用设备的手电筒,如下所示:
android.hardware.camera2.CameraManager
Android
tct7dpnv2#
使用相机依赖性并尝试以下演示:
import 'package:camera/camera.dart'; import 'package:flutter/material.dart'; class CameraFlashLightScreen extends StatefulWidget { const CameraFlashLightScreen({super.key}); @override State<CameraFlashLightScreen> createState() => _CameraFlashLightScreenState(); } class _CameraFlashLightScreenState extends State<CameraFlashLightScreen> { late CameraController cameraController; late Future<void> cameraInitializeFuture; late bool _isFlashOn = false; @override void initState() { super.initState(); cameraInitializeFuture = initializeCamera(); } Future<void> initializeCamera() async { final cameras = await availableCameras(); final backCamera = cameras.firstWhere( (camera) => camera.lensDirection == CameraLensDirection.back, orElse: () => cameras.first, ); cameraController = CameraController( backCamera, ResolutionPreset.low, ); await cameraController.initialize(); } Future<void> toggleFlashLight() async { try { if (_isFlashOn) { await cameraController.setFlashMode(FlashMode.off); } else { await cameraController.setFlashMode(FlashMode.torch); } setState(() { _isFlashOn = !_isFlashOn; }); } catch (e) {} } @override void dispose() { cameraController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Flashlight App'), ), body: FutureBuilder<void>( future: cameraInitializeFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return Center( child: ElevatedButton( onPressed: toggleFlashLight, child: Text( _isFlashOn ? 'Turn Off Flashlight' : 'Turn On Flashlight'), ), ); } else { return Center( child: CircularProgressIndicator(), ); } }, ), ); } }
字符串
2条答案
按热度按时间gab6jxml1#
有几个软件包(与Dart 3兼容)可用于使用设备的手电筒,如下所示:
android.hardware.camera2.CameraManager
来访问Android
的torch。tct7dpnv2#
使用相机依赖性并尝试以下演示:
字符串