Open In App

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:

flutter create APPNAME

import 'package:get/get.dart';

Example 1:






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("GeekforGeeks 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("GeekforGeeks 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:




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("GeekforGeeks 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("GeekforGeeks 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.


Article Tags :