Skip to content

Instantly share code, notes, and snippets.

@AlexV525
Created May 14, 2020 02:35
Show Gist options
  • Save AlexV525/e38a8dcdf555d29d83e057ea53c16dea to your computer and use it in GitHub Desktop.
Save AlexV525/e38a8dcdf555d29d83e057ea53c16dea to your computer and use it in GitHub Desktop.
Will page build between push and pop?
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
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: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
child: Text('Push to 1'),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => OtherPage(1),
),
);
}
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class OtherPage extends StatelessWidget {
OtherPage(this.num);
final int num;
@override
Widget build(BuildContext context) {
print('build $num');
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
child: Text('Push to ${num + 1}'),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => OtherPage(num + 1),
),
);
}
),
FlatButton(
child: Text('Pop to first'),
onPressed: () {
Navigator.of(context).popUntil((route) => route.isFirst);
}
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment