简体   繁体   中英

Call QR scanner/ Bar code scanner in a Container or any widget in Flutter

I'm supposed to call a Barcode scanner in Expansion Tile as a child. I could call the scanner using a function.The Scanner is being opened in a full screen. As I mentioned earlier my requirement is to call it as a child of ExpansionTile in a container with certain dimensions. I used flutter_barcode_scanner: ^0.0.9 package for the scanner.

here is the code I tried.

class ExpansionTileSample extends StatefulWidget {
  @override
  ExpansionTileSampleState createState() => new ExpansionTileSampleState();
}

class ExpansionTileSampleState extends State<ExpansionTileSample> {
  final GlobalKey<AppExpansionTileState> expansionTile = new GlobalKey();
  String barcode,value="";

  bool expand = false;
  onExpansionChanged(bool val) {
    setState(() {

      expand = !expand;
       if (expand) {

        barcodeScan();
      } else {
        print("Closed");
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: const Text('ExpansionTile'),
      ),
      body: new AppExpansionTile(
        onExpansionChanged: onExpansionChanged,
        key: expansionTile,
        title: new Text(value),
        backgroundColor: Theme.of(context).accentColor.withOpacity(0.025),
        child: expand ? Text("done") : Text("not done"),
      ),
    );
  }
  Future barcodeScan() async{
    barcode=await FlutterBarcodeScanner.scanBarcode("#004297", "cancel", false);
    setState(() {
      value=barcode;
    });
  }
}

How do I call the function(since it is not a widget, I cannot call it as a child) in place of child: expand ? Text("done") : Text("not done"), child: expand ? Text("done") : Text("not done"), in my code?

Instead of flutter_barcode_scanner plugin, you can use fast_qr_reader_view which will provide you QRReaderPreview widget. By that you will be able to achieve your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM