我正在使用 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/