123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- import 'package:flutter/material.dart';
- import 'package:permission_handler/permission_handler.dart';
- import 'package:r_scan/r_scan.dart';
- class RScanDialog extends StatefulWidget {
- @override
- _RScanDialogState createState() => _RScanDialogState();
- }
- class _RScanDialogState extends State<RScanDialog> {
- RScanController _controller;
- @override
- void initState() {
- super.initState();
- initController();
- }
- bool isFirst = true;
- Future<void> initController() async {
- _controller = RScanController();
- _controller.addListener(() {
- final result = _controller.result;
- if (result != null) {
- if (isFirst) {
- Navigator.of(context).pop(result);
- isFirst = false;
- }
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: Colors.black,
- body: FutureBuilder<bool>(
- future: canOpenCameraView(),
- builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
- if (snapshot.hasData && snapshot.data == true) {
- return Stack(
- children: <Widget>[
- ScanImageView(
- child: RScanView(
- controller: _controller,
- ),
- ),
- Align(
- alignment: Alignment.bottomCenter,
- child: FutureBuilder(
- future: getFlashMode(),
- builder: _buildFlashBtn,
- ))
- ],
- );
- } else {
- return Container();
- }
- },
- ),
- );
- }
- Future<bool> getFlashMode() async {
- bool isOpen = false;
- try {
- isOpen = await _controller.getFlashMode();
- } catch (_) {}
- return isOpen;
- }
- Future<bool> canOpenCameraView() async {
- var status =
- await PermissionHandler().checkPermissionStatus(PermissionGroup.camera);
- if (status != PermissionStatus.granted) {
- var future = await PermissionHandler()
- .requestPermissions([PermissionGroup.camera]);
- for (final item in future.entries) {
- if (item.value != PermissionStatus.granted) {
- return false;
- }
- }
- } else {
- return true;
- }
- return true;
- }
- Widget _buildFlashBtn(BuildContext context, AsyncSnapshot<bool> snapshot) {
- return snapshot.hasData
- ? Padding(
- padding: EdgeInsets.only(
- bottom: 24 + MediaQuery.of(context).padding.bottom),
- child: IconButton(
- icon: Icon(snapshot.data ? Icons.flash_on : Icons.flash_off),
- color: Colors.white,
- iconSize: 46,
- onPressed: () {
- if (snapshot.data) {
- _controller.setFlashMode(false);
- } else {
- _controller.setFlashMode(true);
- }
- setState(() {});
- }),
- )
- : Container();
- }
- }
- class ScanImageView extends StatefulWidget {
- final Widget child;
- const ScanImageView({Key key, this.child}) : super(key: key);
- @override
- _ScanImageViewState createState() => _ScanImageViewState();
- }
- class _ScanImageViewState extends State<ScanImageView>
- with TickerProviderStateMixin {
- AnimationController controller;
- @override
- void initState() {
- super.initState();
- controller = AnimationController(
- vsync: this, duration: Duration(milliseconds: 1000));
- controller.repeat(reverse: true);
- }
- @override
- void dispose() {
- controller.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return AnimatedBuilder(
- animation: controller,
- builder: (BuildContext context, Widget child) => CustomPaint(
- foregroundPainter:
- _ScanPainter(controller.value, Colors.white, Colors.green),
- child: widget.child,
- willChange: true,
- ));
- }
- }
- class _ScanPainter extends CustomPainter {
- final double value;
- final Color borderColor;
- final Color scanColor;
- _ScanPainter(this.value, this.borderColor, this.scanColor);
- Paint _paint;
- @override
- void paint(Canvas canvas, Size size) {
- if (_paint == null) {
- initPaint();
- }
- double width = size.width;
- double height = size.height;
- double boxWidth = size.width * 2 / 3;
- double boxHeight = height / 4;
- double left = (width - boxWidth) / 2;
- double top = boxHeight;
- double bottom = boxHeight * 2;
- double right = left + boxWidth;
- _paint.color = borderColor;
- final rect = Rect.fromLTWH(left, top, boxWidth, boxHeight);
- canvas.drawRect(rect, _paint);
- _paint.strokeWidth = 3;
- Path path1 = Path()
- ..moveTo(left, top + 10)
- ..lineTo(left, top)
- ..lineTo(left + 10, top);
- canvas.drawPath(path1, _paint);
- Path path2 = Path()
- ..moveTo(left, bottom - 10)
- ..lineTo(left, bottom)
- ..lineTo(left + 10, bottom);
- canvas.drawPath(path2, _paint);
- Path path3 = Path()
- ..moveTo(right, bottom - 10)
- ..lineTo(right, bottom)
- ..lineTo(right - 10, bottom);
- canvas.drawPath(path3, _paint);
- Path path4 = Path()
- ..moveTo(right, top + 10)
- ..lineTo(right, top)
- ..lineTo(right - 10, top);
- canvas.drawPath(path4, _paint);
- _paint.color = scanColor;
- final scanRect = Rect.fromLTWH(
- left + 10, top + 10 + (value * (boxHeight - 20)), boxWidth - 20, 3);
- _paint.shader = LinearGradient(colors: <Color>[
- Colors.white54,
- Colors.white,
- Colors.white54,
- ], stops: [
- 0.0,
- 0.5,
- 1,
- ]).createShader(scanRect);
- canvas.drawRect(scanRect, _paint);
- }
- @override
- bool shouldRepaint(CustomPainter oldDelegate) {
- return true;
- }
- void initPaint() {
- _paint = Paint()
- ..style = PaintingStyle.stroke
- ..strokeWidth = 1
- ..isAntiAlias = true
- ..strokeCap = StrokeCap.round
- ..strokeJoin = StrokeJoin.round;
- }
- }
|