搜索插件、代码仓与 APP 分类

double_back_to_close 封面
插件详情v2.0.0

double_back_to_close

用于在关闭应用/路由/屏幕之前请求双击返回的 Flutter 包。将任何组件包裹在其中即可使用。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
585
喜欢数
65
Pub 分数
140

安装插件

flutter pub add double_back_to_close

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 包,会在关闭应用、路由或屏幕前提示用户连续按两次返回按钮。它可包装任何控件以启用此行为,有助于防止意外退出。适用于移动和桌面应用,避免因无意导航而影响用户体验。该包支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,确保在各种设备上功能一致。

适合场景

  • 防止意外关闭应用
  • 提升移动应用的用户体验
  • 避免意外的路线取消
  • 保护关键工作流程
  • 确保有意的导航退出
  • 支持多平台应用程序

主要特点

  • 双击返回检测
  • 跨平台兼容性
  • 简单的 widget 包装
  • 可自定义的超时时间
  • 所需配置最少
  • 与路由和屏幕配合使用

适合对象

  • 移动应用开发人员
  • 桌面应用程序创建者
  • 以用户体验为中心的项目
  • 具有敏感工作流程的应用程序
  • 寻求简单退出保护的开发者
  • 跨平台 Flutter 应用

使用建议

  • 可能不适合所有UI模式
  • 需要用户了解双击行为
  • 超时设置可能需要调整
  • 不适合快速交互
  • 有限的自定义选项
  • 某些用户可能存在潜在的可访问性问题

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

double_back_to_close

Flutter package for request double back pressed before close app/route/screen.

Demo

Usage

Wrapping widget with DoubleBack where you want to use double back to close screen or app:

Default (using TOAST)
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DoubleBack(
          message:"Press back again to close",
          child: Home(),
        ),
      ),
  }
}
Default (using TOAST) with optional style
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DoubleBack(
          message:"Press back again to close",
          child: Home(),
        ),
      ),
      // optional style
      textStyle: TextStyle(
        fontSize: 13,
        color: Colors.white,
      ),
      background: Colors.red,
      backgroundRadius: 30,
    );
  }
}
Custom ( using snackbar for example)


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DoubleBack(
        onFirstBackPress: (context) {

          // you can use your custom here
          // change this with your custom action
          final snackBar = SnackBar(content: Text('Press back again to exit'));
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
          // ---

        },

        child: Home(),
      ),
    );
  }
}
with custom delay
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DoubleBack(
          onFirstBackPress: (context) {

            Flushbar(
              title: "Hey User",
              message: "Press back again to exit",
              duration: Duration(seconds: 15), // show 15 second flushbar
            )..show(context);

          },
          child: Home(),
          waitForSecondBackPress: 15, // wait for 15 second for second back pressed
        ),
    );
  }
}
with custom condition

if you want to show message at spesific condition, for example, only show message if pageView at index 0.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Imagine that you are using materialbottom and only want to close when tabIndex = 0

      home: DoubleBack(
          condition: tabIndex == 0, // only show message when tabIndex=0
          onConditionFail: (){
            setState((){
                tabIndex = 0; // if not 0, set pageview jumptopage 0
            });
          }
          child: Home(),
        ),
    );
  }
}

所属分类

源码仓库

使用此插件的项目

暂无相关数据。