通常情况下,子widget无法单独感知父widget的变化,当父state变化时,通过其build重建所有子widget; InheritedWidget可以避免这种全局创建,实现局部的子widget更新: 子widget通过BuildContext.inheritFromWidgetOfExactType从buildContext中获取并监听指定类型的父Inherit
Inherited Widget : In flutter, the inherited widget is a base class that allows those classes to extend the information under the tree from it. Inherited widgets are also a kind of state management technique. It works by telling registered build references when a change occurs. However, it add...
While we generally use Provider or GetIt to pass things around in Flutter, there are times when you don’t want to have any dependencies on these libraries, and instead just want to define your own MyFoo.of(context) lookup. Often this is when you’re creating packages yourself....
In this repo you will see more details about the Inherited widget and how to use it in your flutter application. - whatsupcoders/FlutterInheritedWidget
1. dependOnInheritedWidgetOfExactType 在Flutter 中的作用 dependOnInheritedWidgetOfExactType 是Flutter 框架中的一个核心方法,用于在 Widget 树中查找并依赖特定类型的 InheritedWidget。当这个 InheritedWidget 的数据发生变化时,依赖它的 Widget 会被通知并重新构建。这个方法通常用于实现状态管理或数据共享功能,允许子...
问题出现的原因是在initState完成之前进行了页面布局 比如这样(错误示范) 把布局放到initState之后就行了 或者延时处理