【问题标题】:Flutter rebuilding the parent widget of the ConsumerFlutter 重建 Consumer 的父小部件
【发布时间】:2020-12-22 17:06:07
【问题描述】:
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]);
      },
    ),
  );
});

updateNavigation 发生时,我正在尝试更新脚手架主体内的任何内容。一切似乎都很好,直到我在主小部件的 build 方法中添加了一个 print,并注意到它在每次更新到 NavigationProvider 时都会被调用。 从那以后我一直在挠头。我不明白为什么只有消费者应该重建时,这个小部件必须重建,在这种情况下,它位于脚手架的主体内。 来自 React 背景,这对我来说似乎很奇怪。 谁能告诉我为什么会这样?

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies


    【解决方案1】:

    我执行热重启后问题就解决了。然后,我在 Flutter 文档中阅读了有关 Hot Reload 的信息,并意识到除非我执行热重启,否则我对代码所做的更改不会被看到。

    虽然这里发布的代码有

    听:假

    在,

    Provider.of(上下文,听:假)

    但是,我最初的代码中没有这个。当添加该论点似乎也没有帮助时,我决定在这里发布一个问题。 问题一直是热重载。应该在发帖前确认一下。

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 2020-07-25
      • 1970-01-01
      • 2021-10-29
      • 2019-08-23
      • 2019-07-12
      • 2020-01-23
      • 2019-12-09
      • 2019-12-09
      相关资源
      最近更新 更多