home / skills / madteacher / mad-agents-skills / flutter-navigation

flutter-navigation skill

/flutter-navigation

This skill helps you implement Flutter navigation and routing with Navigator and go_router, enabling deep linking, data passing, and web history.

npx playbooks add skill madteacher/mad-agents-skills --skill flutter-navigation

Review the files below or copy the command above to add this skill to your agents.

Files (9)
SKILL.md
3.2 KB
---
name: flutter-navigation
description: Comprehensive guide for Flutter navigation and routing including Navigator API, go_router, deep linking, passing/returning data, and web-specific navigation. Use when implementing screen transitions, configuring routing systems, setting up deep links, handling browser history, or managing navigation state in Flutter applications.
---

# Flutter Navigation

## Overview

Implement navigation and routing in Flutter applications across mobile and web platforms. Choose the right navigation approach, configure deep linking, manage data flow between screens, and handle browser history integration.

## Choosing an Approach

### Use Navigator API (Imperative) When:
- Simple apps without deep linking requirements
- Single-screen to multi-screen transitions
- Basic navigation stacks
- Quick prototyping

Example: `assets/navigator_basic.dart`

### Use go_router (Declarative) When:
- Apps requiring deep linking (iOS, Android, Web)
- Web applications with browser history support
- Complex navigation patterns with multiple Navigator widgets
- URL-based navigation needed
- Production applications with scalable architecture

Example: `assets/go_router_basic.dart`

### Avoid Named Routes
Flutter team does NOT recommend named routes. They have limitations:
- Cannot customize deep link behavior
- No browser forward button support
- Always pushes new routes regardless of current state

## Common Tasks

### Pass Data Between Screens

**With Navigator:**
```dart
Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => DetailScreen(item: myItem)),
);
```

**With go_router:**
```dart
context.push('/details?id=123');
// Extract: final id = state.uri.queryParameters['id'];
```

Example: `assets/passing_data.dart`

### Return Data From Screens

```dart
final result = await Navigator.push(
  context,
  MaterialPageRoute<String>(builder: (context) => SelectionScreen()),
);
if (!context.mounted) return;
```

Example: `assets/returning_data.dart`

### Configure Deep Linking

**Android:** Configure `AndroidManifest.xml` intent filters
**iOS:** Configure `Info.plist` for Universal Links
**Web:** Automatic with go_router, choose URL strategy

For detailed setup: `references/deep-linking.md`

### Web URL Strategy

**Hash (default):** `example.com/#/path` - no server config needed
**Path:** `example.com/path` - cleaner URLs, requires server config

For server setup: `references/web-navigation.md`

## Navigation Methods

### go_router Navigation
- `context.go('/path')` - replace current route
- `context.push('/path')` - add to stack
- `context.pop()` - go back

### Navigator Navigation
- `Navigator.push()` - add route to stack
- `Navigator.pop()` - remove route from stack

## Advanced Topics

**Route Guards:** Implement authentication redirects
**Nested Routes:** Create shell routes with shared UI
**Error Handling:** Handle 404 and navigation errors
**Multiple Navigators:** Manage independent navigation stacks

For advanced patterns: `references/go_router-guide.md`

## Decision Guide

Use [navigation-patterns.md](references/navigation-patterns.md) for:
- Complete comparison of navigation approaches
- Deep linking behavior by platform
- Web-specific considerations
- Common patterns and anti-patterns

Overview

This skill is a practical guide for implementing navigation and routing in Flutter apps across mobile and web. It helps you choose between the Navigator API and go_router, set up deep links, manage data passing and returns, and handle browser history for web builds. The content focuses on actionable recommendations and real-world patterns for scalable navigation.

How this skill works

The guide explains imperative navigation with the Navigator API and declarative routing with go_router, showing when each approach fits. It covers passing and returning data between screens, configuring deep linking for Android, iOS, and web, and managing web URL strategies and browser history. Advanced topics include route guards, nested routes, error handling, and coordinating multiple Navigator widgets.

When to use it

  • Use Navigator API for small apps, simple stacks, or quick prototypes without deep linking.
  • Use go_router for apps that need deep links, URL-based routing, or web browser history support.
  • Choose path-based web URLs for clean links when you can configure the server; use hash URLs otherwise.
  • Apply nested routes or multiple Navigators when parts of the UI need independent navigation stacks.
  • Add route guards when you need authentication redirects or conditional access to routes.

Best practices

  • Prefer go_router for production apps that require deep linking and consistent URL behavior across platforms.
  • Avoid named routes for complex apps—named routes limit deep-link customization and browser back/forward behavior.
  • Pass data via constructor parameters or query parameters; validate and parse parameters on route entry.
  • Always check context.mounted after awaiting navigation results before updating state.
  • Handle unknown routes or errors with a dedicated 404/error route and user-friendly messages.

Example use cases

  • A mobile app with a few screens and simple back-stack navigation using Navigator.push/pop.
  • A web-first app that needs readable URLs, deep links, and browser history using go_router with path URL strategy.
  • An app that opens content from external links (universal links/deep links) and routes users to the correct screen.
  • A multi-tab UI where each tab manages its own Navigator to preserve independent navigation stacks.
  • A gated flow that redirects unauthenticated users to a login route via route guards.

FAQ

When should I choose go_router over Navigator?

Choose go_router when you need deep linking, URL-based navigation, web history support, or complex nested routes. Navigator is fine for simple stacks and quick prototypes.

How do I return data from a pushed screen?

Use Navigator.push and await the Future returned by push. After the pushed route pops with a result, handle it and check context.mounted before updating UI.