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. Everything else of the template stays; one that collects codes or keeps scanning takes one code here. formats, scanWindow, facing, hint and check 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.

The theme and texts of a ScannerScope around context apply, under the template's theme, theme and strings.

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 = base.copyWith(
    scanWindow: scanWindow,
    formats: formats == null ? null : () => formats,
    hint: hint == null ? null : () => hint,
    facing: facing == null ? null : () => facing,
    check: check == null ? null : () => check,
    // One code and the screen closes: a template that collects codes or
    // keeps scanning takes one at a time here.
    mode: switch (base.mode) {
      ScannerMode.collect || ScannerMode.continuous => ScannerMode.single,
      final ScannerMode mode => mode,
    },
    showResult: false,
    closeOnResult: true,
    // It counts the codes of a scanner that keeps scanning.
    counter: false,
  );
  // The route is built under the navigator: take the caller's scope along.
  final ScannerScope? around = context
      .getInheritedWidgetOfExactType<ScannerScope>();
  final ScannerStrings texts = strings ?? ScannerStrings.of(context);
  // The page has no Material: keep the caller's font.
  final TextStyle text = DefaultTextStyle.of(context).style;
  final Widget scanner = ScannerScope(
    theme: around?.theme ?? const ScannerTheme(),
    strings: texts,
    child: DefaultTextStyle(
      style: text,
      child: _ScanPage(
        template: page,
        theme: theme,
        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: texts.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,
          ),
    ),
  });
}