Skip to content

Commit

Permalink
migrated qr_code_scanner to mobile_scanner package
Browse files Browse the repository at this point in the history
  • Loading branch information
MukalDadhwal committed Jan 24, 2025
1 parent 495253f commit b1bb883
Show file tree
Hide file tree
Showing 16 changed files with 1,279 additions and 1,110 deletions.
3 changes: 3 additions & 0 deletions devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:
5 changes: 1 addition & 4 deletions lib/locator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:get_it/get_it.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:talawa/main.dart';
Expand Down Expand Up @@ -105,9 +104,7 @@ final actionHandlerService = locator<ActionHandlerService>();
Future<void> setupLocator() async {
locator.registerSingleton(DataBaseMutationFunctions());

locator.registerSingleton(GraphqlConfig(
httpLink: HttpLink('http://10.0.2.2:4000/'),
webSocketLink: WebSocketLink("ws://10.0.2.2:4000/graphql/")));
locator.registerSingleton(GraphqlConfig());
//services
locator.registerSingleton(NavigationService());

Expand Down
6 changes: 2 additions & 4 deletions lib/models/chats/chat_list_tile_data_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions lib/services/graphql_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class GraphqlConfig {
static String? token;
late HttpLink httpLink;
late WebSocketLink webSocketLink;

GraphqlConfig({required this.httpLink, required this.webSocketLink});
// httpLink = HttpLink('http://10.0.2.2:4000/graphql');
// webSocketLink = WebSocketLink("http://10.0.2.2:4000/graphql");



Expand Down
4 changes: 0 additions & 4 deletions lib/services/image_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class ImageService {
try {
final CroppedFile? croppedImage = await _imageCropper.cropImage(
sourcePath: imageFile.path,
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.original,
],
uiSettings: [
AndroidUiSettings(
toolbarTitle: 'Crop Image',
Expand Down
180 changes: 44 additions & 136 deletions lib/view_model/pre_auth_view_models/set_url_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
// import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'dart:developer';

import 'package:qr_flutter/qr_flutter.dart';
import 'package:talawa/constants/app_strings.dart';
import 'package:talawa/enums/enums.dart';
import 'package:talawa/locator.dart';
Expand Down Expand Up @@ -59,6 +55,9 @@ class SetUrlViewModel extends BaseModel {
/// qrValidator.
AutovalidateMode validate = AutovalidateMode.disabled;

/// qrController.
final MobileScannerController controller = MobileScannerController();

/// This function initialises the variables.
///
/// **params**:
Expand Down Expand Up @@ -239,64 +238,44 @@ class SetUrlViewModel extends BaseModel {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// SizedBox(
// height: 250,
// width: 250,
// child: QRView(
// key: qrKey,
// onQRViewCreated: _onQRViewCreated,
// overlay: QrScannerOverlayShape(
// borderRadius: 10,
// borderLength: 20,
// borderWidth: 10,
// cutOutSize: 250,
// ),
// /*overlayMargin: EdgeInsets.all(50)*/
// ),
// ),

SizedBox(
height: 250,
width: 250,
child: MobileScanner(
fit: BoxFit.contain,
onDetectError: (error, stackTrace) {
debugPrint('On detect Error: $error');
},
errorBuilder: (p0, error, p2) {
if (error.errorCode ==
MobileScannerErrorCode.permissionDenied) {
log('Camera permission was denied.');
navigationService.showTalawaErrorSnackBar(
'The Camera is not working',
MessageType.error,
);
} else if (error.errorCode ==
MobileScannerErrorCode.unsupported) {
log('This device does not support scanning.');
navigationService.showTalawaErrorSnackBar(
'Scanning is unsupported on this device',
MessageType.error,
);
} else {
log('An unknown error occurred: $error');
controller: controller,
errorBuilder: (ctx, error, _) {
String errorMessage = '';
switch (error.errorCode) {
case MobileScannerErrorCode.controllerUninitialized:
errorMessage = 'camera is not ready';
break;
case MobileScannerErrorCode.permissionDenied:
errorMessage =
'Please provide camera permission to scan QR code';
break;
case MobileScannerErrorCode.unsupported:
errorMessage =
'This device does not support scanning.';
break;
default:
errorMessage = 'An unkonwn error occurred';
}

WidgetsBinding.instance.addPostFrameCallback((_) {
navigationService.showTalawaErrorSnackBar(
'An unknown error occurred',
errorMessage,
MessageType.error,
);
}
return const Text("Something went wrong");
},
onDetect: (barcode) {
if (barcode.raw != null && barcode.barcodes.isNotEmpty) {
final String code =
barcode.barcodes.first.displayValue!;
// Handle the scanned QR code here
print('QR Code found: $code');
} else {
print('Failed to scan QR Code');
}
});

return Center(
child: Text(
errorMessage,
),
);
},
onDetect: _onQRViewCreated,
),
),

Expand All @@ -318,18 +297,15 @@ class SetUrlViewModel extends BaseModel {
/// This is the helper function which execute when the on QR view created.
///
/// **params**:
/// * `controller`: QRViewController
/// * `scanData`: BarcodeCapture
///
/// **returns**:
/// None
void _onQRViewCreated(MobileScannerController controller) {
controller.barcodes.listen((BarcodeCapture scanData) {
/// if the scanData is not empty.
if (scanData.barcodes.isNotEmpty) {
final String code = scanData.barcodes.first.displayValue!;
print(code);
// try {
void _onQRViewCreated(BarcodeCapture scanData) {
if (scanData.raw != null && scanData.barcodes.isNotEmpty) {
final String code = scanData.barcodes.first.displayValue!;
try {
final List<String> data = code.split('?');
url.text = data[0];
final List<String> queries = data[1].split('&');
Expand All @@ -343,81 +319,13 @@ class SetUrlViewModel extends BaseModel {
graphqlConfig.getOrgUrl();
Navigator.pop(navigationService.navigatorKey.currentContext!);
navigationService.pushScreen('/selectOrg', arguments: orgId);
// } on MobileScannerBarcodeException catch (e) {
// debugPrint(e.toString());
// navigationService.showTalawaErrorSnackBar(
// "The Camera is not working",
// MessageType.error,
// );
// } on QrEmbeddedImageException catch (e) {
// debugPrint(e.toString());
// navigationService.showTalawaErrorDialog(
// "The QR is not Working",
// MessageType.error,
// );
// } on QrUnsupportedVersionException catch (e) {
// debugPrint(e.toString());
// navigationService.showTalawaErrorDialog(
// "This QR version is not Supported.",
// MessageType.error,
// );
// } on Exception catch (e) {
// debugPrint(e.toString());
// navigationService.showTalawaErrorSnackBar(
// "This QR is not for the App",
// MessageType.error,
// );
// }
// }
} on Exception catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorSnackBar(
"The Camera is not working",
MessageType.error,
);
}
});

// void _onQRViewCreated(QRViewController controller) {
// controller.scannedDataStream.listen((scanData) {
// /// if the scanData is not empty.
// if (scanData.code!.isNotEmpty) {
// print(scanData.code);
// try {
// final List<String> data = scanData.code!.split('?');
// url.text = data[0];
// final List<String> queries = data[1].split('&');
// orgId = queries[0].split('=')[1];
// Vibration.vibrate(duration: 100);
// controller.stopCamera();
// controller.dispose();
// final box = Hive.box('url');
// box.put(urlKey, url.text);
// box.put(imageUrlKey, "${url.text}/talawa/");
// graphqlConfig.getOrgUrl();
// Navigator.pop(navigationService.navigatorKey.currentContext!);
// navigationService.pushScreen('/selectOrg', arguments: orgId);
// } on CameraException catch (e) {
// debugPrint(e.toString());
// navigationService.showTalawaErrorSnackBar(
// "The Camera is not working",
// MessageType.error,
// );
// } on QrEmbeddedImageException catch (e) {
// debugPrint(e.toString());
// navigationService.showTalawaErrorDialog(
// "The QR is not Working",
// MessageType.error,
// );
// } on QrUnsupportedVersionException catch (e) {
// debugPrint(e.toString());
// navigationService.showTalawaErrorDialog(
// "This QR version is not Supported.",
// MessageType.error,
// );
// } on Exception catch (e) {
// debugPrint(e.toString());
// navigationService.showTalawaErrorSnackBar(
// "This QR is not for the App",
// MessageType.error,
// );
// }
// }
// });
// }
}
}
}
Loading

0 comments on commit b1bb883

Please sign in to comment.