Reshaping Provider: Understanding the bottom layer of Flutter and InheritedWidget (tutorial with source code)

State management is one of the most complex topics in Flutter, especially state shared with multiple widgets, in other words, "application state". InheritedWidget is one of Flutter's core widgets, which exposes values ​​as application state to descendant widgets.

When thinking about exposing value to multiple widgets, we tend to think about using Riverpod, Provider, Bloc, or other "state management" packages.

However, we can actually do the same thing without using any package, but InheritedWidget.

It's clear that its InheritedWidget functionality isn't enough to build more than medium-sized apps and remain robust or flexible, and it requires some boilerplate code for each state, so it's almost always relevant to use these packages for production apps.

However, understanding how things work in the InheritedWidgetFlutter framework still helps us understand the underlying mechanics of these packages and consider their appropriate usage, or we may even have the opportunity to build small applications without these packages.

In this article, we will first look at how to use InheritedWidget. We then abstract the code so that we can use this mechanism for multiple purposes with less code.

Since Providerpackage is a wrapper for InheritedWidgt, we quickly found that our abstraction code was very similar to Provider, in other words, reinvented Provider. Although the package is all but deprecated and Riverpod is recommended, understanding the basic concepts and mechanics of the package is valuable for understanding the package or other state management package providers. Riverpod

So, let's get started with InheritedWidget.

How to use InheritedWidget

We'll build a simple application that displays a list of articles.

おすすめ

転載: blog.csdn.net/iCloudEnd/article/details/132046036