Andrea Costanzo
Andrea Costanzo

Reputation: 2215

Avoid widget being re-built on navigator.push in flutter

Exist a way to avoid that the "same" widget is rebuilt when we push a page using the navigator?

Example: avoid that MyFancyWidget() is rebuilt when we push from navigator from page 1 to page 2?

//page 1
return Scaffold(
  body: Row([
    MyFancyWidget(),
    /*Remaining part of the first page*/
  ])
);

//page 2
return Scaffold(
  body: Row([
    MyFancyWidget(),
    /*Remaining part of the second page*/
  ])
);

Use case:

I have faced a problem while building a sidebar in flutter web. Since navigation inside my application is a particular issue, I need to use navigator.pushNamed every time I click on a link to a page in the sidebar. This choice was done because I need that my web-site open that specific page if the user click the reload button or load the previous one when he/she tries to navigate using the "back button" of the browser. However using pushNamed raise an issue with the sidebar: every time the user clicks on a link the whole widget is obviously rebuilt with the rest of the page since the sidebar is part of the actual page.

Upvotes: 0

Views: 848

Answers (1)

Jackson Lee
Jackson Lee

Reputation: 1248

Every time you change pages it will re-build the widget on Flutter Web. You can use a TabController which will allow you to navigate without "reloading" the entire page.

Read this Question to see how

Upvotes: 1

Related Questions