Everything You Need to Know About Navigation and Routing in Flutter

Everything You Need to Know About Navigation and Routing in Flutter

What is Navigation and Routing in Flutter?

In the context of Flutter, navigation refers to the process of switching between different screens or pages within an application, while routing involves managing the flow and organisation of these screens. Let’s delve into the details:

Benefit of do routing and navigation in flutter:

  1. Screen Organisation and Flow:

    • Routing allows you to structure your app into different screens or pages. Each screen represents a distinct part of your app’s functionality.

    • By navigating between screens, users can follow a logical flow within the app. For example, moving from a login screen to a dashboard or from a product list to a product details page.

  2. Modularity and Separation of Concerns:

    • Routing encourages a modular approach to building apps. Each screen can be encapsulated as a separate widget or class.

    • Separating screens into smaller components improves code organisation and maintainability. It also enables better collaboration among team members.

  3. User Experience (UX):

    • Navigation provides a seamless experience for users. They can easily switch between different parts of the app without losing context.

    • Well-designed navigation enhances user satisfaction and engagement.

  4. Deep Linking and URL Handling:

    • Routing enables deep linking, allowing users to directly access specific screens within the app via URLs.

    • Deep links are useful for sharing content, handling notifications, and improving user acquisition.

  5. State Preservation:

    • When navigating between screens, Flutter preserves the state of widgets on the stack.

    • This means that user input, scroll positions, and other widget states are retained when returning to a previous screen.

  6. Custom Transitions and Animations:

    • You can customise the transition animations between screens using Navigator.

    • For example, sliding, fading, or scaling effects can enhance the visual appeal of your app.

  7. Named Routes for Clarity:

    • Named routes provide a clear and descriptive way to define navigation paths.

    • Developers and designers can easily understand the app’s structure by looking at the route names.

  8. Handling Back Navigation:

    • Navigator automatically handles the back button or swipe gestures for going back to the previous screen.

    • This behaviour ensures a consistent user experience across different platforms.

  9. Advanced Routing Scenarios:

    • For complex apps, you can use third-party routing packages like go_router to handle dynamic routes, nested navigation, and more.

Ways to do Navigation and routing in Flutter

Certainly! In Flutter, there are three main ways to handle routing and navigation:

  1. Direct Navigation withMaterialPageRoute

  2. Static Navigation with Named Routes

  3. Dynamic Navigation with Generated Routes

1.Direct Navigation with MaterialPageRoute:

  • The most straightforward method involves using the Navigatorwidget.

  • You can navigate between screens by calling imperative methods like push() or pop().

  • For example

      onPressed: () {
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) => const UserScreen(user: user),
          ),
        );
      }
    

The MaterialPageRoute specifies transition animations for Material Design.

Pros of MaterialPageRoute:

  1. Simplicity and Built-in Transitions:

    • MaterialPageRoute is straightforward to use. You define named routes, and it handles navigation between screens.

    • It provides built-in Material Design transitions (e.g., slide, fade) when navigating between pages.

  2. Centralised Instantiation:

    • When using named routes with MaterialPageRoute, you keep page instantiation centralised.

    • This avoids code duplication and makes your app cleaner and more maintainable.

  3. Integration with Material Design:

    • MaterialPageRoute aligns seamlessly with Material Design guidelines.

    • It ensures consistency in navigation animations and visual cues for Android apps.

Cons of MaterialPageRoute:

  1. Limited Customisation:

    • While MaterialPageRoute provides basic transitions, it might not cover all custom animation needs.

    • If you require more complex or unique transitions, you’ll need to extend or create your own route.

  2. iOS-Specific Behaviour:

    • On iOS devices, MaterialPageRoute introduces some iOS-like sliding animations in the background.

    • If you want platform-specific behaviour, this might not be ideal.

  3. Not Ideal for All Scenarios:

    • For more advanced routing requirements (e.g., deep linking, dynamic routes), MaterialPageRoute alone may not suffice.

    • Consider other options like PageRouteBuilder or third-party routing packages.


2.Static Navigation with Named Routes:

  • Named routes provide a declarative way to define navigation paths.

  • Define named routes in the routes parameter of MaterialApp:

  • For example

      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          routes: {
            '/': (context) => HomeScreen(),
            '/dashboard': (context) => DashboardScreen(),
          },
        );
      }
    
  • However, named routes have limitations and may not suit all applications.

Pros of Named Routes:

  1. Faster Navigation for Frequently Accessed Routes:

    • Named routes allow you to navigate directly to a specific screen without pushing it onto the navigation stack.

    • This results in faster navigation for frequently accessed routes, as there’s no need to rebuild the entire stack.

  2. Simpler Code Structure for Easier Maintenance:

    • By defining named routes, you create a clear mapping between route names and their corresponding screens.

    • This simplifies your codebase and makes it easier to maintain and understand.

  3. Quicker App Startup When Routes Are Limited:

    • When using named routes, the app initialises only the necessary routes during startup.

This can lead to quicker app launch times, especially when you have a limited Number of routes.

Cons of Named Routes:

  1. Impact on Startup Time with Many Routes:

    • If your app has an extensive set of named routes, it can impact the app’s startup time.

    • The more routes you define, the longer it takes to initialise the navigation structure.

  2. Less Flexible for Dynamic Route Generation:

  • Named routes are static and defined upfront in your app.

  • If you need dynamic route generation (e.g., based on user data or external factors), named routes may not be the best fit.


3. Dynamic Navigation with Generated Routes:

  1. Route Generation Based on Data:

    • Instead of hardcoding routes, you can generate them dynamically based on data from your app.

    • For example, if you have a list of reports, each with a unique ID, you can create routes for individual report dynamically.

    • Use a function to generate the route based on the report ID:

        String generateReportRoute(String reportId) {
          return '/report/$reportId';
        }
      

UsingonGenerateRoute Callback:

  • In your MaterialApp, set the onGenerateRoute callback:

      MaterialApp(
        onGenerateRoute: (settings) {
          if (settings.name.startsWith('/report/')) {
            final reportId = settings.name.split('/').last;
            return MaterialPageRoute(
              builder: (context) => ReportScreen(reportId: reportId),
            );
          }
          // Handle other routes here...
        },
      )
    
    • When a route matches the pattern (e.g., /report/123), create the corresponding screen dynamically.
  • Passing Parameters to Screens:

    • When navigating to dynamically generated routes, pass any necessary parameters (e.g., report ID) to the screen.

    • Extract the parameters from the route settings and use them to populate the screen content.

  • Deep Linking and Query Parameters:

    • You can also handle deep linking by parsing query parameters from the route.

    • For example, if your route is /search?query=flutter, extract the query parameter to perform a search.

Pros of Dynamic Navigation and Generated Routes:

  1. Adapts to Network Changes:

    • Dynamic routing automatically adjusts routes when the network changes, such as when new devices connect or existing ones move. This ensures data flows smoothly even in a changing environment.
  2. Balances Traffic Loads:

    • By spreading data across multiple pathways, dynamic routing prevents any single link from becoming congested. This load balancing improves overall network performance.
  3. Chooses the Best Path:

    • Dynamic routing evaluates all possible routes and selects the most efficient one for data to travel. This optimisation results in a quicker and more reliable network.
  4. Reduces Manual Configuration:

    • Dynamic routing reduces the need for network administrators to manually set up routes. It saves time, minimises human error, and simplifies maintenance.
  5. Improves Fault Tolerance:

    • When issues occur (e.g., link failure), dynamic routing quickly finds alternative paths. This fault tolerance helps keep the network operational.

Cons of Dynamic Navigation and Generated Routes:

  1. Increased Network Complexity:

    • Dynamic routing can make the network harder to understand and manage because it constantly changes the paths data takes. Administrators must adapt to these changes.
  2. Potential Security Vulnerabilities:

    • If not secured properly, dynamic routing might expose the network to risks. Routes are shared and updated automatically, which could lead to security vulnerabilities.
  3. Higher Resource Consumption:

    • Routers using dynamic routing require more memory and processing power. This increased resource consumption can make the network more expensive to operate.
  4. More Difficult Troubleshooting:

    • Diagnosing issues becomes challenging because dynamic routes can change. Identifying problems and pinpointing their causes may take more effort.
  5. Slower Convergence Times:

    • After network changes, routers need time to agree on the best paths. During this convergence process, the network might experience slowdowns.

Deferent Navigator methods in Flutter

  1. push and pop:

    • push: Adds a new route to the top of the stack, navigating to a new screen.

      • Use when moving from one screen to another.

      • Example: Navigating from a login screen to a home screen.

    • pop: Removes the top route from the stack, returning to the previous screen.

      • Use when going back to the previous screen.

      • Example: Going back from a details screen to a list screen.

        // Push to a new screen
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) => const DetailsScreen(),
          ),
        );

        // Pop back to the previous screen
        Navigator.of(context).pop();
  1. pushNamed and pop:

    • pushNamed: Navigates to a named route (previously defined in your app).

      • Use when you have named routes (e.g., ‘/home’, ‘/profile’).

      • Example: Navigating to the profile screen using its route name.

    • pop: Removes the top route from the stack (similar to the previous case).

        // Navigate to a named route
        Navigator.pushNamed(context, '/profile');
      
        // Go back to the previous screen
        Navigator.pop(context);
      
  2. pushReplacement:

    • Replaces the current route with a new one.

    • Useful for scenarios like login/logout flows.

    • Example: After successful login, replace the login screen with the home screen.

        // Replace the current screen with a new screen
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => const NewHomeScreen(),
          ),
        );
      
  3. popUntil:

    • Pops routes until a specified condition is met.

    • Useful for navigating back to a specific screen.

    • Example: Popping all screens until reaching the home screen.

        // Pop until reaching the home screen
        Navigator.popUntil(context, ModalRoute.withName('/home'));
      
  4. pushAndRemoveUntil:

    • Pushes a new route and removes all previous routes until a condition is met.

    • Useful for resetting the navigation stack.

    • Example: After successful registration, push the home screen and remove all previous screens.

        // Push home screen and remove all previous screens
        Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(builder: (context) => const HomeScreen()),
          (route) => false, // Remove all previous routes
        );
      
  5. pushReplacementNamed:

    • Similar to pushReplacement, but navigates to a named route.

    • Replaces the current route with the specified named route.

    • Example: After successful authentication, replace the login screen with the dashboard screen.

        // Replace the current screen with a named route
        Navigator.pushReplacementNamed(context, '/dashboard');
      

Remember to choose the appropriate method based on your app’s flow and requirements. Each method serves a specific purpose, allowing you to create seamless navigation experiences for your users!

Did you find this article valuable?

Support JoFlee by becoming a sponsor. Any amount is appreciated!