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.

112 lines
3.2KB

  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: 0));
  83. }).catchError((onError) {
  84. Utils.showDialog(
  85. title: "Không tìm thấy lô",
  86. message: "Thử lại với mã tem khác?",
  87. textConfirm: "Thử lại",
  88. textCancel: "Huỷ",
  89. onConfirm: () {
  90. Get.back();
  91. scan(context);
  92. });
  93. });
  94. } catch (e) {
  95. Utils.showDialog(
  96. title: "Không tìm thấy lô",
  97. message: "Thử lại với mã tem khác?",
  98. textConfirm: "Thử lại",
  99. textCancel: "Huỷ",
  100. onConfirm: () {
  101. Get.back();
  102. scan(context);
  103. });
  104. }
  105. }