Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Created February 26, 2020 09:59
Show Gist options
  • Save CaiJingLong/ff332447d9aded662f8afcf7d5cf2fe3 to your computer and use it in GitHub Desktop.
Save CaiJingLong/ff332447d9aded662f8afcf7d5cf2fe3 to your computer and use it in GitHub Desktop.
pop to root
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(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: HomePage(title: 'Flutter Demo Home Page'),
);
}
}
class HomePage extends StatelessWidget {
final String title;
final int level;
const HomePage({Key key, this.title, this.level = 0}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("$title $level"),
),
body: Column(
children: <Widget>[
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) {
return HomePage(
title: title,
level: level + 1,
);
},
),
);
},
child: Text("next"),
),
RaisedButton(
onPressed: () {
for (var i = 0; i < level; i++) {
Navigator.pop(context);
}
},
child: Text("to root"),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment