zbar_view.dart 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. part of fqreader;
  2. class ZBarView extends StatefulWidget {
  3. final int width;
  4. final int height;
  5. final Rect? scanRect;
  6. final ScanEvent onScan;
  7. const ZBarView({Key? key,required this.width,required this.height, this.scanRect,required this.onScan})
  8. : super(key: key);
  9. @override
  10. State<StatefulWidget> createState() {
  11. // TODO: implement createState
  12. return ZBarViewState();
  13. }
  14. }
  15. class ZBarViewState extends State<ZBarView> {
  16. int? _viewID;
  17. MethodChannel? _channel;
  18. @override
  19. Widget build(BuildContext context) {
  20. var ratio = MediaQuery.of(context).devicePixelRatio;
  21. // TODO: implement build
  22. if (defaultTargetPlatform == TargetPlatform.android) {
  23. return AndroidView(
  24. viewType: 'info.geteasy.fqreader_view',
  25. creationParams: {
  26. 'width': 1080,
  27. 'height': 1920,
  28. 'scanLeft': (widget.scanRect?.left.toInt() ?? 0 * ratio).toInt(),
  29. 'scanTop': (widget.scanRect?.top.toInt() ?? 0 * ratio).toInt(),
  30. 'scanBottom': (widget.scanRect?.bottom.toInt() ?? widget.height * ratio).toInt(),
  31. 'scanRight': (widget.scanRect?.right.toInt() ?? widget.width * ratio).toInt(),
  32. },
  33. creationParamsCodec: const StandardMessageCodec(),
  34. onPlatformViewCreated: _onPlatformViewCreated,
  35. gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
  36. new Factory<OneSequenceGestureRecognizer>(
  37. () => new EagerGestureRecognizer()),
  38. ].toSet(),
  39. );
  40. } else {
  41. return Container();
  42. }
  43. }
  44. void _onPlatformViewCreated(int id) {
  45. _viewID = id;
  46. _channel = MethodChannel('info.geteasy.fqreader_view.event$_viewID');
  47. _channel!.setMethodCallHandler(_handleMessages);
  48. this.startCamera();
  49. }
  50. Future _handleMessages(MethodCall call) async {
  51. switch (call.method) {
  52. case "onScan":
  53. String data = call.arguments['data'];
  54. widget.onScan(ScanResult(data: data)).then((value) {
  55. if (value) {
  56. this.startCamera();
  57. }
  58. });
  59. break;
  60. }
  61. }
  62. startCamera() async {
  63. return await _channel!.invokeMethod("startCamera");
  64. }
  65. stopCamera() async {
  66. return await _channel!.invokeMethod("stopCamera");
  67. }
  68. setFlash() async {
  69. return await _channel!.invokeMethod("setFlash");
  70. }
  71. Future<bool> getFlash() async {
  72. bool result = await _channel!.invokeMethod("getFlash");
  73. return result;
  74. }
  75. }