You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.4KB

  1. import 'package:barcode_scan/barcode_scan.dart';
  2. import 'package:camera/camera.dart';
  3. import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_detail.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:get/route_manager.dart';
  7. import 'app.dart';
  8. import 'data/repository/authentication_repository.dart';
  9. import 'data/repository/repository.dart';
  10. import 'presentation/custom_widgets/widget_utils.dart';
  11. List<CameraDescription> cameras = [];
  12. Future<void> main() async {
  13. // Fetch the available cameras before initializing the app.
  14. try {
  15. WidgetsFlutterBinding.ensureInitialized();
  16. cameras = await availableCameras();
  17. } on CameraException catch (e) {
  18. print(e.description);
  19. }
  20. runApp(App(authenticationRepository: AuthenticationRepository()));
  21. }
  22. Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
  23. if (message.containsKey('data')) {
  24. // Handle data message
  25. final dynamic data = message['data'];
  26. }
  27. if (message.containsKey('notification')) {
  28. // Handle notification message
  29. final dynamic notification = message['notification'];
  30. }
  31. // Or do other work.
  32. }
  33. Future scan(BuildContext context) async {
  34. var _aspectTolerance = 0.00;
  35. var _selectedCamera = -1;
  36. var _useAutoFocus = true;
  37. var _autoEnableFlash = false;
  38. var repository = Repository();
  39. try {
  40. var options = ScanOptions(
  41. strings: {
  42. "cancel": "Huỷ",
  43. "flash_on": "Bật flash",
  44. "flash_off": "Tắt flash",
  45. },
  46. useCamera: _selectedCamera,
  47. autoEnableFlash: _autoEnableFlash,
  48. android: AndroidOptions(
  49. aspectTolerance: _aspectTolerance,
  50. useAutoFocus: _useAutoFocus,
  51. ),
  52. );
  53. var result = await BarcodeScanner.scan(options: options);
  54. print(result.toString());
  55. if (result.type == ResultType.Cancelled) {
  56. print("canncel");
  57. } else if (result.type == ResultType.Error) {
  58. print("error");
  59. } else {
  60. print("show check crop");
  61. _showAlertCheckCropCode(context, result.rawContent, repository);
  62. }
  63. } on PlatformException catch (e) {
  64. print("error: ${e.message}");
  65. }
  66. }
  67. _showAlertCheckCropCode(
  68. BuildContext context, String cropCode, Repository repository) async {
  69. Get.defaultDialog(
  70. title: "Kiểm tra thông tin lô ....",
  71. middleText: "",
  72. content: CircularProgressIndicator());
  73. try {
  74. await repository
  75. .getPlotDetailByCode(cropCode, page: 1, size: 1)
  76. .then((value) {
  77. print("ok");
  78. if (Get.isDialogOpen) Get.back();
  79. Get.to(PlotDetailScreen(
  80. cropId: value.tbCropDTO.id,
  81. cropType: value.tbCropDTO.type,
  82. initialIndex: 1,
  83. code: value.tbCropDTO.code,
  84. areaM2: value.tbCropDTO.areaM2,
  85. suppliesName: value.tbCropDTO.suppliesName,
  86. ));
  87. }).catchError((onError) {
  88. Utils.showDialog(
  89. title: "Không tìm thấy lô",
  90. message: "Thử lại với mã tem khác?",
  91. textConfirm: "Thử lại",
  92. textCancel: "Huỷ",
  93. onConfirm: () {
  94. Get.back();
  95. scan(context);
  96. });
  97. });
  98. } catch (e) {
  99. Utils.showDialog(
  100. title: "Không tìm thấy lô",
  101. message: "Thử lại với mã tem khác?",
  102. textConfirm: "Thử lại",
  103. textCancel: "Huỷ",
  104. onConfirm: () {
  105. Get.back();
  106. scan(context);
  107. });
  108. }
  109. }