main.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. void main() => runApp(new MyApp());
  8. class MyApp extends StatefulWidget {
  9. @override
  10. _MyAppState createState() => new _MyAppState();
  11. }
  12. class _MyAppState extends State<MyApp> {
  13. GlobalKey<ScanViewState> scanView;
  14. @override
  15. void initState() {
  16. super.initState();
  17. scanView = GlobalKey<ScanViewState>();
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. return new MaterialApp(
  22. home: Builder(builder: (context) {
  23. ScreenUtil.getInstance().init(context);
  24. double bodyHeight = (ScreenUtil.screenHeight - ScreenUtil.appBarHeight);
  25. Rect viewRect = Rect.fromLTRB(
  26. 0, ScreenUtil.appBarHeight, ScreenUtil.screenWidth, ScreenUtil.screenHeight);
  27. Rect scanRect = Rect.fromLTWH(
  28. ScreenUtil.screenWidth * 0.1,
  29. (bodyHeight - bodyHeight * 0.8) / 2 + 60,
  30. ScreenUtil.screenWidth * 0.8,
  31. ScreenUtil.screenWidth * 0.8);
  32. return new Scaffold(
  33. appBar: new AppBar(
  34. title: const Text('Plugin example app'),
  35. ),
  36. body: Stack(
  37. children: <Widget>[
  38. ScanView(
  39. key: scanView,
  40. continuityScan: true,
  41. scanType: [
  42. ScanType.QR_CODE,
  43. ScanType.CODABAR,
  44. ScanType.CODE_39,
  45. ScanType.CODE_93,
  46. ScanType.CODE_128,
  47. ScanType.EAN8,
  48. ScanType.EAN13
  49. ],
  50. onScan: (value) async {
  51. showWeuiSuccessToast(
  52. context: context, message: Text("扫描成功:" + value),closeDuration:Duration(milliseconds: 500));
  53. return true;
  54. },
  55. viewRect: viewRect,
  56. scanRect: scanRect),
  57. Positioned(
  58. top: 0.0,
  59. left: 0.0,
  60. child: FlatButton(
  61. child: Text("启动扫描"),
  62. color: Colors.red,
  63. onPressed: () => scanView.currentState.startScan(),
  64. ),
  65. ),
  66. Positioned(
  67. top: 0.0,
  68. left: 80.0,
  69. child: FlatButton(
  70. child: Text("暂停扫描"),
  71. color: Colors.red,
  72. onPressed: () => scanView.currentState.stopScan(),
  73. ),
  74. ),
  75. Positioned(
  76. top: 0.0,
  77. left: 160.0,
  78. child: FlatButton(
  79. child: Text("开灯"),
  80. color: Colors.red,
  81. onPressed: () => scanView.currentState.turnOn(),
  82. ),
  83. ),
  84. Positioned(
  85. top: 0.0,
  86. left: 240.0,
  87. child: FlatButton(
  88. child: Text("关灯"),
  89. color: Colors.red,
  90. onPressed: () => scanView.currentState.turnOff(),
  91. ),
  92. ),
  93. Positioned(
  94. top: scanRect.top - 60,
  95. left: scanRect.left,
  96. child: Container(
  97. width: scanRect.width,
  98. height: scanRect.height,
  99. decoration: BoxDecoration(border: Border.all()),
  100. ),
  101. )
  102. ],
  103. ),
  104. );
  105. }),
  106. );
  107. }
  108. }