SoFunction
Updated on 2025-04-07

Detailed explanation of the navigation and routing examples of flutter

Navigation and routing

Flutter provides a complete system for navigating and handling deep links between screens. Small applications without complex deep links are availableNavigator, and applications with specific deep linking and navigation requirements should also useRouterTo handle it correctlyAndroidandiOSDeep links on the app and keeps synchronized with the address bar when the app runs on the web.

Navigator

NavigatorThe navigation group can use correct transition animation to display the corresponding interface. Of course, similar to the routing on the web, the interface is actuallyStackThe form is preserved.

By routedbuildContextContext and call the correspondingpush()orpop()Method, we can navigate to a new interface, such as:

onPressed: () {
  (context).push(
    MaterialPageRoute(
      builder: (context) => const SongScreen(song: song),
    ),
  );
},
child: Text(),

becauseNavigatorSaved oneRouteThe stack of the object (represents the history stack), sopush()Methods are also usedRouteObject as parameter.MaterialPageRouteThe object isRoutesubclass ofMaterial Designtransition animation.

Named routes

For some applications with simple navigation and deep link requirements, we can useNavigatorNavigate and useMaterialAppThe object'sroutesProperties configure routes:

@override
  Widget build(BuildContext context) {
  return MaterialApp(
    routes: {
      '/': (context) => HomeScreen(),
      '/details': (context) => DetailScreen(),
    },
  );
}

The route we configure here isNamed routes

Limitations of named routes

Although named routes can handle deep links, their performance is always consistent and there is no way to customize them. When the application platform receives a new deep link, Flutter will push the new route to the navigation device no matter where the user is at this time.

Flutter apps that use named routes do not support the browser's forward button. For these reasons, the official actually does not recommend using named routes in most applications.

Of course, during the actual development process, we need to make adjustments based on actual conditions.

Using Router

Flutter applications with advanced navigation and routing requirements (e.g. web applications that use direct links to each screen, or have multiple, or nested navigationNavigatorComponents' application) should use such asgo_routerA routing package like this that can resolve the routing path and configure it when the application receives a new deep linkNavigator

To use the route, we need to switch toMaterialApporCupertino Appand provide router configuration for it.

(
  routerConfig: GoRouter(
    // …
  )
);

Becausego_routerSuch packages are declarative, so when deep links are received, they will always display the same interface.

Use both Router and Navigator

RouterandNavigatorWorks in concert at design time. We can use likego_routerSuch a routing package API can also be usedNavigatorofpush()orpop()Method for navigation.

When we useRouteror when navigating a declarative routing package,NavigatorEvery routing page on it is supported. This means that the route uses the parameters on the page according to the pageNavigatorThe route created by the constructor.

Instead, by calling()A route navigation for methods such as thepageless(No page) routing. If we are using routing packages, the routes supported by the page are always deeply linked, while those without pages are not.

When a page supports route is deleted from the navigator, all pageless routes after it will also be deleted. For example, if the deep link is navigated by removing the supported routes from the navigator, all pageless routes afterwards (until the next _pagebacked route) will also be deleted.

Deep linking

Flutter supports deep links on iOS, Android and web browsers. Opening the URL will display the screen in the application. With the following steps, we can start and display the route using a named routes parameter (using the routes parameter or onGenerateRoute) or using the Router widget.

If we run the application in a web browser, no additional settings are required. The routing path is handled the same way as in-depth links for iOS or Android. By default, web applications use mode:/#/path/to/app/screenRead the deep link path from the url fragment, but this can be changed by configuring the application's url policy.

Enable Deep Linking on Android

Just need to be inIn the configuration file<activity>Add a metadata tag and intent filter tag to the tag:

<!-- Deep linking -->
<meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
<intent-filter android:autoVerify="true">
    <action android:name="" />
    <category android:name="" />
    <category android:name="" />
    <data android:scheme="http" android:host="" />
    <data android:scheme="https" />
</intent-filter>

After configuration, just restart the entire application.

Enable Deep Linking on ios

Need to be inios/RunnerUnder the folderAdd two new keys to the file:

<key>FlutterDeepLinkingEnabled</key>
<true/>
<key>CFBundleURLTypes</key>
<array>
    <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string></string>
    <key>CFBundleURLSchemes</key>
    <array>
    <string>customscheme</string>
    </array>
    </dict>
</array>

CFBundleURLNameis a unique URL that distinguishes our application from other applications that use the same scheme.

After the configuration is completed, the application restart is also required.

Configuring URL policies on the web

The flutter web application supports two URL policies:

  • hash mode. like:/#/path/to/screen.
  • path mode. like:/path/to/screen.

It's very simple to configure, fromflutter_web_pluginsPlugin library importusePathUrlStrategyThe method is called in the entry function.

import 'package:flutter_web_plugins/url_strategy.dart';
void main() {
  usePathUrlStrategy();
  runApp(ExampleApp());
}

PathUrlStrategy uses the history API, which requires additional configuration of the web server. How to configure it should be related to nginx~

at last

After getting familiar with navigation and routing, deepening your understanding and memory in the components, and then getting familiar with the interface request method, you can basically start developing flutter applications~

The above is the detailed explanation of flutter navigation and routing examples. For more information about flutter navigation routing, please follow my other related articles!