一个简单且灵活的 Flutter 关闭应用插件

(Android 按两次返回键退出,MacOS、iOS、Windows、Linux 一键退出)

功能

  • ✅ 按两次返回键退出应用
    • ✅ 自定义时间间隔
    • ✅ 自定义提示消息
    • ✅ 自定义匹配条件
    • ✅ 支持 Android
  • ✅ 一键退出应用
    • ✅ 支持 iOS
    • ✅ 支持 MacOS
    • ✅ 支持 Windows
    • ✅ 支持 Linux

截图

使用

  • 依赖
flutter pub add flutter_close_app

dependencies:
  flutter_close_app: ^1.0.0
  • 导入包
import 'package:flutter_close_app/flutter_close_app.dart';

示例

  • 按两次返回键退出应用

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FlutterCloseAppPage(
      onCloseFailed: () {
        // Condition does not match: the first press or the second press interval is more than 2 seconds, display a prompt message
        ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
          content: Text('Press again to exit ?'),
        ));
      },
      child: Scaffold(
        appBar: AppBar(),
        body: ...
      ),
    );
  }
}
  • 一键退出应用
FlutterCloseApp.close();
  • 高级用法

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FlutterCloseAppPage(
+      interval: 3, // Custom interval is 3 seconds
+      condition: tabIndex == 0, // Only close the app on the home page
      onCloseFailed: () {
        // Not the homepage does not prompt
+        if(tabIndex != 0) return;
        // Condition does not match: the first press or the second press interval is more than 2 seconds, display a prompt message
        ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
          content: Text('Press again to exit ?'),
        ));
      },
      child: Scaffold(
        appBar: AppBar(),
        body: ...
      ),
    );
  }
}

问题和反馈

请提交 issues 来发送反馈或报告错误。谢谢!

许可

GitHub

查看 Github