显示对话框或进入相机屏幕

时间:2018-08-20 21:16:20

标签: dart flutter

大家好,我有下面的代码,如果返回错误,我想显示一个对话框,如果没有返回错误,请进入相机屏幕,我希望它在对话框中显示返回的错误。下面的函数返回结果,基于这些结果我们可以进入相机屏幕或返回错误消息。请帮助我解决此问题,谢谢!

  import 'dart:async';

  import 'package:flutter/material.dart';
  import 'package:barcode_scan/barcode_scan.dart';
  import 'package:flutter/services.dart';

  void main() => runApp(MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    ));

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() {
    return new HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  String result = "Hey there !";

  Future _scanQR() async {
    //check to see if barcode is scanned
    try {

      String qrResult = await BarcodeScanner.scan();
      setState(() {
        result = qrResult;
      });
    } on PlatformException catch (ex) {
      if (ex.code == BarcodeScanner.CameraAccessDenied) {
        setState(() {
          result = "Camera permission was denied";
        });
      } else {
        setState(() {
          result = "Unknown Error $ex";
        });
      }
    } on FormatException {
      setState(() {
        result = "You pressed the back button before scanning anything";
      });
    } catch (ex) {
      setState(() {
        result = "Unknown Error $ex";
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("QR Scanner"),
      ),
      body: Center(
        child: Text(
          result,
          style: new TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
        ),
      ),
      floatingActionButton: FloatingActionButton.extended(
        icon: Icon(Icons.camera_alt),
        label: Text("Scan"),
        onPressed: _scanQR,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
}

0 个答案:

没有答案