Last active
January 13, 2026 13:09
-
-
Save PlugFox/52c08d44d4074d37c236528b058eedd7 to your computer and use it in GitHub Desktop.
Pop and push demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Pop and Push Demo | |
| * https://gist.github.com/PlugFox/52c08d44d4074d37c236528b058eedd7 | |
| * https://dartpad.dev?id=52c08d44d4074d37c236528b058eedd7 | |
| * Mike Matiunin <plugfox@gmail.com>, 13 January 2026 | |
| */ | |
| import 'dart:async'; | |
| import 'package:flutter/material.dart'; | |
| void main() => runZonedGuarded<void>( | |
| () => runApp(const App()), | |
| (error, stackTrace) => print('Top level exception: $error'), // ignore: avoid_print | |
| ); | |
| class App extends StatelessWidget { | |
| const App({super.key}); | |
| @override | |
| Widget build(BuildContext context) => MaterialApp( | |
| title: 'Pop and Push Demo', | |
| home: Builder( | |
| builder: (context) => Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Home'), | |
| ), | |
| body: SafeArea( | |
| child: Center( | |
| child: ElevatedButton( | |
| onPressed: () => Navigator.maybeOf(context) | |
| ?..push( | |
| MaterialPageRoute<void>( | |
| builder: (context) => const A(), | |
| ), | |
| ), | |
| child: const Text('Go to A'), // The onPressed is set in the A widget. | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| class A extends StatelessWidget { | |
| const A({ | |
| super.key, | |
| }); | |
| @override | |
| Widget build(BuildContext context) => Scaffold( | |
| appBar: AppBar( | |
| title: const Text('A'), | |
| ), | |
| body: SafeArea( | |
| child: Center( | |
| child: ElevatedButton( | |
| child: const Text('Go to B'), | |
| onPressed: () { | |
| Navigator.maybeOf(context) | |
| ?..popUntil((route) => route.isFirst) | |
| ..push( | |
| MaterialPageRoute<void>( | |
| builder: (context) => const B(), | |
| ), | |
| ); | |
| }), | |
| ), | |
| ), | |
| ); | |
| } | |
| class B extends StatelessWidget { | |
| const B({ | |
| super.key, | |
| }); | |
| @override | |
| Widget build(BuildContext context) => Scaffold( | |
| appBar: AppBar( | |
| title: const Text('B'), | |
| ), | |
| body: SafeArea( | |
| child: Center( | |
| child: ElevatedButton( | |
| child: const Text('Go to A'), | |
| onPressed: () { | |
| Navigator.maybeOf(context) | |
| ?..popUntil((route) => route.isFirst) | |
| ..push( | |
| MaterialPageRoute<void>( | |
| builder: (context) => const A(), | |
| ), | |
| ); | |
| }), | |
| ), | |
| ), | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment