无法从其构建器小部件中删除"覆盖条目"



我已经创建了一个overlayEntry并将其插入到overlayState中,在构建器方法中,我返回了一个应该删除overlayEntryGestureDetector,但如何实现类似overlayEntry.remove();的东西?


showOverLay(BuildContext context, Widget child,
[double top, double bottom, double right, double left]) {
OverlayState overlayState = Overlay.of(context);
OverlayEntry overlayEntry = OverlayEntry(
builder: (context) {
return Material(
color: Colors.transparent,
child: GestureDetector(
onTap: () {
/// implementation to remove `overlayEntry` there 
},
child: Positioned(
top: top,
bottom: bottom,
right: right,
left: left,
child: GestureDetector(
onTap: () {},
child: child,
),
),
),
);
},
);
overlayState.insert(overlayEntry);
}

你可以这样做

OverlayEntry? overlay;
overlay = OverlayEntry(builder: (context) {
return Container(
width: 200,
height: 200,
alignment: Alignment.center,
color: Colors.red,
child: ElevatedButton(
child: Text('click'),
onPressed: () {
overlay?.remove();
},
),
);
});
Overlay.of(context)?.insert(overlay);

最新更新