【发布时间】: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