Navigation and Routing in Flutter

In Flutter, Navigation refers to moving between different screens (called routes) in your app. Routing is the technique used to define and manage those transitions.

Keyword Explanation:

πŸ”„ Basic Navigation

// Navigate to a new screen
Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => SecondScreen()),
);

// Go back to previous screen
Navigator.pop(context);
    
Important: Use Navigator.push() to move forward and Navigator.pop() to go back.

πŸ—ΊοΈ Named Routes

Named routes help manage navigation in larger apps by giving each route a name.

void main() {
  runApp(MaterialApp(
    initialRoute: '/',
    routes: {
      '/': (context) => HomeScreen(),
      '/about': (context) => AboutScreen(),
    },
  ));
}

// Navigate using name
Navigator.pushNamed(context, '/about');
    
Why use named routes?

🏠 Real World Analogy

Imagine an app as a house with many rooms. Each room is a screen. Navigation is like walking from one room to another, and the Navigator keeps track of where you've been so you can go back if needed.

For more, check Flutter's official Navigation guide.