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.

103 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. void 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(BuildContext context, String cropCode, Repository repository) async {
  68. Get.defaultDialog(title: "Kiểm tra thông tin lô ....", middleText: "", content: const CircularProgressIndicator());
  69. try {
  70. await repository.getPlotDetailByCode(cropCode, page: 1, size: 1).then((value) {
  71. print("ok");
  72. if (Get.isDialogOpen) Get.back();
  73. Get.to(PlotDetailScreen(cropId: value.tbCropDTO?.id ?? -1, cropType: value.tbCropDTO?.type ?? -1, initialIndex: 0));
  74. }).catchError((onError) {
  75. Utils.showDialog(
  76. title: "Không tìm thấy lô",
  77. message: "Thử lại với mã tem khác?",
  78. textConfirm: "Thử lại",
  79. textCancel: "Huỷ",
  80. onConfirm: () {
  81. Get.back();
  82. scan(context);
  83. });
  84. });
  85. } catch (e) {
  86. Utils.showDialog(
  87. title: "Không tìm thấy lô",
  88. message: "Thử lại với mã tem khác?",
  89. textConfirm: "Thử lại",
  90. textCancel: "Huỷ",
  91. onConfirm: () {
  92. Get.back();
  93. scan(context);
  94. });
  95. }
  96. }