सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
Scoped Model एक हल्का राज्य प्रबंधन समाधान है जो Flutter के लिए एक विजेट वृक्ष में प्रतिक्रियाशील मॉडल साझाकरण की अनुमति देता है। यह मॉडल ऑब्जेक्ट को विजेट पदानुक्रम में नीचे ले जाने का एक सरल तरीका प्रदान करता है, जिससे बच्चे विजेट मॉडल को प्रतिक्रियाशील रूप से एक्सेस और अपडेट कर सकते हैं। पैकेज एक स्कोप्ड दृष्टिकोण का उपयोग करता है, जिससे केवल संबंधित विजेट मॉडल बदलने पर रीबिल्ड होते हैं, जिससे प्रदर्शन में सुधार होता है। छोटे से मध्यम आकार के ऐप्स के लिए यह आदर्श है, जहां प्रोवाइडर या ब्लॉक जैसे एक पूर्ण राज्य प्रबंधन समाधान अत्यधिक हो सकता है। सभी प्रमुख प्लेटफॉर्म के लिए समर्थन है, और यह मौजूदा Flutter प्रोजेक्ट में बिना किसी दिक्कत के एकीकृत हो सकता है।
उपयोग
छोटे एप्लिकेशन में सरल राज्य प्रबंधन
कई विजेट्स के मध्य डेटा साझा करना
मॉडल बदलावों के आधार पर प्रतिक्रियाशील UI अपडेट
फॉर्म स्थिति का प्रबंधन
थीम या उपयोगकर्ता प्राथमिकताओं के कार्यान्वयन
मुख्य विशेषताएँ
प्रतिक्रियाशील मॉडल प्रसार
न्यूनतम बॉयलरप्लेट
क्रॉस-प्लेटफ़ॉर्म समर्थन
मौजूदा कोड के साथ आसान एकीकरण
मॉडल बदलाव पर स्वचालित विजेट पुनर्निर्माण
इनके लिए उपयुक्त
शुरुआती Flutter विकासकर्ता
हल्के राज्य की आवश्यकता
Prototyping
मध्यम राज्य जटिलता वाले ऐप्स
भारी राज्य प्रबंधन फ्रेमवर्क से बचने वाले प्रोजेक्ट
विचार योग्य बातें
बड़े पैमाने के एप्लिकेशन के लिए उपयुक्त नहीं
सीमित निर्मित डिबगिंग उपकरण
Riverpod जैसे उन्नत समाधानों की तुलना में कम लचीला
जटिल राज्य परिवर्तनों का हस्तचालित संभाल
मॉडल अपडेट के लिए कोई निर्मित त्रुटि संहनन नहीं है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
scoped_model
A set of utilities that allow you to easily pass a data Model from a parent Widget down to its descendants. In addition, it also rebuilds all of the children that use the model when the model is updated. This library was originally extracted from the Fuchsia codebase.
This Library provides three main classes:
The Model class. You will extend this class to create your own Models, such as SearchModel or UserModel. You can listen to Models for changes!
The ScopedModel Widget. If you need to pass a Model deep down your Widget hierarchy, you can wrap your Model in a ScopedModel Widget. This will make the Model available to all descendant Widgets.
The ScopedModelDescendant Widget. Use this Widget to find the appropriate ScopedModel in the Widget tree. It will automatically rebuild whenever the Model notifies that change has taken place.
This library is built upon several features of Flutter:
The Model class implements the Listenable interface
AnimationController and TextEditingController are also Listenables
The Model is passed down the Widget tree using an InheritedWidget. When an InheritedWidget is rebuilt, it will surgically rebuild all of the Widgets that depend on its data. No need to manage subscriptions!
It uses the AnimatedBuilder Widget under the hood to listen to the Model and rebuild the InheritedWidget when the model changes.
Examples
Counter App - Introduction to the tools provided by Scoped Model.
Todo App - Shows how to write a Todo app with persistence and tests.
Usage
Let's demo the basic usage with the all-time favorite: A counter example!
// Start by creating a class that holds some view the app's state. In// our example, we'll have a simple counter that starts at 0 can be // incremented.//// Note: It must extend from Model. classCounterModelextendsModel{
int _counter = 0;
intget counter => _counter;
voidincrement() {
// First, increment the counter
_counter++;
// Then notify all the listeners.notifyListeners();
}
}
// Create our App, which will provide the `CounterModel` to // all children that require it! classCounterAppextendsStatelessWidget{
@overrideWidgetbuild(BuildContext context) {
// First, create a `ScopedModel` widget. This will provide // the `model` to the children that request it. returnnewScopedModel<CounterModel>(
model: newCounterModel(),
child: newColumn(children: [
// Create a ScopedModelDescendant. This widget will get the// CounterModel from the nearest ScopedModel<CounterModel>. // It will hand that model to our builder method, and rebuild // any time the CounterModel changes (i.e. after we // `notifyListeners` in the Model). newScopedModelDescendant<CounterModel>(
builder: (context, child, model) => newText('${model.counter}'),
),
newText("Another widget that doesn't depend on the CounterModel")
])
);
}
}
Finding the Model
There are two ways to find the Model provided by the ScopedModel Widget.
Use the ScopedModelDescendant Widget. It will find the Model and run the
builder function whenever the Model notifies the listeners.
Use the ScopedModel.of static method directly. To make this method more readable for frequent access, you can consider adding your own of method to your own Model classes like so:
classCounterModelextendsModel{
// .../// Wraps [ScopedModel.of] for this [Model].staticCounterModelof(BuildContext context) =>
ScopedModel.of<CounterModel>(context);
}
Listening to multiple Models in a build function
In many cases, it makes sense to split your Models apart into logical components
by functionality. For example, rather than having an AppModel that contains
all of your application logic, it can often make more sense to split models
apart into a UserModel, a SearchModel and a ProductModel, for example.
However, if you need to display information from two of these models in a single
Widget, you might be wondering how to achieve that! To do so, you have two
options:
Use multiple ScopedModelDescendant Widgets
Use multiple ScopedModel.of calls. No need to manage subscriptions,
Flutter takes care of all of that through the magic of InheritedWidgets.
classCombinedWidgetextendsStatelessWidget{
@overrideWidgetbuild(BuildContext context) {
final username =
ScopedModel.of<UserModel>(context, rebuildOnChange: true).username;
final counter =
ScopedModel.of<CounterModel>(context, rebuildOnChange: true).counter;
returnText('$username tapped the button $counter times');
}
}