Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanner area border isn't created and scan doesn't work after toggle flash (IOS) #65

Open
valera21115 opened this issue Jun 23, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@valera21115
Copy link

flutter: 3.0.2
ai_barcode: 3.2.2

After opening scanner screen in IOS scan area border isn't created and exception is thrown .
image

Also, after toggle flash, scan doesn't work.

main.dart

import 'package:flutter/material.dart';

import 'scanner_screen.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomePage(title: 'Home page'),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Center(
            child:ElevatedButton(onPressed: () {Navigator.push(context, MaterialPageRoute(builder: (context) => const ScannerScreen()));},
                child: const Text('Open scanner'))
        ));
  }
}

scanner_screen.dart

import 'dart:io';

import 'package:ai_barcode/ai_barcode.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class ScannerScreen extends StatefulWidget {
  const ScannerScreen({Key? key}) : super(key: key);

  @override
  State<ScannerScreen> createState() => _ScannerScreenState();
}

class _ScannerScreenState extends State<ScannerScreen> {
  late ScannerController _scannerController;
  bool _isFlashOn = false;
  String? barcode;

  @override
  void initState() {
    super.initState();
    _scannerController = ScannerController(scannerResult: (barcode)  => setState(()=> this.barcode = barcode), scannerViewCreated: () {
      if (Platform.isIOS) {
        Future.delayed(const Duration(seconds: 2), () {
          _scannerController.startCamera();
          _scannerController.startCameraPreview();
        });
      } else {
        _scannerController.startCamera();
        _scannerController.startCameraPreview();
      }
    });
  }

  @override
  Widget build(BuildContext context) => SafeArea(
    child: Scaffold(
      body: FutureBuilder<bool?>(
        future: Permission.camera.request().isGranted,
        builder: (_, snapshot) {
          if (snapshot.hasData) {
            return  Stack(
              children: <Widget>[
                PlatformAiBarcodeScannerWidget(
                  platformScannerController: _scannerController,
                ),
                  Padding(
                      padding: const EdgeInsets.only(top: 8),
                      child: IconButton(
                          padding: const EdgeInsets.only(left: 14),
                          onPressed: () {
                            _scannerController.toggleFlash();
                            setState(() => _isFlashOn = !_isFlashOn);
                          },
                          icon: Icon(
                            _isFlashOn ? Icons.flash_on : Icons.flash_off,
                            color: Colors.white,
                            size: 40,
                          ))),
                Align(alignment: Alignment.bottomCenter,child: Text(barcode == null ? 'Scan a code' :  barcode!, style: const TextStyle(color: Colors.red, fontSize: 24)))
              ],
            );
          }
          return const Center(child: CircularProgressIndicator());
        },
      ),
    ),
  );
}
@pdliuw pdliuw added the bug Something isn't working label Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants