Переглянути джерело

Update example REAMD.md code to reflect api changes

The example is copied verbatim from ./example/lib/main.dart
flutter-beta
operator.name 5 роки тому
committed by GitHub
джерело
коміт
fed7f7e345
Не вдалося знайти GPG ключ що відповідає даному підпису Ідентифікатор GPG ключа: 4AEE18F83AFDEB23
1 змінених файлів з 117 додано та 29 видалено
  1. +117
    -29
      example/README.md

+ 117
- 29
example/README.md Переглянути файл

@@ -22,11 +22,15 @@ Demonstrates how to use the qr_code_scanner plugin.
## Example: ## Example:
```dart ```dart
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart';


void main() => runApp(MaterialApp(home: QRViewExample())); void main() => runApp(MaterialApp(home: QRViewExample()));


const flashOn = 'FLASH ON';
const flashOff = 'FLASH OFF';
const frontCamera = 'FRONT CAMERA';
const backCamera = 'BACK CAMERA';

class QRViewExample extends StatefulWidget { class QRViewExample extends StatefulWidget {
const QRViewExample({ const QRViewExample({
Key key, Key key,
@@ -37,60 +41,144 @@ class QRViewExample extends StatefulWidget {
} }


class _QRViewExampleState extends State<QRViewExample> { class _QRViewExampleState extends State<QRViewExample> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
var qrText = "";
var qrText = '';
var flashState = flashOn;
var cameraState = frontCamera;
QRViewController controller; QRViewController controller;
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');

@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
body: Column( body: Column(
children: <Widget>[ children: <Widget>[
Expanded( Expanded(
flex: 4,
child: QRView( child: QRView(
key: qrKey, key: qrKey,
onQRViewCreated: _onQRViewCreated, onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Colors.red,
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: 300,
),
), ),
flex: 4,
), ),
Expanded( Expanded(
child: Column(children:
<Widget>[
Text("This is the result of scan: $qrText"),
RaisedButton(
onPressed: (){
if(controller != null){
controller.flipCamera();
}
},
child: Text(
'Flip',
style: TextStyle(fontSize: 20)
flex: 1,
child: FittedBox(
fit: BoxFit.contain,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text('This is the result of scan: $qrText'),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.all(8),
child: RaisedButton(
onPressed: () {
if (controller != null) {
controller.toggleFlash();
if (_isFlashOn(flashState)) {
setState(() {
flashState = flashOff;
});
} else {
setState(() {
flashState = flashOn;
});
}
}
},
child:
Text(flashState, style: TextStyle(fontSize: 20)),
),
),
Container(
margin: EdgeInsets.all(8),
child: RaisedButton(
onPressed: () {
if (controller != null) {
controller.flipCamera();
if (_isBackCamera(cameraState)) {
setState(() {
cameraState = frontCamera;
});
} else {
setState(() {
cameraState = backCamera;
});
}
}
},
child:
Text(cameraState, style: TextStyle(fontSize: 20)),
),
)
],
), ),
)
],
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.all(8),
child: RaisedButton(
onPressed: () {
controller?.pauseCamera();
},
child: Text('pause', style: TextStyle(fontSize: 20)),
),
),
Container(
margin: EdgeInsets.all(8),
child: RaisedButton(
onPressed: () {
controller?.resumeCamera();
},
child: Text('resume', style: TextStyle(fontSize: 20)),
),
)
],
),
],
),
), ),
flex: 1,
) )
], ],
), ),
); );
} }


bool _isFlashOn(String current) {
return flashOn == current;
}

bool _isBackCamera(String current) {
return backCamera == current;
}

void _onQRViewCreated(QRViewController controller) { void _onQRViewCreated(QRViewController controller) {
final channel = controller.channel;
controller.init(qrKey);
this.controller = controller; this.controller = controller;
channel.setMethodCallHandler((MethodCall call) async {
switch (call.method) {
case "onRecognizeQR":
dynamic arguments = call.arguments;
setState(() {
qrText = arguments.toString();
});
}
controller.scannedDataStream.listen((scanData) {
setState(() {
qrText = scanData;
});
}); });
} }

@override
void dispose() {
controller.dispose();
super.dispose();
}
} }

``` ```






Завантаження…
Відмінити
Зберегти