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.

130 lines
4.1KB

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