Flutter University
  • πŸ‘‹Welcome to Flutter University
  • Learn Flutter
    • πŸš—Basics
      • 🧺Dart Basics
      • πŸš›Setup & Installation
      • 🐣Hello Flutter
      • πŸŒ‰Widgets
      • ⛸️Basic State Management
      • πŸ‡ΎπŸ‡ΉBasic Layout and Styling
      • 🐝Basic Interactivity
      • πŸ›£οΈNavigation
      • πŸͺ„Working with Assets
    • πŸš…Intermediate
      • 🎯Deeper into Dart
      • ⭐More on State Management
      • πŸ“ƒForm Handling
      • πŸ—ΌNetworking
      • πŸŽ‡Persistence
      • πŸ§™β€β™‚οΈAnimations
      • πŸ§ͺTesting
      • πŸ“¦Package Management
    • ✈️Professional
      • πŸŽ“Advanced Animations
      • 🎨Custom Painters
      • 🐼Continuous Integration/Continuous Deployment (CI/CD)
      • 🎭Performance Profiling
      • πŸ”¬Native Integrations
      • 🌍Accessibility and Localization
      • 🀘Understanding Design Patterns
      • πŸ“šFlutter Architecture
        • The Layer Model
        • Reactive User Interfaces
        • Flutter Widgets
        • The Rendering Process
        • Platform Embedders Overview
        • Integrating with Other Code
        • Support for the Web
  • Tutorials
    • 🌈UI
      • 🏚️Clubhouse Clone
      • πŸ”‰Netflix Clone
    • βš”οΈFull Stack
    • ⛓️Blockchain
    • πŸ€–AI/ML
  • Miscellaneous
    • πŸ–₯️100 Days of Flutter
    • 🎨Join Community
Powered by GitBook
On this page
  • 1. The Navigator Widget 🧭
  • 2. Basic Navigation πŸ”„
  • 3. Named Routes πŸ“œ
  • 4. Passing Data Between Routes πŸ”„
  • Assignments πŸ“

Was this helpful?

  1. Learn Flutter
  2. Basics

Navigation

Navigation is the mechanism of moving between different screens in your application. Flutter provides a robust navigation and routing system to manage the transition between different pages.

1. The Navigator Widget 🧭

The Navigator widget is at the core of Flutter's navigation system. It manages a stack of Route objects and provides methods to manipulate the stack, like push and pop.

2. Basic Navigation πŸ”„

Let's start with a simple example of navigating between two screens.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirstPage(),
    );
  }
}

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Page'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondPage()),
            );
          },
          child: Text('Go to Second Page'),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Page'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Back to First Page'),
        ),
      ),
    );
  }
}

3. Named Routes πŸ“œ

Named routes provide a way to define a mapping between a named identifier and a route. This is useful for more structured navigation and handling deep links.

MaterialApp(
  initialRoute: '/',
  routes: {
    '/': (context) => FirstPage(),
    '/second': (context) => SecondPage(),
  },
);

4. Passing Data Between Routes πŸ”„

You can pass data between routes by passing arguments to the Navigator.push method.

// Define a constructor for SecondPage to accept data
class SecondPage extends StatelessWidget {
  final String data;

  SecondPage({required this.data});

  // ...
}

// Now pass data while navigating
Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => SecondPage(data: 'Hello from First Page!'),
  ),
);

Assignments πŸ“

Let's reinforce your understanding with some practical exercises:

By tackling these assignments, you’ll get hands-on experience with Flutter's navigation system.

Up next, working with assets!

Last updated 1 year ago

Was this helpful?

πŸš—
πŸ›£οΈ