Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Navigating between pages in flutter

How to navigate between one page to another in flutter ?

Aman Jain, January 4, 2022January 15, 2022

Flutter provides its inbuilt navigator to navigate between pages. In our application it must have multiple screens and we wanted to navigate on all screens so in flutter we use Navigator class to navigate between pages.

In android we use Activity and in iOS we use ViewController to navigate between pages.

In this article i will show you to navigate to another page and also passing parameters to pages. I will take an example of two screens where we will navigate and than come back to the same page.

Step 1 : Create flutter project

Very first step is to create the fluter application using command line tool or in Android studio.

flutter create example_app

This will create a new project with name my_app then go in to the folder.

Step 2 : Create two widgets for navigation

Now, Create two widgets to navigate between the pages. Let’s create one widget name as FirstPageWidget and Second as SecondPageWidget

File : lib/FirstPageWidget.dart

import 'package:flutter/material.dart';
class FirstPageWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar: AppBar(
          // The title text which will be shown on the action bar
          title: const Text("Readerstacks.com Demo"),
        ),
        body: Center(
        child: Column(children: [
      Text(
        'Hello, ReaderStack  World! My First page',
        style: Theme.of(context).textTheme.headline4,
      ),
      ElevatedButton(
          child: const Text("Navigate to Second"),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const SecondPageWidget('this is params')),
            );
          })
      ]))
    );
  }
}

Here we used Navigator.push to navigate to another page which is SecondPageWidget and also used Scaffold for material mobile design.

lib/SecondPageWidget.dart

import 'package:flutter/material.dart';
class SecondPageWidget extends StatelessWidget {
  
  final String title;
  
  const SecondPageWidget(this.title,{Key? key}) : super(key: key);

  
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        appBar: AppBar(
          // The title text which will be shown on the action bar
          title: Text(title),
        ),
        body: Center(
        child: Column(children: [
      Text(
        'Hello, ReaderStack  World! My Second page',
        style: Theme.of(context).textTheme.headline4,
      ),
      ElevatedButton(
           child: const Text("Navigate Back"),
          onPressed: () {
            Navigator.pop(context);
          })
      ]))
    );
  }
} 

We used here Navigator.pop to go back to the last page.

Step 3 : Add Page to main file

Add you new pages to main.dart and start navigating.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Application name
      title: 'Flutter Hello World',
      // Application theme data, you can set the colors for the application as
      // you want
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      // A widget which will be started on application startup
      home:  FirstPageWidget(),
    );
  }
}
 

Step 4: Run the project

Simply run the project using command line or in android studio to check the implementation.

flutter run lib/main.dart

How to pass the data between pages using navigator.push?

In the above example we navigate page to another page but not passed any data to other page so to pass the data between one or two page we need to pass extra param to Navigator as below

Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondPageWidget("this is param")),
            );

Here we passed a parameter to SecondPageWidget(“this is param”) and in SecondPageWidget change as below

  final String title;
  const SecondPageWidget(this.title,{Key? key}) : super(key: key);

Run the implementation in dartpad.

Related

Dart Flutter

Post navigation

Previous post
Next post

Related Posts

Dart flutter refresh page on back

How to refresh a page or widget on navigation pop/back in flutter ?

April 28, 2022November 5, 2023

Sometime in out application we want to refresh a page or widget on navigation pop/back in flutter because we want to update the state of our page so users can see the latest update based on their actions. So In this article I will show you to flutter refresh page…

Read More
Dart Create routing in flutter

How to create routing in flutter and pass parameters to routes ?

January 24, 2022October 4, 2022

Flutter has multiple ways to create navigation and routing. In our last article i demonstrate How to navigate between one page to another in flutter and in this article i will show you to create a router where we can define all routes using flutter named routes. Fluter named routes…

Read More
Dart How to create a scroll view in flutter

How to create a scroll view in flutter ?

January 8, 2022November 5, 2023

Flutter provides multiple way to scroll the view and one of the most efficient way is ListView.builder. Using the ListView class we can configure multiple options. Using these widgets we can create horizontal as well vertical scroll. ListView GridView CustomScrollView DraggableScrollableSheet NestedScrollView PageView SingleChildScrollView Here are the few scroll widgets…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version