How to Implement Navigation In A Flutter App?

10 minutes read

In order to implement navigation in a Flutter app, you'll begin by defining the various screens or pages you want to navigate between. Each screen will be represented by a widget in Flutter.

  1. Define the Screens: Create a new widget for each screen/page you want to navigate to. These widgets will extend the StatefulWidget or StatelessWidget class. Add necessary UI elements and logic to each screen/widget.
  2. Set Up Navigation: In your main.dart file (or any other entry point), create an instance of MaterialApp widget. This will serve as the container for your app and handle navigation. Define a routes parameter inside MaterialApp to map the names of your screens with their respective widget classes. For example, { '/page1': (context) => Page1(), '/page2': (context) => Page2(), ... }.
  3. Navigating Between Screens: To navigate from one screen to another, you can use the Navigator class which is accessible within the BuildContext. Inside a widget, typically through the press of a button or a user interaction, call Navigator.push() method and provide the BuildContext and the name of the screen to navigate to. If you need to pass data to the next screen, you can provide it as arguments to the Navigator.push() method. To return to the previous screen, you can call Navigator.pop(context).
  4. Handling Navigation: In each screen class, use the ModalRoute.of(context) property to access any data or arguments passed while navigating. Utilize the initState method to retrieve the passed arguments or initialize any required variables. Implement the desired navigation logic within each screen/widget.


Remember to import the necessary dependencies such as material.dart and any other custom files where you define your screens. Following these steps will allow you to implement navigation in your Flutter app smoothly, enabling users to move between different screens.

Best Dart Books to Read in 2024

1
Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

Rating is 5 out of 5

Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

2
Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

Rating is 4.9 out of 5

Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

3
Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

Rating is 4.8 out of 5

Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

4
Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

Rating is 4.7 out of 5

Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

5
The Dart Programming Language

Rating is 4.6 out of 5

The Dart Programming Language

6
Mastering Dart: A Comprehensive Guide to Learn Dart Programming

Rating is 4.5 out of 5

Mastering Dart: A Comprehensive Guide to Learn Dart Programming

7
Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

Rating is 4.4 out of 5

Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

8
Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition

Rating is 4.3 out of 5

Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition


How to implement tab-based navigation in Flutter?

To implement tab-based navigation in Flutter, you can use the TabBar widget and the TabBarView widget together. Here's a step-by-step guide:


Step 1: Create a DefaultTabController widget and specify the number of tabs you want.

1
2
3
4
5
6
DefaultTabController(
  length: 3,
  child: Scaffold(
    ...
  ),
);


Step 2: Inside the Scaffold, create a AppBar widget with a TabBar.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
appBar: AppBar(
  title: Text('Tab Navigation'),
  bottom: TabBar(
    tabs: [
      Tab(icon: Icon(Icons.home), text: 'Home'),
      Tab(icon: Icon(Icons.search), text: 'Search'),
      Tab(icon: Icon(Icons.person), text: 'Profile'),
    ],
  ),
),


Step 3: Create a TabBarView widget to define the content of each tab.

1
2
3
4
5
6
7
body: TabBarView(
  children: [
    HomeScreen(),       // Replace HomeScreen with your own widget
    SearchScreen(),     // Replace SearchScreen with your own widget
    ProfileScreen(),    // Replace ProfileScreen with your own widget
  ],
),


That's it! Now you have implemented tab-based navigation in your Flutter app. Make sure to replace HomeScreen, SearchScreen, and ProfileScreen with your own widgets for each tab.


How to pass arguments between screens in Flutter navigation?

To pass arguments between screens in Flutter navigation, you can follow these steps:

  1. Declare a Route object for the screen that you want to navigate to with arguments. This can be done by defining a named route in the MaterialApp widget's routes parameter or by using the onGenerateRoute parameter.
  2. Define a constructor for the screen that you want to navigate to. This constructor should accept the arguments that you want to pass.
  3. When navigating to the screen, use the Navigator.pushNamed function and provide the name of the route.
  4. In the destination screen, access the arguments using the ModalRoute.of(context).settings.arguments property.


Here is an example:

  1. Declare a named route in the MaterialApp widget's routes parameter:
1
2
3
4
5
MaterialApp(
  routes: {
    '/secondScreen': (context) => SecondScreen(),
  },
);


  1. Define a constructor for the SecondScreen that accepts the arguments:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class SecondScreen extends StatelessWidget {
  final String message;

  SecondScreen({required this.message});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Screen'),
      ),
      body: Center(
        child: Text(message),
      ),
    );
  }
}


  1. Navigate to the SecondScreen and pass the arguments:
1
Navigator.pushNamed(context, '/secondScreen', arguments: 'Hello from the first screen!');


  1. Access the arguments in the SecondScreen:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final String message = ModalRoute.of(context)!.settings.arguments as String;

    return Scaffold(
      appBar: AppBar(
        title: Text('Second Screen'),
      ),
      body: Center(
        child: Text(message),
      ),
    );
  }
}


Note that you can pass any type of argument by replacing the String type used in this example with the desired type.


How to present a modal screen in a Flutter app?

To present a modal screen in a Flutter app, you can make use of the showDialog function provided by the Flutter framework. Here's a step-by-step guide for presenting a modal screen:

  1. Create a new stateful widget that represents your modal screen. This widget will typically extend the StatefulWidget class. For example, let's call it MyModalScreen:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class MyModalScreen extends StatefulWidget {
  @override
  _MyModalScreenState createState() => _MyModalScreenState();
}

class _MyModalScreenState extends State<MyModalScreen> {
  @override
  Widget build(BuildContext context) {
    return Container(
      // Build your modal screen UI here
    );
  }
}


  1. Add a method to your main widget that presents the modal screen. This method will call showDialog and pass your MyModalScreen widget as the builder parameter:
1
2
3
4
5
6
7
8
void _presentModalScreen(BuildContext context) {
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return MyModalScreen();
    },
  );
}


  1. Trigger the presentation of the modal screen from your main widget, usually through a button press or any other user interaction:
1
2
3
4
5
6
FlatButton(
  onPressed: () {
    _presentModalScreen(context);
  },
  child: Text('Show Modal'),
),


Now, when the button is pressed, the MyModalScreen widget will appear as a modal screen on top of your main app screen. You can customize the appearance and behavior of the modal screen based on your requirements.


What is a route in Flutter navigation?

In Flutter navigation, a route is a screen or a page of the application. It represents a specific UI component that the user interacts with or observes, such as a login page, a home screen, or a profile page. Each route can have its own set of widgets and functionalities to deliver a specific user experience. Flutter uses a stack-based navigation system called the Navigator to manage and transition between routes.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To create a simple Flutter app with Dart, follow these steps:Install Flutter SDK: First, download and install Flutter SDK from the official Flutter website according to your operating system (Windows, macOS, or Linux). Set up your IDE: Flutter works with vario...
State management in a Flutter app refers to the process of handling and updating the data within the app. Flutter provides several options for managing state, allowing developers to choose the most suitable approach based on their app&#39;s complexity and requ...
To create a navigation bar in HTML and CSS, you can use the following steps:Create a new HTML file or open an existing one in a text editor.Start by creating a element to indicate that it is a navigation bar. Inside the element, you will add the navigation l...