【问题标题】:Flutter show dialog after scanning barcode扫描条码后颤振显示对话框
【发布时间】:2021-09-30 16:31:00
【问题描述】:
  @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;
  }

我正在我的应用程序中实现条形码和二维码扫描。每当我扫描时,我必须从 QR 码/条形码中获取数据,稍后我想用 API 发送该数据。在获得响应后成功调用 API 后,我想显示一些对话框。问题是在扫描页面关闭并且不显示对话框之后。

注意:扫描页面后不应该关闭需要显示对话框。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:
    @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;
    }
    

    如果上面的代码运行良好,那么您只需要显示上面的对话即可。添加更多信息以使该问题对其他人也有用。

    【讨论】:

      【解决方案2】:

      这可能是因为您正在调用 Navigator.pop(context);两次 onBackPress()?

      不确定这在应用程序中发生了多深,但这可能是首先要检查的地方。

      【讨论】:

        猜你喜欢
        • 2022-11-11
        • 2020-08-23
        • 1970-01-01
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-18
        • 2018-11-21
        相关资源
        最近更新 更多