Reputation: 13161
In onGenerateRoute
method in MaterialApp
, it looks wasteful to create Widgets every time, route is changed and Widget will also lose context. Should these widgets new Desktop(sugar)
be cached and reuse?
class AppComponentState extends State<AppComponent> implements SugarBuilder {
Sugar sugar;
_getRoute(RouteSettings settings) {
final List<String> path = settings.name.split('/');
if (path[0] != '') return null;
if (path[1] == 'sugar') {
if (sugar == null) {
return Navigator.pushNamed(context, '/login');
} else {
if (path[2] == 'module') {
return new ModulePage(sugar); // need to cache?
} else {
return new Desktop(sugar); // need to cache?
}
}
}
return null;
}
Widget build(BuildContext context) {
return new MaterialApp(
...
onGenerateRoute: _getRoute,
);
}
}
Upvotes: 1
Views: 3169
Reputation: 116848
Creating new Widget objects should generally be cheap. Flutter's widget framework will take care of updating the render tree when your widgets produce a different render objects. If you have a lot of global state for your app, you can store it in model objects and then pass them to the widgets as needed.
Upvotes: 4