Junior — Middle
Мұрагерлік Widget класы туралы және оны қалай қолданатынын түсіндіріп берсеңіз?
sobes.tech AI
Answer from AI
InheritedWidget in Flutter is a base class for widgets that can pass data to their descendants in the widget tree. It allows for efficient sharing of state or configuration with child widgets without the need to explicitly pass data through constructors.
How it works:
InheritedWidgetstores data that may be needed by descendants.- Descendants can access this data via the
of(context)method. - When the data in
InheritedWidgetchanges, Flutter automatically rebuilds those widgets that depend on this data.
Example usage:
class MyInheritedWidget extends InheritedWidget {
final int data;
MyInheritedWidget({required this.data, required Widget child}) : super(child: child);
static MyInheritedWidget? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>();
}
@override
bool updateShouldNotify(MyInheritedWidget oldWidget) {
return data != oldWidget.data;
}
}
Thus, InheritedWidget serves as the foundation for building reactive and efficient state management systems in Flutter.