import 'dart:io'; import 'package:flutter/material.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart'; void main() => runApp(QRScanPage()); // リスト 6.9_cf class QRScanPage extends StatefulWidget { @override State<StatefulWidget> createState() => _QRScanPageState(); } class _QRScanPageState extends State<QRScanPage> { final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); String? scannedUrl; QRViewController? controller; @override void reassemble() { super.reassemble(); if (Platform.isAndroid) { controller?.pauseCamera(); } else if (Platform.isIOS) { controller?.resumeCamera(); } } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( backgroundColor: Colors.white, body: Column( children: <Widget>[ Expanded( flex: 5, child: QRView( key: qrKey, onQRViewCreated: _onQRViewCreated, ) ), Expanded( flex: 1, child: Center( child: scannedUrl == null ? CircularProgressIndicator() : Text(scannedUrl!) ) ) ] ) ) ); } void _onQRViewCreated(QRViewController controller) { this.controller = controller; controller.scannedDataStream.listen((scanData) { setState(() { scannedUrl = scanData.code; }); }); } @override void dispose() { controller?.dispose(); super.dispose(); } }