Skip to content

Commit

Permalink
修改扫码后的操作
Browse files Browse the repository at this point in the history
  • Loading branch information
hetian9288 committed Jun 8, 2019
1 parent adf4fb9 commit 34fd0a2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@

## 1.0.0+1

## 1.0.1

修改扫码后的操作

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ QrReaderView(
``` dart
Widget build(BuildContext context) {
return new Scaffold(
body: QrcodeReaderView(onScan: onScan),
body: QrcodeReaderView(key: qrViewKey, onScan: onScan),
);
}
GlobalKey<QrcodeReaderViewState> qrViewKey = GlobalKey();
Future onScan(String data) async {
await showCupertinoDialog(
context: context,
Expand All @@ -56,5 +58,6 @@ Future onScan(String data) async {
);
},
);
qrViewKey.currentState.startScan();
}
```
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
107 changes: 66 additions & 41 deletions lib/qrcode_reader_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<QrcodeReaderView> with TickerProviderStateMixin {
/// 扫码后的后续操作
/// ```dart
/// GlobalKey<QrcodeReaderViewState> qrViewKey = GlobalKey();
/// qrViewKey.currentState.startScan();
/// ```
class QrcodeReaderViewState extends State<QrcodeReaderView>
with TickerProviderStateMixin {
QrReaderViewController _controller;
AnimationController _animationController;
bool openFlashlight;
Expand All @@ -39,17 +45,18 @@ class _QrcodeReaderViewState extends State<QrcodeReaderView> 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);
});
}
Expand All @@ -59,31 +66,32 @@ class _QrcodeReaderViewState extends State<QrcodeReaderView> 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();
}

Expand All @@ -92,7 +100,13 @@ class _QrcodeReaderViewState extends State<QrcodeReaderView> with TickerProvider
_controller.stopCamera();
}

Future scanImage() async {
Future<bool> setFlashlight() async {
openFlashlight = await _controller.setFlashlight();
setState(() {});
return openFlashlight;
}

Future _scanImage() async {
stopScan();
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
if (image == null) {
Expand Down Expand Up @@ -136,7 +150,7 @@ class _QrcodeReaderViewState extends State<QrcodeReaderView> with TickerProvider
child: QrReaderView(
width: constraints.maxWidth,
height: constraints.maxHeight,
callback: onCreateController,
callback: _onCreateController,
),
),
if (widget.headerWidget != null) widget.headerWidget,
Expand All @@ -147,7 +161,8 @@ class _QrcodeReaderViewState extends State<QrcodeReaderView> 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,
Expand All @@ -156,7 +171,9 @@ class _QrcodeReaderViewState extends State<QrcodeReaderView> 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,
Expand All @@ -167,30 +184,32 @@ class _QrcodeReaderViewState extends State<QrcodeReaderView> 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: <Widget>[
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: scanImage,
onTap: _scanImage,
child: Container(
width: 45,
height: 45,
Expand Down Expand Up @@ -237,15 +256,17 @@ class _QrcodeReaderViewState extends State<QrcodeReaderView> 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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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: 王贺天<[email protected]>
homepage: https://github.com/hetian9288/flutter_qr_reader

Expand Down

0 comments on commit 34fd0a2

Please sign in to comment.