前提
Flutterでqr_code_sccanerというQR読み取りライブラリを使用したいです。
状態管理をRiverpodで管理しており、バーコードを読み取って値を渡す方法が分からないです。
以下にサンプルコードが載っています
https://pub.dev/packages/qr_code_scanner/example
発生している問題
Statefulで記載すると
Dart
1void _onQRViewCreated(QRViewController controller) { 2 setState(() { 3 this.controller = controller; 4 }); 5 controller.scannedDataStream.listen((scanData) { 6 setState(() { 7 result = scanData; 8 }); 9 }); 10 }
ここの setState() をRiverpodではどのように書き換えするのかご教授いただきたいです。
該当のコード
Dart
1 2class QRScan extends ConsumerWidget { 3 Barcode? result; 4 QRViewController? controller; 5 final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); 6 7 8 Widget build(BuildContext context, WidgetRef ref) { 9 return Scaffold( 10 body: Column( 11 children: <Widget>[ 12 Expanded(flex: 3, child: _buildQrView(context)), 13 Expanded( 14 flex: 1, 15 child: FittedBox( 16 fit: BoxFit.contain, 17 child: Column( 18 children: <Widget>[ 19 if (result != null) 20 Text('${result!.code}') 21 else 22 Text('scan'), 23 Row( 24 children: <Widget>[ 25 IconButton( 26 onPressed: () { 27 Navigator.of(context).pop(); 28 }, 29 icon: Icon(Icons.radio_button_checked), 30 iconSize: 80, 31 ), 32 TextButton(onPressed: () { 33 Navigator.of(context).pop(); 34 }, child: Text('終了')) 35 ], 36 ), 37 ], 38 ), 39 )) 40 ], 41 ), 42 ); 43 } 44 45// カメラ枠 46 Widget _buildQrView(BuildContext context) { 47 var scanArea = (MediaQuery.of(context).size.width < 400 || 48 MediaQuery.of(context).size.height < 400) 49 ? 150.0 50 : 300.0; 51 52 return QRView( 53 key: qrKey, 54 onQRViewCreated: _onQRViewCreated, //_onQRViewCreated, 55 overlay: QrScannerOverlayShape( 56 borderColor: Colors.red, 57 borderRadius: 10, 58 borderLength: 30, 59 borderWidth: 10, 60 cutOutSize: scanArea), 61 ); 62 } 63 64 void _onQRViewCreated(QRViewController controller) { 65 // setState(() { 66 // this.controller = controller; 67 // }); 68 69 controller.scannedDataStream.listen((scanData) { 70 // setState(() { 71 // result = scanData; 72 // }); 73 74 result = scanData; 75 }); 76 } 77}
初心者ですがよろしくお願いします。
あなたの回答
tips
プレビュー