我创建了一个 Flutter 应用程序,其中:
不幸的是,应用程序在“隐藏”时不会刷新(即应用程序的窗口完全被其他东西覆盖时)。这意味着当应用程序的窗口被其他窗口完全覆盖时,OBS feed 无法正确更新。 我想知道如何强制应用程序刷新 UI,即使隐藏时也是如此。
这里是
Flutter Test 应用程序的修改版本,展示了该问题每当计数器递增时
build()
flutter: rebuilding widget
flutter: incrementing counter
flutter: rebuilding widget
flutter: incrementing counter
flutter: rebuilding widget
flutter: incrementing counter
flutter: rebuilding widget
flutter: lifecycle changed to AppLifecycleState.hidden
flutter: incrementing counter
flutter: incrementing counter
flutter: incrementing counter
flutter: lifecycle changed to AppLifecycleState.inactive
flutter: rebuilding widget
flutter: incrementing counter
flutter: rebuilding widget
flutter: incrementing counter
flutter: rebuilding widget
flutter: incrementing counter
flutter: rebuilding widget
flutter: lifecycle changed to AppLifecycleState.resumed
flutter: incrementing counter
flutter: rebuilding widget
请注意,当应用程序生命周期为
hidden
时,小部件如何停止重建(即,当应用程序生命周期状态为
incrementing counter
时,rebuilding widget
打印输出后面没有 hidden
)。我想强制应用程序在状态更改时重新渲染,即使是隐藏的。此外,值得注意的是,其他 macOS 本机应用程序(例如终端)在被其他窗口隐藏时刷新不会出现问题。
src/scheduler/binding.dart:SchedulerBinding.handleAppLifecycleStateChanged
以在应用程序隐藏时启用框架。
case AppLifecycleState.resumed:
case AppLifecycleState.inactive:
_setFramesEnabledState(true);
case AppLifecycleState.hidden:
case AppLifecycleState.paused:
case AppLifecycleState.detached:
_setFramesEnabledState(false);
更改为
case AppLifecycleState.resumed:
case AppLifecycleState.inactive:
case AppLifecycleState.hidden:
_setFramesEnabledState(true);
case AppLifecycleState.paused:
case AppLifecycleState.detached:
_setFramesEnabledState(false);
否则,您也可以手动安排帧,但这有点hacky。
void main() {
runApp(const MyApp());
SchedulerBinding.instance.addPersistentFrameCallback((_) {
if (SchedulerBinding.instance.lifecycleState == AppLifecycleState.hidden) {
SchedulerBinding.instance.scheduleForcedFrame();
}
});
}
``