fqreader.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. library fqreader;
  2. import 'dart:async';
  3. import 'dart:ui' as ui;
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. class Fqreader {
  7. static const MethodChannel _channel =
  8. const MethodChannel('fqreader');
  9. static Future<int> initView({@required Rect viewRect,@required Rect scanRect,double devicePixelRatio}) async {
  10. final int textureId = await _channel.invokeMethod('initView',{
  11. "viewRect":{
  12. "left":(viewRect.left * devicePixelRatio).toInt(),
  13. "top":(viewRect.top* devicePixelRatio).toInt(),
  14. "right":(viewRect.right* devicePixelRatio).toInt(),
  15. "bottom":(viewRect.bottom* devicePixelRatio).toInt()
  16. },
  17. "scanRect":{
  18. "left":(scanRect.left* devicePixelRatio).toInt(),
  19. "top":(scanRect.top* devicePixelRatio).toInt(),
  20. "right":(scanRect.right* devicePixelRatio).toInt(),
  21. "bottom":(scanRect.bottom* devicePixelRatio).toInt(),
  22. }
  23. });
  24. return textureId;
  25. }
  26. static Future<String> startScan() async{
  27. await _channel.invokeMethod('startScan');
  28. }
  29. static Future stopScan() async{
  30. await _channel.invokeMethod('stopScan');
  31. }
  32. }
  33. class ScanView extends StatefulWidget{
  34. final ValueChanged<String> onScan;
  35. final Rect scanRect;
  36. final Rect viewRect;
  37. const ScanView({this.onScan,@required this.viewRect,@required this.scanRect});
  38. @override
  39. State<StatefulWidget> createState() =>ScanViewState();
  40. }
  41. class ScanViewState extends State<ScanView>{
  42. int _textureId;
  43. @override
  44. void initState() {
  45. // TODO: implement initState
  46. super.initState();
  47. MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
  48. Fqreader.initView(viewRect: widget.viewRect,scanRect:widget.scanRect,devicePixelRatio:mediaQuery.devicePixelRatio).then((textureId){
  49. setState(() {
  50. _textureId = textureId;
  51. });
  52. new EventChannel('fqreader/qrcodeEvents$_textureId')
  53. .receiveBroadcastStream()
  54. .listen(_listener);
  55. });
  56. }
  57. @override
  58. Widget build(BuildContext context) {
  59. return _textureId != null
  60. ? new Texture(textureId: _textureId)
  61. : new Container();
  62. }
  63. Future<String> startScan() async{
  64. }
  65. void _listener(dynamic value) {
  66. if(widget != null)
  67. widget.onScan(value);
  68. }
  69. }