From 34fd0a299d54614c6f4e0e3e7ea946e0481e17fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=B4=BA=E5=A4=A9?= Date: Sat, 8 Jun 2019 20:21:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=AB=E7=A0=81=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++ README.md | 5 +- example/pubspec.lock | 2 +- lib/qrcode_reader_view.dart | 107 ++++++++++++++++++++++-------------- pubspec.yaml | 2 +- 5 files changed, 76 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf92a8e..81024de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,3 +14,7 @@ ## 1.0.0+1 +## 1.0.1 + +修改扫码后的操作 + diff --git a/README.md b/README.md index 7c1cbb2..a4942b6 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,12 @@ QrReaderView( ``` dart Widget build(BuildContext context) { return new Scaffold( - body: QrcodeReaderView(onScan: onScan), + body: QrcodeReaderView(key: qrViewKey, onScan: onScan), ); } +GlobalKey qrViewKey = GlobalKey(); + Future onScan(String data) async { await showCupertinoDialog( context: context, @@ -56,5 +58,6 @@ Future onScan(String data) async { ); }, ); + qrViewKey.currentState.startScan(); } ``` \ No newline at end of file diff --git a/example/pubspec.lock b/example/pubspec.lock index 4cdca58..0fc36c4 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -47,7 +47,7 @@ packages: path: ".." relative: true source: path - version: "1.0.0+1" + version: "1.0.1" flutter_test: dependency: "direct dev" description: flutter diff --git a/lib/qrcode_reader_view.dart b/lib/qrcode_reader_view.dart index bfec753..2166c43 100644 --- a/lib/qrcode_reader_view.dart +++ b/lib/qrcode_reader_view.dart @@ -13,19 +13,25 @@ class QrcodeReaderView extends StatefulWidget { final Color boxLineColor; final Widget helpWidget; QrcodeReaderView({ - Key key, - @required this.onScan, - this.headerWidget, - this.boxLineColor = Colors.cyanAccent, - this.helpWidget, - this.scanBoxRatio = 0.85, - }) : super(key: key); + Key key, + @required this.onScan, + this.headerWidget, + this.boxLineColor = Colors.cyanAccent, + this.helpWidget, + this.scanBoxRatio = 0.85, + }) : super(key: key); @override - _QrcodeReaderViewState createState() => new _QrcodeReaderViewState(); + QrcodeReaderViewState createState() => new QrcodeReaderViewState(); } -class _QrcodeReaderViewState extends State with TickerProviderStateMixin { +/// 扫码后的后续操作 +/// ```dart +/// GlobalKey qrViewKey = GlobalKey(); +/// qrViewKey.currentState.startScan(); +/// ``` +class QrcodeReaderViewState extends State + with TickerProviderStateMixin { QrReaderViewController _controller; AnimationController _animationController; bool openFlashlight; @@ -39,17 +45,18 @@ class _QrcodeReaderViewState extends State with TickerProvider void _initAnimation() { setState(() { - _animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 1000)); + _animationController = AnimationController( + vsync: this, duration: Duration(milliseconds: 1000)); }); _animationController - ..addListener(upState) + ..addListener(_upState) ..addStatusListener((state) { if (state == AnimationStatus.completed) { _timer = Timer(Duration(seconds: 1), () { _animationController?.reverse(from: 1.0); }); } else if (state == AnimationStatus.dismissed) { - _timer =Timer(Duration(seconds: 1), () { + _timer = Timer(Duration(seconds: 1), () { _animationController?.forward(from: 0.0); }); } @@ -59,31 +66,32 @@ class _QrcodeReaderViewState extends State with TickerProvider void _clearAnimation() { _timer?.cancel(); - _animationController?.dispose(); - _animationController = null; + if (_animationController != null) { + _animationController?.dispose(); + _animationController = null; + } } - void upState() { + void _upState() { setState(() {}); } - void onCreateController(QrReaderViewController controller) async { + void _onCreateController(QrReaderViewController controller) async { _controller = controller; - _controller.startCamera(onQrBack); + _controller.startCamera(_onQrBack); } bool isScan = false; - Future onQrBack(data, _) async { - if (isScan == true) return ; + Future _onQrBack(data, _) async { + if (isScan == true) return; isScan = true; stopScan(); await widget.onScan(data); - startScan(); - isScan = false; } void startScan() { - _controller.startCamera(onQrBack); + isScan = false; + _controller.startCamera(_onQrBack); _initAnimation(); } @@ -92,7 +100,13 @@ class _QrcodeReaderViewState extends State with TickerProvider _controller.stopCamera(); } - Future scanImage() async { + Future setFlashlight() async { + openFlashlight = await _controller.setFlashlight(); + setState(() {}); + return openFlashlight; + } + + Future _scanImage() async { stopScan(); var image = await ImagePicker.pickImage(source: ImageSource.gallery); if (image == null) { @@ -136,7 +150,7 @@ class _QrcodeReaderViewState extends State with TickerProvider child: QrReaderView( width: constraints.maxWidth, height: constraints.maxHeight, - callback: onCreateController, + callback: _onCreateController, ), ), if (widget.headerWidget != null) widget.headerWidget, @@ -147,7 +161,8 @@ class _QrcodeReaderViewState extends State with TickerProvider painter: QrScanBoxPainter( boxLineColor: widget.boxLineColor, animationValue: _animationController?.value ?? 0, - isForward: _animationController?.status == AnimationStatus.forward, + isForward: + _animationController?.status == AnimationStatus.forward, ), child: SizedBox( width: qrScanSize, @@ -156,7 +171,9 @@ class _QrcodeReaderViewState extends State with TickerProvider ), ), Positioned( - top: (constraints.maxHeight - qrScanSize) * 0.333333 + qrScanSize + 24, + top: (constraints.maxHeight - qrScanSize) * 0.333333 + + qrScanSize + + 24, width: constraints.maxWidth, child: Align( alignment: Alignment.center, @@ -167,30 +184,32 @@ class _QrcodeReaderViewState extends State with TickerProvider ), ), Positioned( - top: (constraints.maxHeight - qrScanSize) * 0.333333 + qrScanSize - 12 - 35, + top: (constraints.maxHeight - qrScanSize) * 0.333333 + + qrScanSize - + 12 - + 35, width: constraints.maxWidth, child: Align( alignment: Alignment.center, child: GestureDetector( behavior: HitTestBehavior.translucent, - onTap: () async { - openFlashlight = await _controller.setFlashlight(); - setState(() {}); - }, + onTap: setFlashlight, child: openFlashlight ? flashOpen : flashClose, ), ), ), Positioned( width: constraints.maxWidth, - bottom: constraints.maxHeight == mediaQuery.size.height ? 12 + mediaQuery.padding.top : 12, + bottom: constraints.maxHeight == mediaQuery.size.height + ? 12 + mediaQuery.padding.top + : 12, child: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ GestureDetector( behavior: HitTestBehavior.translucent, - onTap: scanImage, + onTap: _scanImage, child: Container( width: 45, height: 45, @@ -237,15 +256,17 @@ class _QrcodeReaderViewState extends State with TickerProvider } } - class QrScanBoxPainter extends CustomPainter { final double animationValue; final bool isForward; final Color boxLineColor; - QrScanBoxPainter({@required this.animationValue, @required this.isForward, this.boxLineColor}) - : assert(animationValue != null), - assert(isForward != null); + QrScanBoxPainter( + {@required this.animationValue, + @required this.isForward, + this.boxLineColor}) + : assert(animationValue != null), + assert(isForward != null); @override void paint(Canvas canvas, Size size) { @@ -277,7 +298,8 @@ class QrScanBoxPainter extends CustomPainter { // rightBottom path.moveTo(size.width, size.height - 50); path.lineTo(size.width, size.height - 12); - path.quadraticBezierTo(size.width, size.height, size.width - 12, size.height); + path.quadraticBezierTo( + size.width, size.height, size.width - 12, size.height); path.lineTo(size.width - 50, size.height); // leftBottom path.moveTo(50, size.height); @@ -287,7 +309,8 @@ class QrScanBoxPainter extends CustomPainter { canvas.drawPath(path, borderPaint); - canvas.clipRRect(BorderRadius.all(Radius.circular(12)).toRRect(Offset.zero & size)); + canvas.clipRRect( + BorderRadius.all(Radius.circular(12)).toRRect(Offset.zero & size)); // 绘制横向网格 final linePaint = Paint(); @@ -322,8 +345,10 @@ class QrScanBoxPainter extends CustomPainter { } @override - bool shouldRepaint(QrScanBoxPainter oldDelegate) => animationValue != oldDelegate.animationValue; + bool shouldRepaint(QrScanBoxPainter oldDelegate) => + animationValue != oldDelegate.animationValue; @override - bool shouldRebuildSemantics(QrScanBoxPainter oldDelegate) => animationValue != oldDelegate.animationValue; + bool shouldRebuildSemantics(QrScanBoxPainter oldDelegate) => + animationValue != oldDelegate.animationValue; } diff --git a/pubspec.yaml b/pubspec.yaml index 0a55b97..6009435 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_qr_reader description: QR code (scan QRCode and picture) recognition (AndroidView/UiKitView) -version: 1.0.0+1 +version: 1.0.1 author: 王贺天 homepage: https://github.com/hetian9288/flutter_qr_reader