Vishali
Vishali

Reputation: 1397

Flutter show dialog after scanning barcode

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: SafeArea(
          child: Scaffold(
              backgroundColor: Colors.transparent,
              // appBar: AppBar(
              //   title: Text('Future Demo Page'),
              // ),
              body: ProgressHUD(
                inAsyncCall: isLoading,
                child: Stack(
                  children: <Widget>[
                    getBarcodeData(),
                  ],
                ),
              )),
        ),
        onWillPop: onBackPress);
  }

  Future<bool> onBackPress() {
    Navigator.pop(context);
    String imageUrl = StorageUtil.getString('imageurl');
    String strBussName = StorageUtil.getString('buss_name');
    String strName = StorageUtil.getString('name');
    Navigator.pop(context);
    Navigator.of(context).push(MaterialPageRoute(
        builder: (context) => MainActivity(imageUrl, strName, strBussName)));
    return null;
  }

  getBarcodeData() async {
    try {
      final qrcoder = await FlutterBarcodeScanner.scanBarcode(
          '#ff6666', 'Cancel', true, ScanMode.QR);
      if (!mounted) return;
      this.barcodeScanner = qrcoder;
      if (barcodeScanner.isNotEmpty) {
        getGoodsData(barcodeScanner);
      }
    } catch (error) {
      barcodeScanner = 'Falied to get Platform Version';
    }
    return barcodeScanner;
  }

I am implementing barcode and QR code scanning in my application. Whenever I am scanning I have to get the data from QR code/barcode, later I want to send that data with API. Once the API is successfully called after getting response i want to show some dialog. The issue is after scanning page is closing and not showing dialog.

Note: After scanning page should not close need to show dialog.

Upvotes: 0

Views: 1445

Answers (2)

Kaleb
Kaleb

Reputation: 589

@override
Widget build(BuildContext context) {
  return WillPopScope(
      child: SafeArea(
        child: Scaffold(
            backgroundColor: Colors.transparent,
            // appBar: AppBar(
            //   title: Text('Future Demo Page'),
            // ),
            body: ProgressHUD(
              inAsyncCall: isLoading,
              child: Stack(
                children: <Widget>[
                  getBarcodeData(),
                ],
              ),
            )),
      ),
      onWillPop: onBackPress);
}

Future<bool> onBackPress() {
  Navigator.pop(context);
  String imageUrl = StorageUtil.getString('imageurl');
  String strBussName = StorageUtil.getString('buss_name');
  String strName = StorageUtil.getString('name');
  Navigator.pop(context);
  Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => MainActivity(imageUrl, strName, strBussName)));
  return null;
}

getBarcodeData() async {
  try {
    final qrcoder = await FlutterBarcodeScanner.scanBarcode(
        '#ff6666', 'Cancel', true, ScanMode.QR);
    if (!mounted) return;
    this.barcodeScanner = qrcoder;
    if (barcodeScanner.isNotEmpty) {
      getGoodsData(barcodeScanner);
      showDialog(
        context: context,
        builder: (context) => new AlertDialog(
          title: new Text(
            'Notice',
            style: TextStyle(color: Colors.blueGrey),
          ),
          content: Text(
            'Here is your dialogue...',
            style: TextStyle(color: Colors.black),
          ),
          actions: <Widget>[
            new TextButton(
              onPressed: () {
                //Todo add any thing you want
              },
              child: new Text('OK'),
            ),
          ],
        ),
      );
    }
  } catch (error) {
    barcodeScanner = 'Falied to get Platform Version';
  }
  return barcodeScanner;
}

If the above code worked fine then all you need is just showing the dialogue as the above. Add more info to make the question useful for other people too.

Upvotes: 1

Hesam Chobanlou
Hesam Chobanlou

Reputation: 381

it could be because you are calling Navigator.pop(context); twice in onBackPress()?

Not sure how deep in the app this is taking place, but that might be the first place to check.

Upvotes: 0

Related Questions