From 79cc43da0d723c7572ec6dbe70a0c3d3b801278b Mon Sep 17 00:00:00 2001 From: r4khul Date: Wed, 4 Feb 2026 13:15:24 +0530 Subject: [PATCH] Add QR scanner torch control using MobileScannerController --- lib/qr_code_screen.dart | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/qr_code_screen.dart b/lib/qr_code_screen.dart index e78131d..8dc8f49 100644 --- a/lib/qr_code_screen.dart +++ b/lib/qr_code_screen.dart @@ -12,8 +12,21 @@ class QrCodeScreen extends StatefulWidget { } class _QrCodeScreenState extends State { + late final MobileScannerController _controller; bool _scanned = false; + @override + void initState() { + super.initState(); + _controller = MobileScannerController(); + } + + @override + void dispose() { + _controller.dispose(); + super.dispose(); + } + void _onDetect(BarcodeCapture capture) async { if (_scanned) return; final barcode = capture.barcodes.first; @@ -31,8 +44,27 @@ class _QrCodeScreenState extends State { return Scaffold( appBar: AppBar( title: Text(AppLocalizations.of(context)!.settingsTitle), + actions: [ + ValueListenableBuilder( + valueListenable: _controller, + builder: (context, state, _) { + final torchState = state.torchState; + return IconButton( + icon: Icon( + torchState == TorchState.on + ? Icons.flash_on + : Icons.flash_off, + ), + onPressed: torchState == TorchState.unavailable + ? null + : () => _controller.toggleTorch(), + ); + }, + ), + ], ), body: MobileScanner( + controller: _controller, fit: BoxFit.cover, onDetect: _onDetect, ),