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.

139 lines
4.4KB

  1. import 'package:barcode_scan/barcode_scan.dart';
  2. import 'package:camera/camera.dart';
  3. import 'package:farm_tpf/presentation/screens/plot_detail/bloc/plot_detail_bloc.dart';
  4. import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_detail.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:flutter_bloc/flutter_bloc.dart';
  8. import 'package:get/route_manager.dart';
  9. import 'package:rflutter_alert/rflutter_alert.dart';
  10. import 'app.dart';
  11. import 'custom_model/CropPlot.dart';
  12. import 'data/repository/authentication_repository.dart';
  13. import 'data/repository/repository.dart';
  14. List<CameraDescription> cameras = [];
  15. Future<void> main() async {
  16. // Fetch the available cameras before initializing the app.
  17. try {
  18. WidgetsFlutterBinding.ensureInitialized();
  19. cameras = await availableCameras();
  20. } on CameraException catch (e) {
  21. print(e.description);
  22. }
  23. runApp(App(authenticationRepository: AuthenticationRepository()));
  24. }
  25. Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
  26. if (message.containsKey('data')) {
  27. // Handle data message
  28. final dynamic data = message['data'];
  29. }
  30. if (message.containsKey('notification')) {
  31. // Handle notification message
  32. final dynamic notification = message['notification'];
  33. }
  34. // Or do other work.
  35. }
  36. Future scan(BuildContext context) async {
  37. var _aspectTolerance = 0.00;
  38. var _selectedCamera = -1;
  39. var _useAutoFocus = true;
  40. var _autoEnableFlash = false;
  41. try {
  42. var options = ScanOptions(
  43. strings: {
  44. "cancel": "Huỷ",
  45. "flash_on": "Bật flash",
  46. "flash_off": "Tắt flash",
  47. },
  48. useCamera: _selectedCamera,
  49. autoEnableFlash: _autoEnableFlash,
  50. android: AndroidOptions(
  51. aspectTolerance: _aspectTolerance,
  52. useAutoFocus: _useAutoFocus,
  53. ),
  54. );
  55. var result = await BarcodeScanner.scan(options: options);
  56. print(result.toString());
  57. if (result.type == ResultType.Cancelled) {
  58. print("canncel");
  59. } else if (result.type == ResultType.Error) {
  60. print("error");
  61. } else {
  62. _showAlertCheckCropCode(context, result.rawContent);
  63. }
  64. } on PlatformException catch (e) {
  65. var result = ScanResult(
  66. type: ResultType.Error,
  67. format: BarcodeFormat.unknown,
  68. );
  69. print("error: ${e.message}");
  70. }
  71. }
  72. _showAlertCheckCropCode(BuildContext context, String cropCode) {
  73. Alert(
  74. context: context,
  75. title: "",
  76. style: AlertStyle(isCloseButton: false, isOverlayTapDismiss: false),
  77. content: BlocProvider<PlotDetailBloc>(
  78. create: (context) => PlotDetailBloc(repository: Repository())
  79. ..add(DataFetched(cropCode: cropCode)),
  80. child: BlocBuilder<PlotDetailBloc, PlotDetailState>(
  81. builder: (context, state) {
  82. if (state is PlotDetailInitial) {
  83. return Center(
  84. child: Text(""),
  85. );
  86. } else if (state is PlotDetailFailure) {
  87. return Center(
  88. child: Column(
  89. children: [
  90. Text("Lô không tồn tại"),
  91. SizedBox(
  92. height: 16,
  93. ),
  94. DialogButton(
  95. onPressed: () {
  96. Get.back();
  97. scan(context);
  98. },
  99. child: Text(
  100. "Quét lại",
  101. style: TextStyle(color: Colors.white, fontSize: 20),
  102. ),
  103. )
  104. ],
  105. ),
  106. );
  107. } else if (state is PlotDetailSuccess) {
  108. var result = state.ownerItem as CropPlot;
  109. WidgetsBinding.instance.addPostFrameCallback((_) {
  110. Get.back();
  111. Get.to(PlotDetailScreen(
  112. cropId: result.tbCropDTO.id,
  113. initialIndex: 1,
  114. ));
  115. });
  116. return Center(child: Text(""));
  117. } else {
  118. return Center(
  119. child: Column(
  120. children: [
  121. CircularProgressIndicator(),
  122. SizedBox(
  123. height: 8,
  124. ),
  125. Text("Kiểm tra thông tin lô ....")
  126. ],
  127. ),
  128. );
  129. }
  130. })),
  131. buttons: []).show();
  132. }