Skip to content

Instantly share code, notes, and snippets.

@lordvidex
Created February 20, 2020 13:10
Show Gist options
  • Save lordvidex/a10ed43452736b5c6b3d1abe6a7eda45 to your computer and use it in GitHub Desktop.
Save lordvidex/a10ed43452736b5c6b3d1abe6a7eda45 to your computer and use it in GitHub Desktop.
Solution to Navigator Question
//Author: Owamoyo Evans - lordvidex
//Email: lordvidex.prime@gmail.com
//GitHub: https://github.com/lordvidex
import 'package:flutter/material.dart';
//Logic on third page
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FirstPage(),
routes: {
SecondPage.routeName: (_)=>SecondPage(),
ThirdPage.routeName: (_)=>ThirdPage(),
},
);
}
}
class FirstPage extends StatelessWidget {
static const routeName = '/';
@override
Widget build(BuildContext context) {
void _nextPage(){
Navigator.of(context).pushNamed(SecondPage.routeName);
}
return Scaffold(
appBar: AppBar(
title: Text('First Page'),
),
body: Center(
child: Text('First Page'),
),
floatingActionButton: FloatingActionButton(
onPressed: _nextPage,
child: Icon(Icons.add),
),
);
}
}
class SecondPage extends StatelessWidget{
static const routeName = '/second';
@override
Widget build(BuildContext context){
void _nextPage(){
Navigator.of(context).pushNamed(ThirdPage.routeName);
}
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: Text('Second Page'),
),
floatingActionButton: FloatingActionButton(
onPressed: _nextPage,
child: Icon(Icons.add),
),
);
}
}
class ThirdPage extends StatelessWidget{
static const routeName = '/third';
@override
Widget build(BuildContext context){
void _nextPage(){
//Logic here - ***************************
Navigator.of(context).popUntil((Route<dynamic> route) => route.isFirst);
}
return Scaffold(
appBar: AppBar(
title: Text('Third Page'),
),
body: Center(
child: Text('Third Page'),
),
floatingActionButton: FloatingActionButton(
onPressed: _nextPage,
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment