How to Use SystemNavigator.pop() to Exit App in Flutter?
Last Updated :
30 May, 2022
In many applications, we need force to close the entire application. So in this article, we are going to do the same. We can use the SystemNavigator.pop() to exit the application. There is no need for additional packages.
How to Use?
Import the library
First, we need to import the service package.
Dart
import 'package:flutter/services.dart' ;
|
Syntax
We will use the Text Button at to call the Navigator pop method that will end the user screen.
Code Example
Dart
import 'package:flutter/material.dart' ;
import 'package:flutter/services.dart' ;
void main() {
runApp(ExitAppRun());
}
class ExitAppRun extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false ,
title: 'ExitApp' ,
home: Scaffold(
appBar: AppBar(
title: Text( 'Exit App' ),
),
body:Center(
child:TextButton.icon(
icon:Icon(Icons.close),
label:Text( 'Exit App' ),
onPressed:(){
SystemNavigator.pop();
},
),
),
),
);
}
}
|
Output UI
Code Explanation
- Main Is The Principal Method Used To Run ExitAppRun Class When The Page Is Loaded.
- Creating Class ExitAppRun, Stateless As There Is No Data Change After The Page Is Loaded (No State Change).
- As Flutter Is Based On Widgets, We Need To Create One.
- Creating A Material App That Takes Scaffold Allowing Us To Use AppBar And Body.
- As An AppBar It Has An Simple Title.
- As An Body, It Takes A Centered Button Once Pressed Will Close The App!
Output
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...