import 'package:barcode_scan/barcode_scan.dart'; import 'package:camera/camera.dart'; import 'package:farm_tpf/presentation/screens/plot_detail/bloc/plot_detail_bloc.dart'; import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_detail.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:get/route_manager.dart'; import 'package:rflutter_alert/rflutter_alert.dart'; import 'app.dart'; import 'custom_model/CropPlot.dart'; import 'data/repository/authentication_repository.dart'; import 'data/repository/repository.dart'; List cameras = []; Future main() async { // Fetch the available cameras before initializing the app. try { WidgetsFlutterBinding.ensureInitialized(); cameras = await availableCameras(); } on CameraException catch (e) { print(e.description); } runApp(App(authenticationRepository: AuthenticationRepository())); } Future myBackgroundMessageHandler(Map message) { if (message.containsKey('data')) { // Handle data message final dynamic data = message['data']; } if (message.containsKey('notification')) { // Handle notification message final dynamic notification = message['notification']; } // Or do other work. } Future scan(BuildContext context) async { var _aspectTolerance = 0.00; var _selectedCamera = -1; var _useAutoFocus = true; var _autoEnableFlash = false; try { var options = ScanOptions( strings: { "cancel": "Huỷ", "flash_on": "Bật flash", "flash_off": "Tắt flash", }, useCamera: _selectedCamera, autoEnableFlash: _autoEnableFlash, android: AndroidOptions( aspectTolerance: _aspectTolerance, useAutoFocus: _useAutoFocus, ), ); var result = await BarcodeScanner.scan(options: options); print(result.toString()); if (result.type == ResultType.Cancelled) { print("canncel"); } else if (result.type == ResultType.Error) { print("error"); } else { _showAlertCheckCropCode(context, result.rawContent); } } on PlatformException catch (e) { var result = ScanResult( type: ResultType.Error, format: BarcodeFormat.unknown, ); print("error: ${e.message}"); } } _showAlertCheckCropCode(BuildContext context, String cropCode) { Alert( context: context, title: "", style: AlertStyle(isCloseButton: false, isOverlayTapDismiss: false), content: BlocProvider( create: (context) => PlotDetailBloc(repository: Repository()) ..add(DataFetched(cropCode: cropCode)), child: BlocBuilder( builder: (context, state) { if (state is PlotDetailInitial) { return Center( child: Text("PlotDetailInitial"), ); } else if (state is PlotDetailFailure) { return Center( child: Column( children: [ Text("Lô không tồn tại"), SizedBox( height: 16, ), DialogButton( onPressed: () { Get.back(); scan(context); }, child: Text( "Quét lại", style: TextStyle(color: Colors.white, fontSize: 20), ), ) ], ), ); } else if (state is PlotDetailSuccess) { var result = state.ownerItem as CropPlot; WidgetsBinding.instance.addPostFrameCallback((_) { Get.back(); Get.to(PlotDetailScreen( cropId: result.tbCropDTO.id, initialIndex: 1, )); }); return Center(child: Text("")); } else { return Center( child: Column( children: [ CircularProgressIndicator(), SizedBox( height: 8, ), Text("Kiểm tra thông tin lô ....") ], ), ); } })), buttons: []).show(); }