Advertisement
deimos

mobile scanner

Feb 28th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:mobile_scanner/mobile_scanner.dart';
  3.  
  4. void main() {
  5. runApp(const MyApp());
  6. }
  7.  
  8. class MyApp extends StatelessWidget {
  9. const MyApp({super.key});
  10.  
  11. // This widget is the root of your application.
  12. @override
  13. Widget build(BuildContext context) {
  14. return const MaterialApp(
  15. title: 'SN scanner Demo',
  16. home: MyHomePage(),
  17. );
  18. }
  19. }
  20.  
  21. class MyHomePage extends StatefulWidget {
  22. const MyHomePage({super.key});
  23.  
  24. @override
  25. State<MyHomePage> createState() => _MyHomePageState();
  26. }
  27.  
  28. class _MyHomePageState extends State<MyHomePage> {
  29. ValueNotifier<String> snFound = ValueNotifier('');
  30.  
  31. @override
  32. Widget build(BuildContext context) {
  33. return Scaffold(
  34. body: Stack(
  35. children: [
  36. SnScanner(
  37. onSnFound: (sn) {
  38. print('********** SN found: $sn');
  39. snFound.value = sn;
  40. },
  41. ),
  42.  
  43. ValueListenableBuilder(
  44. valueListenable: snFound,
  45. builder: (_, sn, __) {
  46. return Align(
  47. alignment: Alignment.bottomCenter,
  48. child: Text(
  49. sn,
  50. textScaleFactor: 2,
  51. style: const TextStyle(backgroundColor: Colors.white54),
  52. ),
  53. );
  54. },
  55. ),
  56. ],
  57. ),
  58. );
  59. }
  60. }
  61.  
  62.  
  63. class SnScanner extends StatelessWidget {
  64. final Function(String sn)? onSnFound;
  65.  
  66. const SnScanner({Key? key, this.onSnFound})
  67. : super(key: key);
  68.  
  69. @override
  70. Widget build(BuildContext context) {
  71. MobileScannerController cameraController = MobileScannerController();
  72.  
  73. return Scaffold(
  74. appBar: AppBar(
  75. title: const Text('Mobile Scanner'),
  76. actions: [
  77. IconButton(
  78. color: Colors.white,
  79. icon: ValueListenableBuilder(
  80. valueListenable: cameraController.torchState,
  81. builder: (context, state, child) {
  82. switch (state as TorchState) {
  83. case TorchState.off:
  84. return const Icon(Icons.flash_off, color: Colors.grey);
  85. case TorchState.on:
  86. return const Icon(Icons.flash_on, color: Colors.yellow);
  87. }
  88. },
  89. ),
  90. iconSize: 32.0,
  91. onPressed: () => cameraController.toggleTorch(),
  92. ),
  93. IconButton(
  94. color: Colors.white,
  95. icon: ValueListenableBuilder(
  96. valueListenable: cameraController.cameraFacingState,
  97. builder: (context, state, child) {
  98. switch (state as CameraFacing) {
  99. case CameraFacing.front:
  100. return const Icon(Icons.camera_front);
  101. case CameraFacing.back:
  102. return const Icon(Icons.camera_rear);
  103. }
  104. },
  105. ),
  106. iconSize: 32.0,
  107. onPressed: () => cameraController.switchCamera(),
  108. ),
  109. ],
  110. ),
  111. body: MobileScanner(
  112. allowDuplicates: false,
  113. controller: cameraController,
  114. onDetect: (barcode, args) {
  115. if (barcode.rawValue == null) {
  116. } else {
  117. final String code = barcode.rawValue!;
  118. if (onSnFound != null && _validateSn(code)) {
  119. onSnFound!(code);
  120. }
  121. }
  122. }));
  123. }
  124.  
  125. bool _validateSn(String sn) {
  126. return sn.isNotEmpty && sn.startsWith('S') && sn.length == 13;
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement