Navigate to a new screen and back :

Interactive example

import ‘package:flutter/material.dart’;

void main() {

 runApp(const MaterialApp(

  title: ‘Navigation Basics’,

  home: FirstRoute(),

 ));

}

class FirstRoute extends StatelessWidget {

 const FirstRoute({super.key});

 @override

 Widget build(BuildContext context) {

  return Scaffold(

   appBar: AppBar(

    title: const Text(‘First Route’),

   ),

   body: Center(

    child: ElevatedButton(

     child: const Text(‘Open route’),

     onPressed: () {

      Navigator.push(

       context,

       MaterialPageRoute(builder: (context) => const SecondRoute()),

      );

     },

    ),

   ),

  );

 }

}

class SecondRoute extends StatelessWidget {

 const SecondRoute({super.key});

 @override

 Widget build(BuildContext context) {

  return Scaffold(

   appBar: AppBar(

    title: const Text(‘Second Route’),

   ),

   body: Center(

    child: ElevatedButton(

     onPressed: () {

      Navigator.pop(context);

     },

     child: const Text(‘Go back!’),

    ),

   ),

  );

 }

}

Leave a comment

Your email address will not be published. Required fields are marked *