颤音:`setState()`当屏幕关闭时,还是如何检测屏幕是否打开?

发布于 2025-02-12 16:13:11 字数 520 浏览 3 评论 0原文

tldr:我有一个问题,如果有以下问题的答案:

  1. 有没有办法可以检测到屏幕是否在EG
if (SCREEN IS ON) {
  setState(() {});  // i.e. update displayed widgets
}
  1. 上setState()这样,当屏幕关闭时,这就是完成吗?

更多详细信息:

我有一个播放音频的应用程序。屏幕打开时,小部件通过setState()进行更新(例如,显示音频的进度)。尽管如此在完成setState()代码之前。

因此,我希望确定我是否可以检测到setState()在屏幕打开时,我可以在屏幕关闭时调用setState()完成。

TLDR: I have a problem that should be solved if there is answer to one of the below questions:

  1. Is there a way I can detect if the screen is on e.g.
if (SCREEN IS ON) {
  setState(() {});  // i.e. update displayed widgets
}
  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蘑菇王子 2025-02-19 16:13:11

我将其用于Chatapp跟踪应用程序状态。我包裹着材料。通过一些更改,您可以做您想做的事。

import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';

import 'constants/credentials.dart';
import 'domain/repositories/my_user_repository.dart';

class AppLifeCycleManager extends StatefulWidget {
  final Widget child;
  final MyUserRepository myUserRepo;

  const AppLifeCycleManager(
      {Key? key, required this.child, required this.myUserRepo})
      : super(key: key);

  @override
  _AppLifeCycleManagerState createState() => _AppLifeCycleManagerState();
}

class _AppLifeCycleManagerState extends State<AppLifeCycleManager>
    with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();

    widget.myUserRepo.setOnlineCustomer();
    WidgetsBinding.instance.addObserver(this);
    // set the publishable key for Stripe - this is mandatory
    Stripe.publishableKey = pkTest;
  }

  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);

    switch (state) {
      case AppLifecycleState.paused:
        widget.myUserRepo.setInactiveCustomer();
        break;
      case AppLifecycleState.resumed:
        widget.myUserRepo.setOnlineCustomer();
        break;
      case AppLifecycleState.inactive:
        widget.myUserRepo.setInactiveCustomer();
        break;
      case AppLifecycleState.detached:
        widget.myUserRepo.setInactiveCustomer();
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }

  @override
  void dispose() {
    widget.myUserRepo.setInactiveCustomer();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}

I use this for chatApp to track the app State. I wrap the MaterialApp. With some changes you could do what you want.

import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';

import 'constants/credentials.dart';
import 'domain/repositories/my_user_repository.dart';

class AppLifeCycleManager extends StatefulWidget {
  final Widget child;
  final MyUserRepository myUserRepo;

  const AppLifeCycleManager(
      {Key? key, required this.child, required this.myUserRepo})
      : super(key: key);

  @override
  _AppLifeCycleManagerState createState() => _AppLifeCycleManagerState();
}

class _AppLifeCycleManagerState extends State<AppLifeCycleManager>
    with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();

    widget.myUserRepo.setOnlineCustomer();
    WidgetsBinding.instance.addObserver(this);
    // set the publishable key for Stripe - this is mandatory
    Stripe.publishableKey = pkTest;
  }

  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);

    switch (state) {
      case AppLifecycleState.paused:
        widget.myUserRepo.setInactiveCustomer();
        break;
      case AppLifecycleState.resumed:
        widget.myUserRepo.setOnlineCustomer();
        break;
      case AppLifecycleState.inactive:
        widget.myUserRepo.setInactiveCustomer();
        break;
      case AppLifecycleState.detached:
        widget.myUserRepo.setInactiveCustomer();
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }

  @override
  void dispose() {
    widget.myUserRepo.setInactiveCustomer();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}

撩动你心 2025-02-19 16:13:11

安装可能是您想要的。另外,您可以检查 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.

七秒鱼° 2025-02-19 16:13:11

我尚未测试过,但是屏幕状态在酒吧中可能是您​​想要的。

I haven't tested this, but screen state in the pub might be what you're looking for.

何以笙箫默 2025-02-19 16:13:11

非常感谢 @Mario-Francois,我得到了以下内容为我工作:

import 'package:flutter/material.dart';

class MyPage extends StatefulWidget {
  MyPage({Key? key}) : super(key: key);

  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> with WidgetsBindingObserver {
  bool screenOn = true;

  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.paused:// screen off/navigate away
      // this.screenOn = false;
        break;
      case AppLifecycleState.resumed:  // screen on/navigate to
        this.screenOn = true;
        break;
      case AppLifecycleState.inactive:  // screen off/navigate away
        this.screenOn = false;
        break;
      case AppLifecycleState.detached:
        break;
    }
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ...
  }
}

Big thanks to @mario-francois I got the following to work for me:

import 'package:flutter/material.dart';

class MyPage extends StatefulWidget {
  MyPage({Key? key}) : super(key: key);

  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> with WidgetsBindingObserver {
  bool screenOn = true;

  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.paused:// screen off/navigate away
      // this.screenOn = false;
        break;
      case AppLifecycleState.resumed:  // screen on/navigate to
        this.screenOn = true;
        break;
      case AppLifecycleState.inactive:  // screen off/navigate away
        this.screenOn = false;
        break;
      case AppLifecycleState.detached:
        break;
    }
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ...
  }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文