RtComponent
The RtComponent
is a StatelessWidget
hat simplifies the creation of reactive widgets by utilizing the dependency injection system.
It provides features similar to RtProvider
and RtConsumer
that are used to inject and consume dependencies.
Syntax
Getters and Methods
builder
: An optional function that creates an instance of theT
dependency. This function is registered by Dependency Injection. If omitted, an attempt will be made to locate it within the closest ancestor where it was initially created.id
: An optional identifier for theT
dependency. If omitted, the dependency will be located by its type(T
).listenStates
: An optional function for defining a list of the states that will rebuild the widget tree defined in therender
method whenever it changes. It exposes the instance of theT
dependency as argument of the function. If omitted, thelistenAll
getter is checked.listenAll
: A boolean value that determines whether to listen to all states(RtState
) of theT
dependency for rebuilds the widget tree defined in therender
method. For default, it is set tofalse
.-
render
: A required function that builds the widget tree based on the instance of theT
dependency. It receives the following arguments:context
: TheBuildContext
of theRtComponent
widget.inst
: The instance of theT
dependency.
Usage
In the following example demonstrates a reactive counter implementation using RtComponent
.
In this example, we introduce two components: Counter
and CounterWithButtons
.
Counter
listens changes in the count
property of CounterController
using the listenStates
getter, and displays its value within the render
method.
On the other hand, CounterWithButtons
provides buttons for incrementing and decrementing the counter and integrates the Counter
component to display the count in its render
method.
Both Counter
and CounterWithButtons
extend RtComponent
with CounterController
as their dependency, use the builder
getter to create an instance of CounterController
and the id
getter to distinguish between different instances of the counter.