lutter 调用原生硬件 Api 实现扫码

2021-03-09 04:28

阅读:1837

var barcode;

 

Future _scan() async { try {

String barcode = await BarcodeScanner.scan(); setState(() {

return this.barcode = barcode; });

} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {

setState(() {
return this.barcode = ‘The user did not grant the camera

permission!‘; });

      } else {
        setState(() {

return this.barcode = ‘Unknown error: $e‘; });

      }
    } on FormatException{

setState(() => this.barcode = ‘null (User returned using the "back"-button before scanning anything. Result)‘);

} catch (e) {
setState(() => this.barcode = ‘Unknown error: $e‘);

} }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(

child: Icon(Icons.camera_roll),

onPressed: _scan, ),

appBar: AppBar( title: Text("扫码"),

),

body:Text("扫码--${barcode}"), );

} }


评论


亲,登录后才可以留言!