scanBarcode function

Future<Barcode?> scanBarcode(
  1. BuildContext context, {
  2. Set<BarcodeFormat>? formats,
  3. ScanWindow? scanWindow,
  4. CameraFacing? facing,
  5. String? hint,
  6. ScannerCheck? check,
  7. ScannerPresentation presentation = ScannerPresentation.page,
  8. ScannerTemplate? template,
  9. ScannerTheme? theme,
  10. ScannerStrings? strings,
})

Opens a scanner and returns the first barcode found (and accepted by check), or null if the user closes it.

final Barcode? barcode = await scanBarcode(context, formats: {BarcodeFormat.qrCode});

The screen is template (default ScannerTemplate.minimal) without its result sheet: the brackets lock onto the code and the screen closes. formats and scanWindow replace the template's; hint is shown under the scan window, e.g. "Point the camera at a QR code" (pass your own localized text, or an empty string for none). A check that refuses a code keeps the scanner open with the verdict's text. presentation opens it as a page or as a sheet.

Implementation

Future<Barcode?> scanBarcode(
  BuildContext context, {
  Set<BarcodeFormat>? formats,
  ScanWindow? scanWindow,
  CameraFacing? facing,
  String? hint,
  ScannerCheck? check,
  ScannerPresentation presentation = ScannerPresentation.page,
  ScannerTemplate? template,
  ScannerTheme? theme,
  ScannerStrings? strings,
}) {
  final ScannerTemplate base = template ?? const ScannerTemplate.minimal();
  final ScannerTemplate page = ScannerTemplate(
    theme: base.theme,
    scanWindow: scanWindow ?? base.scanWindow,
    formats: formats ?? base.formats,
    title: base.title,
    hint: hint == null ? base.hint : (hint.isEmpty ? null : hint),
    hintIcon: base.hintIcon,
    closeButton: base.closeButton,
    onClose: base.onClose,
    torchButton: base.torchButton,
    photoButton: base.photoButton,
    zoomButtons: base.zoomButtons,
    switchCameraButton: base.switchCameraButton,
    check: check ?? base.check,
    showResult: false,
    closeOnResult: true,
    initialZoom: base.initialZoom,
    facing: facing ?? base.facing,
    haptics: base.haptics,
    sound: base.sound,
  );
  // The page has no Material: keep the caller's font.
  final TextStyle text = DefaultTextStyle.of(context).style;
  final Widget scanner = DefaultTextStyle(
    style: text,
    child: _ScanPage(
      template: page,
      theme: theme,
      strings: strings,
      sheet: presentation == ScannerPresentation.sheet,
    ),
  );
  return Navigator.of(context).push<Barcode>(switch (presentation) {
    ScannerPresentation.page => PageRouteBuilder<Barcode>(
      reverseTransitionDuration: const Duration(milliseconds: 200),
      pageBuilder:
          (BuildContext context, Animation<double> _, Animation<double> _) =>
              scanner,
      transitionsBuilder:
          (
            BuildContext context,
            Animation<double> animation,
            Animation<double> _,
            Widget child,
          ) => FadeTransition(
            opacity: CurvedAnimation(parent: animation, curve: Curves.easeOut),
            child: child,
          ),
    ),
    ScannerPresentation.sheet => PageRouteBuilder<Barcode>(
      opaque: false,
      barrierDismissible: true,
      barrierColor: const Color(0x6B06090D),
      barrierLabel: (strings ?? const ScannerStrings()).close,
      transitionDuration: const Duration(milliseconds: 320),
      reverseTransitionDuration: const Duration(milliseconds: 240),
      pageBuilder:
          (BuildContext context, Animation<double> _, Animation<double> _) =>
              scanner,
      transitionsBuilder:
          (
            BuildContext context,
            Animation<double> animation,
            Animation<double> _,
            Widget child,
          ) => SlideTransition(
            position: Tween<Offset>(begin: const Offset(0, 1), end: Offset.zero)
                .animate(
                  CurvedAnimation(
                    parent: animation,
                    curve: Curves.easeOutCubic,
                    reverseCurve: Curves.easeInCubic,
                  ),
                ),
            child: child,
          ),
    ),
  });
}