Basic Routing with Flutter Navigator 2.0 Example



Did you know?

The Flutter team launched Navigator 2.0 to manage complicated routing. It is because Navigator 1.0 was only capable of adding and deleting pages from stacks. This made it difficult to handle deep linking or URI parsing for Flutter Web.

But can Navigator 2.0 really solve this problem?

For that, we need to understand Flutter Navigator 2.0 in greater detail. It will also make the process of Flutter app development simpler for you in the future.

Overview of Flutter Navigator!


Navigator 1.0 is an imperative API. In contrast, flutter’s Navigator 2.0 operates mostly in an imperative manner. Yet, it has a declarative API.

In the case of Navigator 1.0, there was a stack through which one could push or pop pages. It was pretty and convenient. But it offered these benefits until the application had simple routing. Simple Routing consisted of the use of fewer screens and used on the web.

The issue became grave when users had to create complicated apps. Implementation of “deep linking,” going straight to a product page from alerts, handling O.S. events, and parsing URIs for the handling of the Flutter web back button follow.

Why should you choose Flutter Navigator 2.0?


Then Navigator 2.0 is released. It is of a descriptive nature, giving you greater control over the Navigation stack. Users can manipulate the stack easily. However, it depends entirely on the state of our app and response to back button events sent by the OS.

Further, it supplies a list of pages and does URI parsing utilizing routeInformationParser and provider. You can use this to not only PUSH and POP from the top but from any place you wished to.

Flutter Navigator 2.0 Example


Navigator 2.0 introduces additional classes. It makes it easier to implement the app screen as a byproduct of the app state. It also enables the parsing of routes from URLs for flutter web. According to Flutter app developers in Denver, Navigator 2.0 offers a new declarative API architecture:

  • Pages:

It is for the existing navigator. The pages which are employed to set the history stack are immutable and must be added to the navigation history stack as listed there.

  • Router:

It is a brand-new widget that envelops the navigator and customizes the current list of sites according to the app state. Additionally, it has the ability to listen to operating system events and adjust the navigation accordingly.

We will basically create a Flutter application and learn how to add pages and routers using Flutter Navigator 2.0 as our example.

The following list of four classes includes:

  • RouterInformationParser
  • RouterDelegate
  • RouterInformationProvider
  • BackButtonDispatcher

These can be further worked on based on our example:

  • Anime Characters screen: It displays a list of all the characters from anime.
  • Characters Details screen: When a specific character is chosen, a new screen that shows the anime to which that character belongs is added onto the stack.
  • Anime Char Model: This class serves as merely a model for anime characters, complete with function Object () { [native code] }, name, and anime.
  • App class: This straightforward class manages all navigation and app state monitoring while managing Material widgets.

Let’s now configure our pages inside the app state. Start by creating a variable to track the application state after that, and then display the screen in accordance with the state.

Next, we have configured our pages so that MaterialPage is passed as a child for each page during custom routing. Further, we made a customRoute.dart for the Character details screen as an example. It demonstrates that we can also customize pages to suit our needs.

  • onPopPage: This function is used to make sure that nothing unexpected happens when POP is being done. A POP operation returns true if it is successful and false otherwise.
  • List<> _selectedChar: It is used to track the application’s status in order to display the character details screen.
  • animeChars: It contains a list of anime characters that is supplied to the Anime characters screen.

It’s quite interesting to know how to integrate Navigator 2.0 into our Flutter application. We have spoken about Navigator 2.0’s fundamental implementation up to this point. All the classes’ connection makes up the remaining code. You are welcome to play around with the source code by cloning the full application. You can also check out the Google Dev Fest celebrating the release of Navigator 2.0.

We have just recently begun to handle routes declaratively that are depending on app states and not something we direct the app to perform up till this point. What is now basically left is the flutter web portion, which parses the URL to go directly to certain pages and vice versa.

Wrapping Up!


This is what exactly you should know about Flutter Navigator 2.0 and also how to use it in your project to build basic routing. We anticipate that the lesson will assist you in beginning the process of adding navigation to the application. You can also hire Flutter app developers in USA if you’re willing to clone a project or learn more about it.

Article Resource - https://zimblecode.com/basic-routing-with-flutter-navigator-2-0-example/

Comments

Popular posts from this blog

Must-Have Features to Build a Trucking Dispatch Software

Must-Have Features to Build a Bus Ticket Booking App in 2023