How To Redirect From One Page To Another Page In Flutter

admin_img Posted By Bajarangi soft , Posted On 22-09-2020

The sample Google flutter code below illustrate how to redirect to a new page on button click in flutter.

Redirect Page In Flutter

Navigate between screens in Flutter

 When you import, use and build widgets from package:flutter/material.dart package, your screens,  commonly derived from Scaffold class, is already under navigation stack management by Flutter.
 

Create a Flutter app
 we will try to move back and forth from a screen to another screen. Let’s start by creating an app.

Redirect Page code
RaisedButton(
  color: Colors.black,
  child: Text('Go to SecondPage', style: TextStyle(color: Colors.white),),
  onPressed: () {
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => SecondPage()),
    );
  },
),

 


The Navigator works like a normal stack implementation. It comes with two well-known methods, push and pop.
  1. Push: The push method is used to add another route onto the top of the current stack. The new page is displayed over the previous one.
  2. Pop: As the Navigator works like a stack, it uses the LIFO (Last-In, First-Out) principle. The pop method removes the topmost route from the stack. This displays the previous page to the user.


Create a Frist Page(Push Method)
class FristPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Redirect Page',),
        backgroundColor: Colors.black,
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.black,
          child: Text('Go to SecondPage', style: TextStyle(color: Colors.white),),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondPage()),
            );
          },
        ),
      ),
    );
  }
}

Create Second page(Pop method)
class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("Second Page"),
        backgroundColor: Colors.black,
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.black,
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!',style: TextStyle(color: Colors.white),),
        ),
      ),
    );
  }
}

Complete Code in Redirect Page
Main.dart
import 'package:flutter/material.dart';
void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'Navigation Basics',
    home: FristPage(),
  ));
}

class FristPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Redirect Page',),
        backgroundColor: Colors.black,
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.black,
          child: Text('Go to SecondPage', style: TextStyle(color: Colors.white),),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondPage()),
            );
          },
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("Second Page"),
        backgroundColor: Colors.black,
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.black,
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!',style: TextStyle(color: Colors.white),),
        ),
      ),
    );
  }
}

Related Post