I imagine this is by design, but when Navigate.Push'ing to a new Page from a StatefulWidget, the original page is rebuilt on rendering the second. This is the sample code from https://flutter.io/cookbook/navigation/navigation-basics/, I have just added print statements to show activity and converted the first page to be Stateful.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: FirstScreen(),
));
}
class FirstScreen extends StatefulWidget {
@override
FirstScreenState createState() {
return new FirstScreenState();
}
}
class FirstScreenState extends State<FirstScreen> {
@override
Widget build(BuildContext context) {
print('Building Screen 1');
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: RaisedButton(
child: Text('Launch screen'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
},
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('Building Screen 2');
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}
Output from this shows that on rendering the second page, the first is also then rebuilt:
I/flutter (20929): Building Screen 2
I/flutter (20929): Building Screen 1
I have an instance of this scenario where Screen 1 uses a FutureBuilder. I don't want to have to reload all the data every time that Screen 2 is rebuilt. Can't help but think I'm missing something fundamental as this seems illogical.