Aus öffentlichen Paketdaten erstellte Orientierung.
stateful_props ist ein Flutter-Paket, das wiederverwendbare Verhaltensweisen zwischen StatefulWidgets ermöglicht, indem Zustand und Logik in modulare, zusammensetzbare Einheiten gekapselt werden. Es verbessert die Lesbarkeit und Wartbarkeit des Codes durch Trennung der Verantwortlichkeiten und ermöglicht es Entwicklern, gemeinsame Funktionalitäten ohne Boilerplate zu teilen. Das Paket fördert eine robuste Komponentenarchitektur durch vorhersehbares Zustandsmanagement und saubere API-Exposition. Ideal für komplexe Benutzeroberflächen, die konsistentes Verhalten über mehrere Widgets hinweg erfordern.
Anwendungsfälle
Wiederverwendung der Formularvalidierungslogik
Animationzustände zwischen Widgets teilen
Benutzerinteraktionsmuster kapseln
Modulare UI-Komponenten erstellen
Verwaltung des gemeinsam genutzten Widget-Zustands
Zustandsintensive Widget-Tests vereinfachen
Hauptmerkmale
Modulare Zustandskapselung
Klare Trennung von Logik und Benutzeroberfläche
Einfache Wiederverwendung über StatefulWidgets
Verbesserte Testbarkeit
Minimales Boilerplate
Unterstützung für alle wichtigen Plattformen
Geeignet für
Entwickler, die wiederverwendbare Widget-Verhalten suchen
Teams, die große Flutter-Anwendungen erstellen
Projekte mit komplexen Zustandsinteraktionen
UI-Komponenten, die konsistente Logik erfordern
Diejenigen, die die Widget-Komplexität verringern möchten
Hinweise
Verständnis des StatefulWidget-Lebenszyklus erforderlich
Am besten geeignet für die Wiederverwendung von Zustandslogik
Kann für einfache Fälle Abstraktionsaufwand hinzufügen
Keine vollständige State-Management-Lösung
Beschränkt auf StatefulWidget-spezifische Anwendungsfälle
Original-README
Originaler README-Schnappschuss aus dem verknüpften Quell-Repository. Aktuelle Inhalte finden Sie im Repository.
Provides a simple way to re-use behaviors across StatefulWidgets. Improves readability and robustness of your components.
The package includes pre-made props for many common flutter use cases, these include:
AnimationControllerProp
FocusNodeProp
FutureProp
IntProp, DoubleProp, StringProp, BoolProp
PageControllerProp
ScrollControllerProp
StreamProp
StreamControllerProp
TabControllerProp
TextEditingControllerProp
Generally speaking the built-in props handle common use cases like calling setState when values change and/or calling dispose when required.
To get started, add a StatefulPropsMixin to any StatefulWidget and then use one of the built-in props. In this case we'll use an AnimationControllerProp to manage an AnimationController for us:
You'll notice there are no listeners or builders here... By default the AnimationControllerProp automatically calls setState each time the animation ticks, (disabled by setting autoBuild=false). Also notice that we don't call dispose() on the controller, the prop is handling that for us.
🕹️🕹️ Advanced Usage
Creating New Props
To create a new prop extend the StatefulProp class, and override any of the optional methods:
Don't be intimidated by the number of methods, most props don't override many fields. The most common configuration is to setup some state when constructed, and use dispose to tear it down:
As shown above, if a prop would like to rebuild the state it is attached to it can call manager.scheduleBuild() (which in turn will call setState()). The built-in props all have a autoBuild parameter which automatically adds this listener. This convention is optional but is shown above for reference.
Re-using Stateful Logic
In addition to providing single controllers, props can encapsulate any collection of logic and state that you would like. Either for use across multiple widgets or to isolate for easier testing. For example you could extract common login-related behavior to a LoginBehaviorProp:
This small prop contains 2 controllers, 1 piece of state, 3 helper methods and 2 actions, forming a sort of "micro state" that holds both stateful fields as well as actions which might work on those fields. It also resembles what is commonly referred to as a ViewModel or ViewController.
This fully encapsulated behavior could then be used inside of any StatefulWidget:
In this way props can act as their own reusable behaviors, shared easily across different widgets without potential bugs that come from mixins or the readability issues that come with nested builders.
They also work great as general view controllers, letting you easily separate logic from the view for improved readability.
Flexible & Robust Design
Because each prop is a class, and can nest other props, they fully support inheritence, composition and mixins, allowing you to easily use or extend existing props to create new ones.
For example, in the source code you'll see that a single ValueProp<T> is used as the base class for all the primitives (IntProp, BoolProp, StringProp and DoubleProp) in classic OOP style:
Props can never clash over field or method names because they all have their own self-contained scope. As a result you can stack as many as you need in a single view without concerns. This is in contrast to mixins which will have issues/clashes in this situation.
📖 Background & Motivation
It is difficult to reuse State logic in Flutter. We either end up with a complex and deeply nested build method or have to copy-paste the logic across multiple widgets. For a full discussion, see here: https://github.com/flutter/flutter/issues/51752#.
For example, have you ever written code like this?
Not only is this code overly verbose, but it's also error-prone. If any of the dipose() calls are missed, you will have a bug. If the setState calls is missed, the view will not appear to update. It would be much nicer if each type of object could handle it's own dispose() call, or automatically rebuild the state when changed.
Notice that all the calls to dispose() and setState() have gone away, as each StatefulProp is responsible for disposing itself and (optionally) calling setState when it has changed. This makes the code easier to read and more robust; dispose calls can't get missed, and the work of rebuilding when the counter value changes is done automatically.
While a similar level of robustness could also be achieved using a combination of nested Builder widgets, it would come at the cost of reduced readability.
🐞 Bugs/Requests
If you encounter any problems please open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are welcome.