flutter - 在 Flutter 中删除后退按钮上的 OverLayEntry

标签 flutter flutter-layout

我正在使用 OverlayEntry 并且我想在 Back Press 上删除条目。

OverlayEntry overlayEntry;
    overlayEntry = OverlayEntry(builder: (c) {
      return FullScreenLoader(
          loaderText: "Placing Order",
          );
    },maintainState: true);
    Overlay.of(context).insert(overlayEntry);

和内部 FullScreenLoader Build我用过的方法onWillScope但它仍然不起作用。
 @override
  Widget build(BuildContext context) {
    return new WillPopScope(
        onWillPop: _onWillPop,
    );
  }

Future<bool> _onWillPop() {
    return "" ?? false;
}

我只想检测物理后按按钮,以便我可以删除后按上的覆盖层。

我错过了什么吗?或者overLay不可能?

最佳答案

你尝试的方式应该有效。
使用 WillPopScope 检测背压并移除覆盖(如果可见)。

这是一个工作示例代码。
保持对覆盖条目的全局引用,并在状态类的构建方法中添加 WillPopScope:

class _XYZState extends State<XYZ> {
  OverlayEntry _detailsOverlayEntry;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: WillPopScope(
        onWillPop: _onWillPop,
        child: GoogleMap(
          mapType: MapType.normal,
          initialCameraPosition: _kFallbackInitialCameraPosition,
          polylines: _polylines.toSet(),
          myLocationEnabled: false,
          myLocationButtonEnabled: false,
          onMapCreated: (GoogleMapController controller) {
            _controller.complete(controller);
          },
        ),
      ),
    );
  }

  Future<bool> _onWillPop() {
    if(_detailsOverlayEntry != null){
      _detailsOverlayEntry.remove();
      _detailsOverlayEntry = null;
      return Future.value(false);
    }
    return Future.value(true);
  }
}

如您所见,_detailsOverlayEntry == null条件检查用于检查覆盖条目是否可见。如果它可见,请将其删除并将引用设置为 null。

下次按下返回时,它将弹出路线。

关于flutter - 在 Flutter 中删除后退按钮上的 OverLayEntry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55742350/

相关文章:

dart - 如何更改或替换 Flutter 中的 ImageCache?

flutter - 标题在flexibleSpaceBar上没有正确显示

android - Flutter:如何画星星

android - Flutter Container BoxShadow 不显示

android - Flutter:无状态小组件中的 “Looking up a deactivated widget'祖先是不安全的

android - Flutter CupertinoTimerPicker不适合父级宽度或高度

android - Flutter 键盘在 android 中隐藏文本字段,但在 iOS 中工作正常

flutter - 如何在 Flutter 中创建 45 度的线性渐变?

Flutter - 仅在有足够空间时显示图像

flutter - 防止 Cupertino 选项卡在切换选项卡时保持状态