gpt4 book ai didi

flutter - 在 Flutter 中添加 OverlayEntry

转载 作者:IT老高 更新时间:2023-10-28 12:44:35 25 4
gpt4 key购买 nike

我正在尝试将 Container 插入 Overlay,但此代码出错。

class _MyHomePageState extends State<MyHomePage> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
final entry = OverlayEntry(builder: (BuildContext overlayContext) {
return Container(
height: 50.0,
width: 50.0,
color: Colors.blue,
);
});
_addOverlay(entry);
}

void _addOverlay(OverlayEntry entry) async {
Overlay.of(context).insert(entry);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter'),
),
body: Center(),
);
}
}

这是错误

setState() or markNeedsBuild() called during build. This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase...

提前谢谢你。

最佳答案

自从上次更新到 Flutter 0.8.1 后,我也注意到了这个变化。我修复了这个问题,以便在最小延迟后添加叠加层

Timer.run(() { Overlay.of(context).insert(calendarOverlay);});

现在这可行,但感觉就像一个黑客......

所以在我的构建中,当覆盖应该出现时,我会使用此代码..

如果有人有更好的解决方案,我很感兴趣;-)

约翰

更新:我发现这段代码也可以工作:

final overlay = Overlay.of(context);
WidgetsBinding.instance.addPostFrameCallback((_) => overlay.insert(entry));

它使我免于包含计时器...

关于flutter - 在 Flutter 中添加 OverlayEntry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51493737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com