Flutter双击弹出退出APP使用Flutter 3.22.0

问题描述 投票:0回答:1

更新 Flutter 版本后

3.22.0
我在使用 pop 退出 APP 时遇到问题。

更新之前,我在

PopScope
小部件之前使用
Scaffold
,并且提供的回调
onPopInvoked
工作正常。 使用
bool
我可以知道这是我第一次按下返回按钮还是第二次,并且我能够正确退出应用程序。

更新后flutter似乎没有触发回调。

有人有同样的问题吗?

flutter dart gorouter
1个回答
0
投票

WillPopScope 已弃用,参考 https://docs.flutter.dev/release/writing-changes/android-predictive-back

您可以尝试使用我的快速解决方案:

int _qtyPressBack = 0;

@override
Widget build(BuildContext context) {
  return PopScope(
    canPop: _qtyPressBack >= 1,
    onPopInvoked: (didPop) {
      setState(() {
        _qtyPressBack++;
      });
      if (didPop) {
        return;
      }
    },
    child: Scaffold(
      appBar: AppBar(
        title: const Text("Test"),
        backgroundColor: Colors.grey,
      ),
      body: const Placeholder(),
    ),
  );
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.