main.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:flutter/services.dart';
  4. import 'package:fqreader/fqreader.dart';
  5. import 'package:flustars/flustars.dart';
  6. import 'package:cool_ui/cool_ui.dart';
  7. import 'package:image_picker/image_picker.dart';
  8. void main() => runApp(new MyApp());
  9. class MyApp extends StatefulWidget {
  10. @override
  11. _MyAppState createState() => new _MyAppState();
  12. }
  13. class _MyAppState extends State<MyApp> {
  14. GlobalKey<ScanViewState> scanView;
  15. @override
  16. void initState() {
  17. super.initState();
  18. scanView = GlobalKey<ScanViewState>();
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. return new MaterialApp(
  23. home: Builder(builder: (context) {
  24. ScreenUtil.getInstance().init(context);
  25. double bodyHeight = (ScreenUtil.screenHeight - ScreenUtil.appBarHeight);
  26. Rect viewRect = Rect.fromLTRB(
  27. 0, ScreenUtil.appBarHeight, ScreenUtil.screenWidth, ScreenUtil.screenHeight);
  28. Rect scanRect = Rect.fromLTWH(
  29. ScreenUtil.screenWidth * 0.1,
  30. (bodyHeight - bodyHeight * 0.8) / 2 + 60,
  31. ScreenUtil.screenWidth * 0.8,
  32. ScreenUtil.screenWidth * 0.8);
  33. return new Scaffold(
  34. appBar: new AppBar(
  35. title: const Text('Plugin example app'),
  36. ),
  37. body: Stack(
  38. children: <Widget>[
  39. ScanView(
  40. key: scanView,
  41. continuityScan: true,
  42. onScan: (value) async {
  43. showWeuiSuccessToast(
  44. context: context, message: Text("扫描成功:" + value),closeDuration:Duration(milliseconds: 500));
  45. return true;
  46. },
  47. viewRect: viewRect,
  48. scanRect: scanRect),
  49. Positioned(
  50. top: 0.0,
  51. left: 0.0,
  52. child: FlatButton(
  53. child: Text("启动扫描"),
  54. color: Colors.red,
  55. onPressed: () => scanView.currentState.startScan(),
  56. ),
  57. ),
  58. Positioned(
  59. top: 0.0,
  60. left: 80.0,
  61. child: FlatButton(
  62. child: Text("暂停扫描"),
  63. color: Colors.red,
  64. onPressed: () => scanView.currentState.stopScan(),
  65. ),
  66. ),
  67. Positioned(
  68. top: 0.0,
  69. left: 160.0,
  70. child: FlatButton(
  71. child: Text("开灯"),
  72. color: Colors.red,
  73. onPressed: () => scanView.currentState.turnOn(),
  74. ),
  75. ),
  76. Positioned(
  77. top: 0.0,
  78. left: 240.0,
  79. child: FlatButton(
  80. child: Text("关灯"),
  81. color: Colors.red,
  82. onPressed: () => scanView.currentState.turnOff(),
  83. ),
  84. ),
  85. Positioned(
  86. top: 60.0,
  87. left: 0.0,
  88. child: FlatButton(
  89. child: Text("扫描图片"),
  90. color: Colors.red,
  91. onPressed: () async {
  92. var image = await ImagePicker.pickImage(source: ImageSource.camera);
  93. var value = await Fqreader.decodeImg(image, [ScanType.ALL]);
  94. if(value == null){
  95. showWeuiSuccessToast(
  96. context: context, message: Text("未扫描到数据"),closeDuration:Duration(milliseconds: 3000));
  97. }else{
  98. showWeuiSuccessToast(
  99. context: context, message: Text("扫描成功:" + value),closeDuration:Duration(milliseconds: 500));
  100. }
  101. },
  102. ),
  103. ),
  104. Positioned(
  105. top: scanRect.top - 60,
  106. left: scanRect.left,
  107. child: Container(
  108. width: scanRect.width,
  109. height: scanRect.height,
  110. decoration: BoxDecoration(border: Border.all()),
  111. ),
  112. )
  113. ],
  114. ),
  115. );
  116. }),
  117. );
  118. }
  119. }