Flutter GetX Routing implementation

Flutter is a popular open-source framework for building high-performance, beautiful, and responsive mobile apps for both Android and iOS. One of the key components of building an app with Flutter is managing navigation between screens. In this article, we will be exploring GetX, a third-party routing library for Flutter that makes it easy to manage navigation in your app.

GetX is a reactive and optimized Flutter routing library that offers a powerful and intuitive navigation solution for Flutter apps. With GetX, you can quickly and easily navigate between screens, pass data between screens, and manage the navigation stack with ease.

So, how does GetX make navigation in Flutter easier? Let’s start by taking a look at an example of how to use GetX to manage navigation in a Flutter app.

Dependency Needed:

<a href="https://pub.dev/packages/get" target="_blank" rel="noreferrer noopener">get:</a>

Here’s an example of how you can use GetX routes in Flutter:


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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'GetX Example',
      initialRoute: '/',
      getPages: [
        GetPage(
          name: '/',
          page: () => HomePage(),
        ),
        GetPage(
          name: '/about',
          page: () => AboutPage(),
        ),
        GetPage(
          name: '/contact',
          page: () => ContactPage(),
        ),
      ],
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                Get.toNamed('/about');
              },
              child: Text('Go to About Page'),
            ),
            SizedBox(
              height: 20,
            ),
            RaisedButton(
              onPressed: () {
                Get.toNamed('/contact');
              },
              child: Text('Go to Contact Page'),
            ),
          ],
        ),
      ),
    );
  }
}

class AboutPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('About Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                Get.back();
              },
              child: Text('Go Back'),
            ),
          ],
        ),
      ),
    );
  }
}

class ContactPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Contact Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                Get.back();
              },
              child: Text('Go Back'),
            ),
          ],
        ),
      ),
    );
  }
}

As you can see, using GetX routes is as simple as defining a list of GetPage objects in your GetMaterialApp widget. Each GetPage object has a name and a page, which is a StatelessWidget that represents the UI for that page.

In this example, we have three pages: the HomePage, the AboutPage, and the ContactPage. To navigate between these pages, we use the Get.toNamed method and pass in the name of the page we want to navigate to. For example, to navigate from the HomePage to the AboutPage, we use Get.toNamed('/about'). To go back from the AboutPage or the ContactPage, we use Get.back().

One of the great things about GetX is that it provides a reactive navigation solution, which means that navigation updates are automatically propagated throughout your app. This makes it easy to update your UI whenever the navigation stack changes.

Other articles or tutorials you may like

GetX also makes it easy to pass data between screens. You can use the Get.arguments method to pass data when navigating to a page, and then access this data in the page that you are navigating to. For example, let’s say you want to pass a message from the HomePage to the AboutPage. You can do this by passing the message as an argument when navigating to the AboutPage:

RaisedButton(
      onPressed: () {
        Get.toNamed('/about', arguments: 'Hello from HomePage');
      },
      child: Text('Go to About Page'),
    ),

Then, in the AboutPage, you can access the message like this:

class AboutPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final message = Get.arguments as String;
    return Scaffold(
      appBar: AppBar(
        title: Text('About Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(message),
            RaisedButton(
              onPressed: () {
                Get.back();
              },
              child: Text('Go Back'),
            ),
          ],
        ),
      ),
    );
  }
}

One of the key benefits of using GetX over Flutter’s built-in routing methods is that GetX provides a more reactive approach to navigation. With GetX, navigation updates are automatically propagated throughout your app, making it easy to update your UI whenever the navigation stack changes. For example, you can use GetX’s observer method to observe changes in the navigation stack and update your UI accordingly:

Get.observer(
  builder: (_) {
    return Text(Get.isFirstRoute(0) ? 'First Page' : 'Second Page');
  },
);

In contrast, with Flutter’s built-in routing methods, you need to manually manage navigation updates and update your UI in response to changes in the navigation stack.

GetX also provides a number of other convenient features that are not available with Flutter’s built-in routing methods. For example, GetX makes it easy to pass data between screens, with the Get.arguments method. This allows you to pass data to a new page when you navigate to it, and access this data in the new page. For example, to pass data to a new page using GetX, you might write something like this:

Get.to(SecondPage(), arguments: 'Hello, World!');

And then access this data in the new page using the Get.arguments method:

String message = Get.arguments;

One of the most useful methods provided by GetX is the Get.off method, which allows you to pop one or more Routes from the navigation stack. For example, to pop the current Route and return to the previous page, you might write something like this using GetX:

Get.off(1);

Another useful method provided by GetX is the Get.offAll method, which allows you to pop all Routes from the navigation stack, effectively resetting the navigation to the first page. For example, to reset the navigation to the first page using GetX, you might write something like this:

Get.offAll();

With Flutter’s built-in routing methods, you can perform similar actions using the Navigator widget. To pop one or more Routes from the navigation stack, you might write something like this:

Navigator.pop(context, result: true);

To reset the navigation to the first page using Flutter’s built-in routing methods, you would need to manually clear the navigation stack. For example, you might write something like this:

Navigator.popUntil(context, ModalRoute.withName(Navigator.defaultRouteName));

GetX also provides a number of other useful navigation methods, including the Get.back method, which allows you to navigate back to the previous page, and the Get.find method, which allows you to find a specific page in the navigation stack.

For example, to navigate back to the previous page using GetX, you might write something like this:

Get.back();

To find a specific page in the navigation stack using GetX, you might write something like this:

Get.find<SecondPage>().then((route) {
  // Do something with the route
});

Flutter’s built-in routing methods include the use of the Navigator widget, which provides a stack-based navigation system. The Navigator widget maintains a stack of Route objects, where each Route represents a page or screen in your app. You can push a new Route onto the stack to navigate to a new page, or pop the top Route to go back to the previous page. For example, to navigate to a new page using Flutter’s built-in routing methods, you might write something like this:

Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => SecondPage()),
);

GetX, on the other hand, provides a more reactive and optimized approach to routing. It uses a GetPage object to represent each page in your app, and you can navigate between these pages using the Get object. GetX provides a number of convenient methods for navigating, including Get.to and Get.offAll, which allow you to navigate to a new page or pop the entire navigation stack, respectively. For example, to navigate to a new page using GetX, you might write something like this:

Get.to(SecondPage());

In conclusion, GetX provides a rich and convenient set of navigation methods that can make your life as a Flutter developer much easier. Whether you’re building a simple app with a few pages, or a complex app with many screens, GetX is definitely worth considering as your go-to routing library.

Previous Post

Leave a Reply

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