Advertisement
abimulya_

Untitled

Sep 23rd, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.89 KB | None | 0 0
  1. // ignore_for_file: public_member_api_docs, sort_constructors_first
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter_hooks/flutter_hooks.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. import 'package:flutter_svg/svg.dart';
  7. import 'package:focus_detector/focus_detector.dart';
  8. import 'package:get/get_utils/src/extensions/context_extensions.dart';
  9.  
  10. import '../../../config/theme/theme.dart';
  11.  
  12. class ScanScannerBarcodeView extends HookWidget {
  13.   const ScanScannerBarcodeView({
  14.     Key? key,
  15.     this.descText = '',
  16.     required this.doOnBarcodeScanned,
  17.   }) : super(key: key);
  18.  
  19.   final String? descText;
  20.   final Function(String barcodeFound) doOnBarcodeScanned;
  21.  
  22.   @override
  23.   Widget build(BuildContext context) {
  24.     final scannedBarcode = useState('');
  25.     final focusNode = useFocusNode();
  26.  
  27.     useEffect(() {
  28.       focusNode.requestFocus();
  29.       return null;
  30.     }, []);
  31.  
  32.     void handleKeyEvent(KeyEvent event) {
  33.       if (event is KeyDownEvent) {
  34.         if (event.logicalKey == LogicalKeyboardKey.enter) {
  35.           // Barcode input is complete
  36.           if (scannedBarcode.value.isNotEmpty) {
  37.             doOnBarcodeScanned(scannedBarcode.value);
  38.             scannedBarcode.value = '';
  39.           }
  40.         } else {
  41.           // Build the barcode string
  42.           scannedBarcode.value += event.character ?? '';
  43.         }
  44.       }
  45.     }
  46.  
  47.     return FocusDetector(
  48.       onVisibilityGained: () {
  49.         focusNode.requestFocus();
  50.       },
  51.       child: KeyboardListener(
  52.         focusNode: focusNode,
  53.         onKeyEvent: handleKeyEvent,
  54.         child: Container(
  55.           color: AccurateColor.grayBlue,
  56.           height: 1.sh,
  57.           width: 1.sw,
  58.           child: Column(
  59.             mainAxisAlignment: MainAxisAlignment.center,
  60.             children: [
  61.               SvgPicture.asset(
  62.                 'assets/svg/scan/ic_scan_scanner_big.svg',
  63.               ),
  64.               Text(
  65.                 descText!,
  66.                 style: context.textTheme.bodyLarge!.copyWith(
  67.                   color: AccurateColor.grayA2,
  68.                 ),
  69.               ),
  70.             ],
  71.           ),
  72.         ),
  73.       ),
  74.     );
  75. /*    return BarcodeKeyboardListener(
  76.       bufferDuration: const Duration(milliseconds: 200),
  77.       onBarcodeScanned: doOnBarcodeScanned,
  78.       child: Container(
  79.         color: AccurateColor.grayBlue,
  80.         height: 1.sh,
  81.         width: 1.sw,
  82.         child: Column(
  83.           mainAxisAlignment: MainAxisAlignment.center,
  84.           children: [
  85.             SvgPicture.asset(
  86.               'assets/svg/scan/ic_scan_scanner_big.svg',
  87.             ),
  88.             Text(
  89.               descText!,
  90.               style: context.textTheme.bodyLarge!.copyWith(
  91.                 color: AccurateColor.grayA2,
  92.               ),
  93.             ),
  94.           ],
  95.         ),
  96.       ),
  97.     );*/
  98.   }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement