Deb
Deb

Reputation: 95

Flutter rebuilding the parent widget of the Consumer

Widget build(BuildContext context) {
print("homepage");
final navigation = Provider.of<NavigationProvider>(context, listen: false);

return Consumer<ThemeProvider>(builder: (context, themeData, _) {
  return Scaffold(
    backgroundColor: themeData.getTheme['mainBackground'],
    appBar: AppBar(
      backgroundColor: themeData.getTheme['appBar'],
      elevation: 0.0,
      title: Text(
        "INTRADAE",
        style: GoogleFonts.montserrat(
          color: themeData.getTheme['textColor'],
        ),
      ),
      actions: <Widget>[
        Container(
          margin: EdgeInsets.only(right: 5),
          child: IconButton(
            icon: Icon(
              Icons.account_balance_wallet,
              size: 30,
              color: themeData.getTheme['textColor'],
            ),
            onPressed: null,
          ),
        )
      ],
    ),
    body: Consumer<NavigationProvider>(
        builder: (context, navigationProvider, _) =>
            navigationProvider.getNavigation),
    bottomNavigationBar: CurvedNavigationBar(
      index: 0,
      height: 50,
      color: themeData.getTheme['secondaryBackground'],
      backgroundColor: themeData.getTheme['mainBackground'],
      buttonBackgroundColor: themeData.getTheme['mainBackground'],
      items: <Widget>[
        Icon(Icons.home, color: Colors.white),
        Icon(Icons.shopping_cart, color: Colors.white),
        Icon(Icons.person, color: Colors.white)
      ],
      onTap: (index) {
        navigation.updateNavigation(_pages[index]);
      },
    ),
  );
});

I am trying to update whatever is inside the body of the scaffold when updateNavigation takes place. Everything seemed fine, until I put a print inside the build method of the main widget and noticed that it was being called on every update the to NavigationProvider. I am scratching my head since. I can't understand why this widget has to rebuild when only the Consumers should rebuild which in this case is inside the body of the Scaffold. Coming from a React background this just seems weird to me. Can anyone tell me why this is happening?

Upvotes: 0

Views: 976

Answers (1)

Deb
Deb

Reputation: 95

The problem was solved once I performed a hot restart. I then read about Hot Reload in Flutter the docs and realised that the changes I was making to the code weren't going to be seen unless I performed a hot restart.

Although the code posted here has

listen:false

in,

Provider.of<NavigationProvider>(context, listen: false)

But, I originally did not have that in my code. And when adding that argument too didn't seem to help, I decided to post a question here. The problem was hot reload all along. Should've have made sure before posting.

Upvotes: 1

Related Questions