Skip to content

Instantly share code, notes, and snippets.

@unacorbatanegra
Last active April 18, 2023 14:26
Show Gist options
  • Save unacorbatanegra/94ce3e7eedf2d48d1cd700ffefa12a4b to your computer and use it in GitHub Desktop.
Save unacorbatanegra/94ce3e7eedf2d48d1cd700ffefa12a4b to your computer and use it in GitHub Desktop.
example
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:sco_mobile/constants/colors.dart';
typedef MobileScannerErrorBuilder = Widget Function(
BuildContext,
MobileScannerException,
Widget?,
);
class ScannerScreen extends StatefulWidget {
const ScannerScreen({Key? key}) : super(key: key);
@override
ScannerScreenState createState() => ScannerScreenState();
}
class ScannerScreenState extends State<ScannerScreen> {
MobileScannerController cameraController = MobileScannerController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(''),
backgroundColor: Colors.transparent,
elevation: 0.0,
leading: IconButton(
icon: SvgPicture.asset(
'assets/images/icons/ArrowLeft.svg',
colorFilter: const ColorFilter.mode(
NeutralColor.n0,
BlendMode.srcIn,
),
),
onPressed: () => Navigator.of(context).pop(),
),
actions: [
IconButton(
icon: ValueListenableBuilder(
valueListenable: cameraController.torchState,
builder: (context, state, child) {
return SvgPicture.asset(
state == TorchState.on
? 'assets/images/icons/smile.svg'
: 'assets/images/icons/perfil.svg',
colorFilter: const ColorFilter.mode(
NeutralColor.n0,
BlendMode.srcIn,
),
);
},
),
onPressed: () => cameraController.toggleTorch(),
),
IconButton(
icon: ValueListenableBuilder(
valueListenable: cameraController.cameraFacingState,
builder: (context, state, child) {
return SvgPicture.asset(
state == CameraFacing.front
? 'assets/images/icons/Eye.svg'
: 'assets/images/icons/EyeSlash.svg',
colorFilter: const ColorFilter.mode(
NeutralColor.n0,
BlendMode.srcIn,
),
);
},
),
onPressed: () => cameraController.switchCamera(),
),
],
),
extendBodyBehindAppBar: true,
body: Stack(
children: [
MobileScanner(
controller: cameraController,
errorBuilder:(context,exception,widget){
print(exception);
return Center(child:Text(exception.toString()));
}
,
onDetect: (capture) {
final List<Barcode> barcodes = capture.barcodes;
for (final barcode in barcodes) {
debugPrint('Barcode found >>>>> ${barcode.rawValue} <<<<<');
}
},
),
_buildOverlay(MediaQuery.of(context).size),
],
),
);
}
}
class InvertedClipper extends CustomClipper<Path> {
final Rect cutOutRect;
InvertedClipper(this.cutOutRect);
@override
Path getClip(Size size) {
final path = Path()
..addRect(Rect.fromLTWH(0, 0, size.width, size.height))
..addRect(cutOutRect);
path.fillType = PathFillType.evenOdd;
return path;
}
@override
bool shouldReclip(InvertedClipper oldClipper) =>
oldClipper.cutOutRect != cutOutRect;
}
Widget _buildOverlay(Size size) {
final cutOutRect = Rect.fromLTWH(
16,
(size.height - 208) / 2,
size.width - 32,
208,
);
return ClipPath(
clipper: InvertedClipper(cutOutRect),
child: Container(
color: NeutralColor.n700.withOpacity(0.9),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment