Showing
2 changed files
with
155 additions
and
159 deletions
| @@ -25,132 +25,130 @@ class _BarcodeScannerWithControllerState | @@ -25,132 +25,130 @@ class _BarcodeScannerWithControllerState | ||
| 25 | 25 | ||
| 26 | @override | 26 | @override |
| 27 | Widget build(BuildContext context) { | 27 | Widget build(BuildContext context) { |
| 28 | - return MaterialApp( | ||
| 29 | - home: Scaffold( | ||
| 30 | - backgroundColor: Colors.black, | ||
| 31 | - body: Builder(builder: (context) { | ||
| 32 | - return Stack( | ||
| 33 | - children: [ | ||
| 34 | - MobileScanner( | ||
| 35 | - controller: controller, | ||
| 36 | - fit: BoxFit.contain, | ||
| 37 | - // controller: MobileScannerController( | ||
| 38 | - // torchEnabled: true, | ||
| 39 | - // facing: CameraFacing.front, | ||
| 40 | - // ), | ||
| 41 | - onDetect: (barcode, args) { | ||
| 42 | - if (this.barcode != barcode.rawValue) { | ||
| 43 | - setState(() { | ||
| 44 | - this.barcode = barcode.rawValue; | ||
| 45 | - }); | ||
| 46 | - } | ||
| 47 | - }), | ||
| 48 | - Align( | 28 | + return Scaffold( |
| 29 | + backgroundColor: Colors.black, | ||
| 30 | + body: Builder(builder: (context) { | ||
| 31 | + return Stack( | ||
| 32 | + children: [ | ||
| 33 | + MobileScanner( | ||
| 34 | + controller: controller, | ||
| 35 | + fit: BoxFit.contain, | ||
| 36 | + // controller: MobileScannerController( | ||
| 37 | + // torchEnabled: true, | ||
| 38 | + // facing: CameraFacing.front, | ||
| 39 | + // ), | ||
| 40 | + onDetect: (barcode, args) { | ||
| 41 | + if (this.barcode != barcode.rawValue) { | ||
| 42 | + setState(() { | ||
| 43 | + this.barcode = barcode.rawValue; | ||
| 44 | + }); | ||
| 45 | + } | ||
| 46 | + }), | ||
| 47 | + Align( | ||
| 48 | + alignment: Alignment.bottomCenter, | ||
| 49 | + child: Container( | ||
| 49 | alignment: Alignment.bottomCenter, | 50 | alignment: Alignment.bottomCenter, |
| 50 | - child: Container( | ||
| 51 | - alignment: Alignment.bottomCenter, | ||
| 52 | - height: 100, | ||
| 53 | - color: Colors.black.withOpacity(0.4), | ||
| 54 | - child: Row( | ||
| 55 | - crossAxisAlignment: CrossAxisAlignment.center, | ||
| 56 | - mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
| 57 | - children: [ | ||
| 58 | - IconButton( | ||
| 59 | - color: Colors.white, | ||
| 60 | - icon: ValueListenableBuilder( | ||
| 61 | - valueListenable: controller.torchState, | ||
| 62 | - builder: (context, state, child) { | ||
| 63 | - switch (state as TorchState) { | ||
| 64 | - case TorchState.off: | ||
| 65 | - return const Icon(Icons.flash_off, | ||
| 66 | - color: Colors.grey); | ||
| 67 | - case TorchState.on: | ||
| 68 | - return const Icon(Icons.flash_on, | ||
| 69 | - color: Colors.yellow); | ||
| 70 | - } | ||
| 71 | - }, | ||
| 72 | - ), | ||
| 73 | - iconSize: 32.0, | ||
| 74 | - onPressed: () => controller.toggleTorch(), | 51 | + height: 100, |
| 52 | + color: Colors.black.withOpacity(0.4), | ||
| 53 | + child: Row( | ||
| 54 | + crossAxisAlignment: CrossAxisAlignment.center, | ||
| 55 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
| 56 | + children: [ | ||
| 57 | + IconButton( | ||
| 58 | + color: Colors.white, | ||
| 59 | + icon: ValueListenableBuilder( | ||
| 60 | + valueListenable: controller.torchState, | ||
| 61 | + builder: (context, state, child) { | ||
| 62 | + switch (state as TorchState) { | ||
| 63 | + case TorchState.off: | ||
| 64 | + return const Icon(Icons.flash_off, | ||
| 65 | + color: Colors.grey); | ||
| 66 | + case TorchState.on: | ||
| 67 | + return const Icon(Icons.flash_on, | ||
| 68 | + color: Colors.yellow); | ||
| 69 | + } | ||
| 70 | + }, | ||
| 75 | ), | 71 | ), |
| 76 | - IconButton( | ||
| 77 | - color: Colors.white, | ||
| 78 | - icon: isStarted | ||
| 79 | - ? const Icon(Icons.stop) | ||
| 80 | - : const Icon(Icons.play_arrow), | ||
| 81 | - iconSize: 32.0, | ||
| 82 | - onPressed: () => setState(() { | ||
| 83 | - isStarted | ||
| 84 | - ? controller.stop() | ||
| 85 | - : controller.start(); | ||
| 86 | - isStarted = !isStarted; | ||
| 87 | - })), | ||
| 88 | - Center( | ||
| 89 | - child: SizedBox( | ||
| 90 | - width: MediaQuery.of(context).size.width - 200, | ||
| 91 | - height: 50, | ||
| 92 | - child: FittedBox( | ||
| 93 | - child: Text( | ||
| 94 | - barcode ?? 'Scan something!', | ||
| 95 | - overflow: TextOverflow.fade, | ||
| 96 | - style: Theme.of(context) | ||
| 97 | - .textTheme | ||
| 98 | - .headline4! | ||
| 99 | - .copyWith(color: Colors.white), | ||
| 100 | - ), | 72 | + iconSize: 32.0, |
| 73 | + onPressed: () => controller.toggleTorch(), | ||
| 74 | + ), | ||
| 75 | + IconButton( | ||
| 76 | + color: Colors.white, | ||
| 77 | + icon: isStarted | ||
| 78 | + ? const Icon(Icons.stop) | ||
| 79 | + : const Icon(Icons.play_arrow), | ||
| 80 | + iconSize: 32.0, | ||
| 81 | + onPressed: () => setState(() { | ||
| 82 | + isStarted | ||
| 83 | + ? controller.stop() | ||
| 84 | + : controller.start(); | ||
| 85 | + isStarted = !isStarted; | ||
| 86 | + })), | ||
| 87 | + Center( | ||
| 88 | + child: SizedBox( | ||
| 89 | + width: MediaQuery.of(context).size.width - 200, | ||
| 90 | + height: 50, | ||
| 91 | + child: FittedBox( | ||
| 92 | + child: Text( | ||
| 93 | + barcode ?? 'Scan something!', | ||
| 94 | + overflow: TextOverflow.fade, | ||
| 95 | + style: Theme.of(context) | ||
| 96 | + .textTheme | ||
| 97 | + .headline4! | ||
| 98 | + .copyWith(color: Colors.white), | ||
| 101 | ), | 99 | ), |
| 102 | ), | 100 | ), |
| 103 | ), | 101 | ), |
| 104 | - IconButton( | ||
| 105 | - color: Colors.white, | ||
| 106 | - icon: ValueListenableBuilder( | ||
| 107 | - valueListenable: controller.cameraFacingState, | ||
| 108 | - builder: (context, state, child) { | ||
| 109 | - switch (state as CameraFacing) { | ||
| 110 | - case CameraFacing.front: | ||
| 111 | - return const Icon(Icons.camera_front); | ||
| 112 | - case CameraFacing.back: | ||
| 113 | - return const Icon(Icons.camera_rear); | ||
| 114 | - } | ||
| 115 | - }, | ||
| 116 | - ), | ||
| 117 | - iconSize: 32.0, | ||
| 118 | - onPressed: () => controller.switchCamera(), | ||
| 119 | - ), | ||
| 120 | - IconButton( | ||
| 121 | - color: Colors.white, | ||
| 122 | - icon: const Icon(Icons.image), | ||
| 123 | - iconSize: 32.0, | ||
| 124 | - onPressed: () async { | ||
| 125 | - final ImagePicker _picker = ImagePicker(); | ||
| 126 | - // Pick an image | ||
| 127 | - final XFile? image = await _picker.pickImage( | ||
| 128 | - source: ImageSource.gallery); | ||
| 129 | - if (image != null) { | ||
| 130 | - if (await controller.analyzeImage(image.path)) { | ||
| 131 | - ScaffoldMessenger.of(context) | ||
| 132 | - .showSnackBar(const SnackBar( | ||
| 133 | - content: Text('Barcode found!'), | ||
| 134 | - backgroundColor: Colors.green, | ||
| 135 | - )); | ||
| 136 | - } else { | ||
| 137 | - ScaffoldMessenger.of(context) | ||
| 138 | - .showSnackBar(const SnackBar( | ||
| 139 | - content: Text('No barcode found!'), | ||
| 140 | - backgroundColor: Colors.red, | ||
| 141 | - )); | ||
| 142 | - } | 102 | + ), |
| 103 | + IconButton( | ||
| 104 | + color: Colors.white, | ||
| 105 | + icon: ValueListenableBuilder( | ||
| 106 | + valueListenable: controller.cameraFacingState, | ||
| 107 | + builder: (context, state, child) { | ||
| 108 | + switch (state as CameraFacing) { | ||
| 109 | + case CameraFacing.front: | ||
| 110 | + return const Icon(Icons.camera_front); | ||
| 111 | + case CameraFacing.back: | ||
| 112 | + return const Icon(Icons.camera_rear); | ||
| 143 | } | 113 | } |
| 144 | }, | 114 | }, |
| 145 | ), | 115 | ), |
| 146 | - ], | ||
| 147 | - ), | 116 | + iconSize: 32.0, |
| 117 | + onPressed: () => controller.switchCamera(), | ||
| 118 | + ), | ||
| 119 | + IconButton( | ||
| 120 | + color: Colors.white, | ||
| 121 | + icon: const Icon(Icons.image), | ||
| 122 | + iconSize: 32.0, | ||
| 123 | + onPressed: () async { | ||
| 124 | + final ImagePicker _picker = ImagePicker(); | ||
| 125 | + // Pick an image | ||
| 126 | + final XFile? image = await _picker.pickImage( | ||
| 127 | + source: ImageSource.gallery); | ||
| 128 | + if (image != null) { | ||
| 129 | + if (await controller.analyzeImage(image.path)) { | ||
| 130 | + ScaffoldMessenger.of(context) | ||
| 131 | + .showSnackBar(const SnackBar( | ||
| 132 | + content: Text('Barcode found!'), | ||
| 133 | + backgroundColor: Colors.green, | ||
| 134 | + )); | ||
| 135 | + } else { | ||
| 136 | + ScaffoldMessenger.of(context) | ||
| 137 | + .showSnackBar(const SnackBar( | ||
| 138 | + content: Text('No barcode found!'), | ||
| 139 | + backgroundColor: Colors.red, | ||
| 140 | + )); | ||
| 141 | + } | ||
| 142 | + } | ||
| 143 | + }, | ||
| 144 | + ), | ||
| 145 | + ], | ||
| 148 | ), | 146 | ), |
| 149 | ), | 147 | ), |
| 150 | - ], | ||
| 151 | - ); | ||
| 152 | - }), | ||
| 153 | - ), | 148 | + ), |
| 149 | + ], | ||
| 150 | + ); | ||
| 151 | + }), | ||
| 154 | ); | 152 | ); |
| 155 | } | 153 | } |
| 156 | } | 154 | } |
| @@ -16,55 +16,53 @@ class _BarcodeScannerWithoutControllerState | @@ -16,55 +16,53 @@ class _BarcodeScannerWithoutControllerState | ||
| 16 | 16 | ||
| 17 | @override | 17 | @override |
| 18 | Widget build(BuildContext context) { | 18 | Widget build(BuildContext context) { |
| 19 | - return MaterialApp( | ||
| 20 | - home: Scaffold( | ||
| 21 | - backgroundColor: Colors.black, | ||
| 22 | - body: Builder(builder: (context) { | ||
| 23 | - return Stack( | ||
| 24 | - children: [ | ||
| 25 | - MobileScanner( | ||
| 26 | - fit: BoxFit.contain, | ||
| 27 | - onDetect: (barcode, args) { | ||
| 28 | - if (this.barcode != barcode.rawValue) { | ||
| 29 | - setState(() { | ||
| 30 | - this.barcode = barcode.rawValue; | ||
| 31 | - }); | ||
| 32 | - } | ||
| 33 | - }), | ||
| 34 | - Align( | 19 | + return Scaffold( |
| 20 | + backgroundColor: Colors.black, | ||
| 21 | + body: Builder(builder: (context) { | ||
| 22 | + return Stack( | ||
| 23 | + children: [ | ||
| 24 | + MobileScanner( | ||
| 25 | + fit: BoxFit.contain, | ||
| 26 | + onDetect: (barcode, args) { | ||
| 27 | + if (this.barcode != barcode.rawValue) { | ||
| 28 | + setState(() { | ||
| 29 | + this.barcode = barcode.rawValue; | ||
| 30 | + }); | ||
| 31 | + } | ||
| 32 | + }), | ||
| 33 | + Align( | ||
| 34 | + alignment: Alignment.bottomCenter, | ||
| 35 | + child: Container( | ||
| 35 | alignment: Alignment.bottomCenter, | 36 | alignment: Alignment.bottomCenter, |
| 36 | - child: Container( | ||
| 37 | - alignment: Alignment.bottomCenter, | ||
| 38 | - height: 100, | ||
| 39 | - color: Colors.black.withOpacity(0.4), | ||
| 40 | - child: Row( | ||
| 41 | - crossAxisAlignment: CrossAxisAlignment.center, | ||
| 42 | - mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
| 43 | - children: [ | ||
| 44 | - Center( | ||
| 45 | - child: SizedBox( | ||
| 46 | - width: MediaQuery.of(context).size.width - 120, | ||
| 47 | - height: 50, | ||
| 48 | - child: FittedBox( | ||
| 49 | - child: Text( | ||
| 50 | - barcode ?? 'Scan something!', | ||
| 51 | - overflow: TextOverflow.fade, | ||
| 52 | - style: Theme.of(context) | ||
| 53 | - .textTheme | ||
| 54 | - .headline4! | ||
| 55 | - .copyWith(color: Colors.white), | ||
| 56 | - ), | 37 | + height: 100, |
| 38 | + color: Colors.black.withOpacity(0.4), | ||
| 39 | + child: Row( | ||
| 40 | + crossAxisAlignment: CrossAxisAlignment.center, | ||
| 41 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
| 42 | + children: [ | ||
| 43 | + Center( | ||
| 44 | + child: SizedBox( | ||
| 45 | + width: MediaQuery.of(context).size.width - 120, | ||
| 46 | + height: 50, | ||
| 47 | + child: FittedBox( | ||
| 48 | + child: Text( | ||
| 49 | + barcode ?? 'Scan something!', | ||
| 50 | + overflow: TextOverflow.fade, | ||
| 51 | + style: Theme.of(context) | ||
| 52 | + .textTheme | ||
| 53 | + .headline4! | ||
| 54 | + .copyWith(color: Colors.white), | ||
| 57 | ), | 55 | ), |
| 58 | ), | 56 | ), |
| 59 | ), | 57 | ), |
| 60 | - ], | ||
| 61 | - ), | 58 | + ), |
| 59 | + ], | ||
| 62 | ), | 60 | ), |
| 63 | ), | 61 | ), |
| 64 | - ], | ||
| 65 | - ); | ||
| 66 | - }), | ||
| 67 | - ), | 62 | + ), |
| 63 | + ], | ||
| 64 | + ); | ||
| 65 | + }), | ||
| 68 | ); | 66 | ); |
| 69 | } | 67 | } |
| 70 | } | 68 | } |
-
Please register or login to post a comment