Flutter – Navigation to Previous Screen using GetX Library Function
When we are using any app then we do navigation to navigate between screens. Sometimes we want to return to the previous screen so we normally use Navigator.pop(context). This is using context and sometimes we find shortcuts to do the task easily. For that, we have Get.back() in flutter. We can send the result to the previous route and do the operations.
Syntax:
Get.back()
Implementation:
- Create a new flutter application.
flutter create APPNAME
- Add get dependency in pubspec.yaml file in dependency section.
- Import the package in main.dart.
import 'package:get/get.dart';
- Now, write code to implement Get.back(). For that, we should have two screens and first, we navigate to a new screen and get back to the previous screen with some data.
Example 1:
Dart
import 'package:flutter/material.dart' ; import 'package:get/get.dart' ; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return GetMaterialApp( title: 'Flutter Demo' , theme: ThemeData( primarySwatch: Colors.blue, ), home: Page1(), debugShowCheckedModeBanner: false , ); } } class Page1 extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "GeekforBeginner GFG" ), centerTitle: true , backgroundColor: Colors.green, ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "Page 1" , textScaleFactor: 2,), Container( child: ElevatedButton( child: Text( "Navigate to next screen" ), onPressed: () { Get.to(Page2()); } ), ), ], ), ), ); } } class Page2 extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "GeekforBeginner GFG" ), backgroundColor: Colors.green, ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "Page 2" , textScaleFactor: 2,), Container( child: ElevatedButton( child: Text( "Navigate to previous screen" ), onPressed: ()=> Get.back() ), ), ], ), ), ); } } |
Output: In this, we are not sending any status get after returned back. We are simply getting back using Get.back().
Example 2:
Dart
import 'package:flutter/material.dart' ; import 'package:get/get.dart' ; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return GetMaterialApp( title: 'Flutter Demo' , theme: ThemeData( primarySwatch: Colors.blue, ), home: Page1(), debugShowCheckedModeBanner: false , ); } } class Page1 extends StatefulWidget { @override _Page1State createState() => _Page1State(); } class _Page1State extends State<Page1> { String? x; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "GeekforBeginner GFG" ), centerTitle: true , backgroundColor: Colors.green, ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "Page 1" , textScaleFactor: 2,), Container( child: ElevatedButton( child: Text( "Navigate to next screen" ), onPressed: () async{ x= await Get.to(Page2()); setState(() { }); } ), ), Text(x?? x.toString(), textScaleFactor: 2,), ], ), ), ); } } class Page2 extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "GeekforBeginner GFG" ), backgroundColor: Colors.green, ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "Page 2" , textScaleFactor: 2,), Container( child: ElevatedButton( child: Text( "Navigate to previous screen" ), onPressed: ()=> Get.back( result: "Data after returning to first page" ) ), ), ], ), ), ); } } |
Output: In this, we are sending results back to the homepage using Get.back( result: “Data after returning to first page”). And receiving result using x= await Get.to(Page2()); in First page.