颤音:`setState()`当屏幕关闭时,还是如何检测屏幕是否打开?
tldr:我有一个问题,如果有以下问题的答案:
- 有没有办法可以检测到屏幕是否在EG
if (SCREEN IS ON) {
setState(() {}); // i.e. update displayed widgets
}
- 上setState()这样,当屏幕关闭时,这就是完成吗?
更多详细信息:
我有一个播放音频的应用程序。屏幕打开时,小部件通过setState()
进行更新(例如,显示音频的进度)。尽管如此在完成setState()
代码之前。
因此,我希望确定我是否可以检测到setState()
在屏幕打开时,我可以在屏幕关闭时调用setState()
完成。
TLDR: I have a problem that should be solved if there is answer to one of the below questions:
- Is there a way I can detect if the screen is on e.g.
if (SCREEN IS ON) {
setState(() {}); // i.e. update displayed widgets
}
- Is there a way to call
setState()
so that is finishes when the screen is off?
More details:
I have an app that plays audio. When the screen is on, the widgets update via setState()
(e.g. to display the progress of the audio). Nonetheless it would be nice to let a user turn off their screen to save battery - however if I call setState()
when the screen is off, it seems that the app waits for the screen to be turned back on before finishing the setState()
code.
Hence I am hoping to determine if I can detect and only call setState()
when the screen is on, alternatively can I call setState()
when the screen is off so that is completes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将其用于Chatapp跟踪应用程序状态。我包裹着材料。通过一些更改,您可以做您想做的事。
I use this for chatApp to track the app State. I wrap the MaterialApp. With some changes you could do what you want.
安装可能是您想要的。另外,您可以检查 applifecyclestate href =“ https://api.flutter.dev/flutter/widgets/widgetsbindingobserver-class.html” rel =“ nofollow noreferrer”> widgetsbindingobserver 。
mounted might be what you are looking for. Alternatively, you could check the AppLifecycleState by using WidgetsBindingObserver.
我尚未测试过,但是屏幕状态在酒吧中可能是您想要的。
I haven't tested this, but screen state in the pub might be what you're looking for.
非常感谢 @Mario-Francois,我得到了以下内容为我工作:
Big thanks to @mario-francois I got the following to work for me: