0Pricing
Flutter Mobile Development · Lektion

Grundlegende Seitennavigation

Lernen Sie, mit Navigator Routen zu öffnen und zu schließen, um einfache Übergänge zwischen verschiedenen Bildschirmen Ihrer Flutter-App zu ermöglichen.

Grundlegende Seitennavigation ist eine kostenlose Flutter Mobile Development-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flutter Mobile Development-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flutter Mobile Development-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Navigating Your App

Think of a mobile app as a collection of pages or "screens." Navigation is how users move between these screens, like going from a product list to a product detail page.

  • Apps need a clear way to move forward.
  • Apps also need a way to go back.
  • Flutter provides powerful tools for this.

Meet the Navigator

In Flutter, the Navigator widget manages a stack of Route objects. When you want to show a new screen, you "push" a route onto this stack. When you want to go back, you "pop" a route off the stack.

  • It's like a stack of playing cards.
  • New cards go on top (push).
  • You remove cards from the top (pop).

Understanding Routes

A Route is an abstraction for a "screen" or "page" in your app. Flutter provides a common type of route called MaterialPageRoute, which creates a platform-specific transition animation (like sliding in from the right on Android).

It's the blueprint for how a new screen will look and behave when shown.

Moving Forward with push()

To navigate to a new screen, you use Navigator.push(). This method takes a BuildContext and a MaterialPageRoute as arguments.

The builder function inside MaterialPageRoute tells Flutter which widget to build for the new screen.

Try Pushing a Route

This example shows how to push a new screen when a button is pressed. Run it and tap the button!

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

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

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Second Screen')),
      body: const Center(
        child: Text('You are on the second screen!'),
      ),
    );
  }
}

What is BuildContext?

You might have noticed context being passed around. BuildContext is a handle to the location of a widget in the widget tree. It helps widgets locate other widgets or data higher up in the tree.

The Navigator uses this context to know which navigation stack to operate on.

Going Back with pop()

When you want to return to the previous screen, you "pop" the current route off the navigation stack using Navigator.pop(). This will reveal the screen that was underneath.

Flutter's AppBar often provides a back button automatically when a new route is pushed, which internally calls Navigator.pop().

Try Popping a Route

Let's modify our previous example to include a button on the second screen that allows us to explicitly go back to the home screen.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

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

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Second Screen')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.pop(context); // This will go back!
          },
          child: const Text('Go Back'),
        ),
      ),
    );
  }
}

Visualizing the Stack

Imagine the Navigator maintains a stack of screens. When you push, a new screen is placed on top. When you pop, the top screen is removed.

The user always sees the topmost screen. This stack behavior is fundamental to how navigation works in Flutter.

  • Push: Adds to top.
  • Pop: Removes from top.
  • First screen is at the bottom.

Navigation Check

Consider a Flutter app with two screens, ScreenA and ScreenB. If you are currently on ScreenA and want to move to ScreenB, then later return to ScreenA, which sequence of Navigator methods would you use?

Recap: Basic Navigation

Great job! You've learned the core concepts of basic navigation in Flutter:

  • The Navigator manages a stack of routes.
  • MaterialPageRoute defines a new screen.
  • Navigator.push() adds a new screen to the stack.
  • Navigator.pop() removes the current screen from the stack.
  • BuildContext is essential for navigation operations.

Next, we'll explore more advanced navigation techniques like named routes!

Häufig gestellte Fragen

Ist die Lektion „Grundlegende Seitennavigation“ kostenlos?

Ja — der vollständige Text von „Grundlegende Seitennavigation“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flutter Mobile Development-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flutter Mobile Development-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Grundlegende Seitennavigation“?

Lernen Sie, mit Navigator Routen zu öffnen und zu schließen, um einfache Übergänge zwischen verschiedenen Bildschirmen Ihrer Flutter-App zu ermöglichen. Du übst Flutter Mobile Development mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Flutter Mobile Development zu starten?

Keine Vorkenntnisse erforderlich. Flutter Mobile Development auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Grundlegende Seitennavigation“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Flutter Mobile Development-Lektion Code schreiben und ausführen?

Ja. Jede Flutter Mobile Development-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Grundlegende Seitennavigation
  2. Benannte Routen und Argumente
  3. TabBars und Drawers
  4. Deep Linking und URL-Navigation
← Zurück zu Flutter Mobile Development