如何写一个双后退按钮按下以使用颤振退出应用程序

Vin*_*ent 8 dart flutter

我是新手,我看到很多Android应用程序可以在双按后退按钮时退出.

第一次按下后退按钮,应用程序显示吐司"再按一次退出应用程序".以下第二次按,app退出.当然,两次印刷之间的时间必须不长.

如何在颤动中做到这一点?

And*_*sky 25

这是我的代码示例(我使用"fluttertoast"用于showong吐司 - 你可以使用小吃吧或警报或其他任何东西)

DateTime currentBackPressTime;

@override
Widget build(BuildContext context) {
  return Scaffold(
    ...
    body: WillPopScope(child: getBody(), onWillPop: onWillPop),
  );
}

Future<bool> onWillPop() {
    DateTime now = DateTime.now();
    if (currentBackPressTime == null || 
        now.difference(currentBackPressTime) > Duration(seconds: 2)) {
      currentBackPressTime = now;
      Fluttertoast.showToast(msg: exit_warning);
      return Future.value(false);
    }
    return Future.value(true);
  }
Run Code Online (Sandbox Code Playgroud)

  • 一定。`WillPopScope` 仅管理后按。因此,可以在第一页上实现此代码,以防止无意中关闭应用程序。如果你想关闭应用程序,可以调用 `SystemChannels.platform.invokeMethod('SystemNavigator.pop');` 或 `SystemNavigator.pop()` (3认同)
  • 回答我自己的问题:“ScaffoldState”有“isDrawerOpen”属性,所以我应该使用它。 (2认同)

Hug*_*sos 6

您可以尝试软件包。

Scaffold包装所有小部件的内,放置DoubleBackToCloseApp传递的SnackBar:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: DoubleBackToCloseApp(
          child: Home(),
          snackBar: const SnackBar(
            content: Text('Tap back again to leave'),
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

旧答案

您也可以选择涉及的解决方案SnackBar。它不像Andrey Turkovsky的回答那么简单,但是却更加优雅,您不必依赖库。

Key

class _FooState extends State<Foo> {
  static const snackBarDuration = Duration(seconds: 3);

  final snackBar = SnackBar(
    content: Text('Press back again to leave'),
    duration: snackBarDuration,
  );

  final scaffoldKey = GlobalKey<ScaffoldState>();

  DateTime backButtonPressTime;

  @override
  Widget build(_) {
    return Scaffold(
      key: scaffoldKey,
      body: WillPopScope(
        onWillPop: onWillPop,
        child: Text('Place your child here'),
      ),
    );
  }

  Future<bool> onWillPop() async {
    DateTime currentTime = DateTime.now();

    bool backButtonHasNotBeenPressedOrSnackBarHasBeenClosed =
        backButtonPressTime == null ||
            currentTime.difference(backButtonPressTime) > snackBarDuration;

    if (backButtonHasNotBeenPressedOrSnackBarHasBeenClosed) {
      backButtonPressTime = currentTime;
      scaffoldKey.currentState.showSnackBar(snackBar);
      return false;
    }

    return true;
  }
}
Run Code Online (Sandbox Code Playgroud)

没有Key

class _FooState extends State<Foo> {
  static const snackBarDuration = Duration(seconds: 3);

  final snackBar = SnackBar(
    content: Text('Press back again to leave'),
    duration: snackBarDuration,
  );

  DateTime backButtonPressTime;

  @override
  Widget build(_) {
    return Scaffold(
      body: Builder(
        builder: (BuildContext context) {
          // The BuildContext must be from one of the Scaffold's children.
          return WillPopScope(
            onWillPop: () => onWillPop(context),
            child: Text('Place your child here'),
          );
        },
      ),
    );
  }

  Future<bool> onWillPop(BuildContext context) async {
    DateTime currentTime = DateTime.now();

    bool backButtonHasNotBeenPressedOrSnackBarHasBeenClosed =
        backButtonPressTime == null ||
            currentTime.difference(backButtonPressTime) > snackBarDuration;

    if (backButtonHasNotBeenPressedOrSnackBarHasBeenClosed) {
      backButtonPressTime = currentTime;
      Scaffold.of(context).showSnackBar(snackBar);
      return false;
    }

    return true;
  }
}
Run Code Online (Sandbox Code Playgroud)