Flutter WidgetsScaffold

Allow Users to press the back button twice to close the App in Flutter

Your user have to click the back button twice before they can close the application

You want your users to always double click the back button before they can leave/close the app? Here’s an easy way to do that in Flutter; We use a Flutter plugin from pub.dev which is called double_back_to_close_app. Why do developers use the “double-tap back twice” feature in applications? Most time user mistakenly press their back button and close their app unintentionally. That makes this feature important in some kind of mobile applications.

PS: The feature that allows user to click back twice before exiting an application only works on the Android platform, and not on the iOS platform.

This Flutter package allows your users to click the back button twice before they can leave/close the application. This is a tutorial for an application which was built with Flutter

Let’s see how this work.

Install the double_back_to_close_app Packages in Pubspec.yaml

Go to your pubspec.yaml file in your project and add the double_back_to_close_app package below your dependency, the same way it is shown below.

dependencies:
  flutter:
    sdk: flutter
  double_back_to_close_app: ^recent version

How to use the double_back_to_close_app

Import your package to the main.dart, and apply your code. See Usage below;

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

void main() => runApp(Example());

class Example extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: DoubleBackToCloseApp(
          snackBar: const SnackBar(
            content: Text('Tap back again to exit'),
          ),
          child: Center(
            child: OutlineButton(
              child: const Text('Tap to simulate back'),
              // ignore: invalid_use_of_protected_member
              onPressed: WidgetsBinding.instance.handlePopRoute,
            ),
          ),
        ),
      ),
    );
  }
}

I hope you were able to understand this guide on how you can allow your users to click the back button of their android phone, before they can close the application.

Feel free to drop a comment below if you had any error while following this tutorial.

GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *

Adblock Detected - We work so hard to bring You great content-- Please disable Adblocker to browse our site

It seems you're using an Adblocker, Please disable it to get this access to this file.