In Flutter, there are two common ways to navigate between pages or views: using the Navigator and implementing a page view with PageView.
- Using Navigator for Navigation Between Pages The Navigator is the primary way to manage a stack of routes, allowing you to push and pop routes (pages) to navigate between them.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home 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('Go back to Home Page'),
),
),
);
}
}
- Using PageView for Swiping Between Pages PageView is useful for scenarios where you want to allow users to swipe between pages, similar to a carousel or a stepper.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: PageViewExample(),
);
}
}
class PageViewExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('PageView Example')),
body: PageView(
children: <Widget>[
Container(
color: Colors.red,
child: Center(child: Text('Page 1', style: TextStyle(fontSize: 24))),
),
Container(
color: Colors.green,
child: Center(child: Text('Page 2', style: TextStyle(fontSize: 24))),
),
Container(
color: Colors.blue,
child: Center(child: Text('Page 3', style: TextStyle(fontSize: 24))),
),
],
),
);
}
}
Top comments (2)
Nice, this should be helpful for beginners still trying to figure stuff out.
One could say its better to use navigator for pages handling authentication.
Also could you touch on the behavior of navigators and its types legacy or otherwise in the next post?
I will be waiting, Have a nice day.
In Flutter, the Navigator is a key component that manages a stack of routes (or pages) for navigation. Understanding how the Navigator behaves and the different types of navigators is essential for managing the navigation flow in your app.
Imperative Navigation (Legacy)
This is the traditional way of navigation, where the developer manually controls the navigation stack using Navigator methods like push, pop, etc. This approach is more common and straightforward, especially for simple apps.
Declarative Navigation (Navigator 2.0)
Introduced in Flutter 1.22, declarative navigation offers a more reactive and flexible way of managing navigation, similar to how UI is managed in a declarative style. This approach is more complex but provides finer control over the navigation stack, making it suitable for complex apps.
Declarative Navigator: Ideal for large, complex apps where navigation needs to be tightly integrated with the app's state. It provides more flexibility and better control over the navigation stack.
Summary
Imperative Navigation (Legacy): Direct, explicit control over navigation with Navigator methods.
Declarative Navigation: Reactive, flexible, and integrated with app state management, suitable for more complex scenarios.
Depending on your app's complexity and requirements, you can choose the appropriate navigation style that fits your needs.