I have noticed that when I move between screens and push route e.g. with
Navigator.pushNamed(context, "/screenNumberTwo");
the build method on my source screen (let's call it screenNumberOne) gets called after the build method for the screenNumberTwo
(though only screen number two is visible). I see this happening both if I move with onPressed
in MaterialButton
and with onTap
in InkWell
, in case of routes with parameters and without.
My routing is done as below
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateTitle: (BuildContext context) =>
Localization.of(context).appTitle,
initialRoute: "/",
routes: {
"/": (context) => FirstScreen(),
"/select_category": (context) => SecondScreen(),
},
onGenerateRoute: (routeSettings) {
print("Route: ${routeSettings.name}");
var path = routeSettings.name.split('/');
if (path[1] == 'thirdscreen') {
if (path.length == 3) {
//(... where I set paramId)
return new MaterialPageRoute(
builder: (context) => new ThirdScreen(paramId),
settings: routeSettings);
} else if (path.length == 4) {
//(... where I set paramId and param2Id)
return new MaterialPageRoute(
builder: (context) => new ThirdScreen(
paramId, param2Id,
),
settings: routeSettings);
}
},
localizationsDelegates: [
const LocalizationDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en'),
const Locale('pl'),
]);
}
}
This is not a functional problem, more of a concern, that flutter in my case is burning CPU on calculating screen that is not present to the user anyway... Any suggestions what to change/where to look for problems, are appreciated!