Press Back Again to Exit Android

double press back button to exit app
printing back button again to get out the app

Hi Guys, Welcome to Proto Coders Bespeak.

At that place are some flutter app that has a characteristic similar app exit when user double press back button.

When back buton is pressed for outset fourth dimension a toast message or a snackbar popup tin can be shown to user saying 'press back button again to exit the app', Then immediatly in a seconds, if back button is pressed again, your flutter app will Leave.

So, In this flutter tutorial, we volition learn how to implement "Press dorsum button to exit" in flutter, too called as double dorsum press to exit app.

Nosotros volition brand use of WillPopScope Widget to acheive this.

WillPopScope widget is used to detect when user press the back push.

Then, whenever a user press back button, you will get a callback at onWillPop function, which return Futurity value i.e. Either Truthful or Fake.

If it returned value is true, the screen will be popped and leave the .

Syntax:

WillPopScope(       onWillPop: () async{           //callback role when back button is pressed       },       child: Scaffold(), );

Here if onWillPop() async function return truthful, then yous flutter app will go exited.


How to Implement Double back printing to go out in flutter

1. Simply Wrap your Scaffold Widget with WillPopScope Widget.

Snippet Instance

WillPopScope(       onWillPop: () async{           //callback office when back push button is pressed       },       child: Scaffold(), );

so now whenever your palpitate app user press dorsum push, WillPopScope volition notice information technology and send the a callback to onWillPop() role in information technology.

Note: if onWillPop render            true          that ways information technology will close the screen i.due east. dorsum printing exit the app.

ii. How to implement back printing again to exit flutter app

To implement press back button again in palpitate, we will make use of DateTime.now(), DateTime.at present() will return electric current time.

DateTime pre_backpress = DateTime.now();

So, We will create two fourth dimension variable & find the time gap betwixt both the time vairable.

concluding timegap = DateTime.now().difference(pre_backpress);  print('$timegap');

The above code will render the timegap betwixt them.

Now, let's initialize booleon variable, where will bank check if timegap elapsing is greater and so two seconds.

concluding cantExit = timegap >= Elapsing(seconds: 2);

if timegap is more than 2 2d we will fix 'cantExit' variable to true.

if cantExit = true; that means we can't exit app, so will show snackbar to user saying ''Press back button over again to Exit app"

if cantExit = imitation; i.e. Timegap between second dorsum press is less then 2 second, that means user tin can go out the app.

Complete Snippet lawmaking of onWillPop

onWillPop: () async{             concluding timegap = DateTime.now().difference(pre_backpress);             final cantExit = timegap >= Duration(seconds: ii);             pre_backpress = DateTime.now();             if(cantExit){              //show snackbar              final snack = SnackBar(content: Text('Printing Dorsum push again to Go out'),duration: Elapsing(seconds: ii),);               ScaffoldMessenger.of(context).showSnackBar(snack);               return fake; // false will practice nothing when back press            }else{              return true;   // truthful will get out the app            }        },

Complete Source Code – Implement double back press to leave palpitate app

main.sprint

import 'parcel:palpitate/material.dart'; import 'package:passing_data/FruitDataModel.dart'; import 'packet:passing_data/FruitDetail.dart';  void main() {   runApp(MyApp()); }  class MyApp extends StatelessWidget {   // This widget is the root of your awarding.   @override   Widget build(BuildContext context) {     return MaterialApp(       title: 'Flutter Demo',       theme: ThemeData(         primarySwatch: Colors.blue,       ),       abode: HomePage(),       debugShowCheckedModeBanner: fake,     );   } }  class HomePage extends StatelessWidget {      DateTime pre_backpress = DateTime.at present();   @override   Widget build(BuildContext context) {     render WillPopScope(       onWillPop: () async{         final timegap = DateTime.at present().deviation(pre_backpress);          terminal cantExit = timegap >= Elapsing(seconds: 2);          pre_backpress = DateTime.now();          if(cantExit){           //prove snackbar           final snack = SnackBar(content: Text('Press Dorsum push button again to Exit'),elapsing: Elapsing(seconds: 2),);            ScaffoldMessenger.of(context).showSnackBar(snack);           return false;         }else{           return true;         }       },       child: Scaffold(         body: Container(),       ),     );   } }

burkeagnight.blogspot.com

Source: https://protocoderspoint.com/flutter-double-back-press-to-exit-app/

0 Response to "Press Back Again to Exit Android"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel