简体   繁体   English

当它是 MaterialApp 的父级时,继承的 Widget 更改是否会重建整个应用程序?

[英]Does Inherited Widget changes rebuild whole application when it's the parrent of MaterialApp?

I want to use InheritedWidget to access and change its data from anywhere in application.我想使用InheritedWidget从应用程序的任何位置访问和更改其数据。 I've read many articles about InheritedWidget , but I do not understand one of its behaviors.我读过很多关于InheritedWidget的文章,但我不理解它的一种行为。

Here it says that only widgets that are using InheritedWidget get rebuilt when data changes(which is exactly what I want).这里它说只有使用InheritedWidget的小部件在数据更改时才会重建(这正是我想要的)。 更新应通知

But here it says that InheritedWidget is immutable and its data only changes when it is rebuilt itself!但是这里它说InheritedWidget是不可变的,它的数据只有在它自己重建时才会改变!
在此处输入图像描述

So doesn't this make the whole widget tree below the InheritedWidget get rebuilt when the data changes?那么,当数据发生变化时,这不会使InheritedWidget下面的整个小部件树得到重建吗?

How can I wrap the MaterialApp widget with InheritedWidget so I can change its data from anywhere in app and only rebuild a small Widget that is using InheritedWidget when the data changes?如何使用InheritedWidget包装MaterialApp小部件,以便我可以从应用程序中的任何位置更改其数据,并且仅在数据更改时重建使用InheritedWidget的小型 Widget?
I know I can implement this using provider package very easily, but in this part of application I want to use InheritedWidget if it's possible :)我知道我可以很容易地使用提供程序包来实现这一点,但是在这部分应用程序中,如果可能的话,我想使用InheritedWidget :)

An object or widget being immutable does not mean its values can't change.不可变的对象或小部件并不意味着它的值不能改变。

You can declare a final List<widget> children , which is immutable since it is final, and still add and remove widgets from it.您可以声明一个final List<widget> children ,它是不可变的,因为它是最终的,并且仍然可以从中添加和删除小部件。 What is immutable is the reference.不可变的是引用。

When you use the InheritedWidget is the same.使用 InheritedWidget 时也是如此。 If a value changes it wont rebuild itself and all the tree below.如果一个值发生变化,它不会重建自身和下面的所有树。 Even it is actually changed and rebuilt, it does not necessarily rebuild all tree below, it'd happen only if the widget type or its child/children reference changed also.即使它实际上被更改和重建,它也不一定重建下面的所有树,只有当小部件类型或其子/子引用也发生更改时才会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM