Understanding three-page flutter app with navigation

This time focus primarily on the comments around Navigator.

import 'package:flutter/material.dart';
void main() {
  // Start application.
  runApp(const MyApp());
}
// Root application widget.
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // Page 1 is our starting page.
      home: const HomePage(),
    );
  }
}
// ======================================================
// PAGE 1 - HOME PAGE
// ======================================================
class HomePage extends StatelessWidget {
  const HomePage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home'),
      ),
      body: Center(
        child: ElevatedButton(
          child: const Text('Open Products'),
          onPressed: () {
            // PUSH means:
            // "Open a new page on top of the current page."
            Navigator.push(
              // context helps Flutter find the Navigator.
              context,
              // MaterialPageRoute creates the new page/route.
              MaterialPageRoute(
                // Tell Flutter which page to open.
                builder: (context) => const ProductsPage(),
              ),
            );
          },
        ),
      ),
    );
  }
}
// ======================================================
// PAGE 2 - PRODUCTS PAGE
// ======================================================
class ProductsPage extends StatelessWidget {
  const ProductsPage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Flutter automatically displays a Back button here
        // because we arrived using Navigator.push().
        title: const Text('Products'),
      ),
      body: Center(
        child: ElevatedButton(
          child: const Text('View Laptop'),
          onPressed: () {
            // Add ProductDetailsPage on top of ProductsPage.
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) =>
                    const ProductDetailsPage(),
              ),
            );
          },
        ),
      ),
    );
  }
}
// ======================================================
// PAGE 3 - PRODUCT DETAILS PAGE
// ======================================================
class ProductDetailsPage extends StatelessWidget {
  const ProductDetailsPage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Product Details'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              'Laptop',
              style: TextStyle(
                fontSize: 28,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 10),
            const Text(
              'Price: ₹75,000',
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                // POP means:
                // "Remove this page and return to previous page."
                Navigator.pop(context);
              },
              child: const Text('Go Back'),
            ),
          ],
        ),
      ),
    );
  }
}

The only navigation code you need to remember initially

Go to another page

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => const ProductsPage(),
  ),
);

Think:

PUSH = Go forward

Return to previous page

Navigator.pop(context);

Think:

POP = Go back

Mental model

When you navigate:

Home
 ↓ PUSH
Products
Home
 ↓ PUSH
Product Details
Products
Home
 ↓ POP
Products
Home

Tip: Don't jump into named routes or advanced routing packages yet. Become comfortable with Navigator.push(), MaterialPageRoute, Navigator.pop(), and BuildContext first.

Comments

Popular posts from this blog

Files in your Flutter Project

Flutter setup on Windows 11 using VS Code + Android Emulator

Understanding single-page flutter app with state