The RtSelector widget is a similarly to RtConsumer.
It obtains the dependency provided by the closest RtProvider widget and allows you to select specific states to compute a value.
This value will trigger a rebuild of the widget tree whenever it changes.
Syntax
Properties
key: An optional Key to use for identifying the widget.
id: An optional String to identify the selector.
child: An optional Widget that remains static while the widget tree is rebuilt.
If defined, it is passed to the builder function.
selector: A function that computes a value V from one or more states and listens for changes to rebuild the widget tree when the value computed changes.
It receives the following arguments:
instance: The instance of T dependency provided by the closest RtProvider widget.
select: A function that allows you to wrap the state(RtState) to be listened for changes and returns it.
builder: A function that rebuilds a widget depending on the RtSelector.
It receives the following arguments:
context: The BuildContext of the RtSelector widget.
instance: The instance of T dependency provided by the closest RtProvider widget.
value: The selected value computed V by the selector function.
child: The child widget passed to RtSelector.
Usage
Basic Usage
To use RtSelector, wrap it around the widget that you want to rebuild when the selected value changes.
Here’s an example of how to use it:
counter_divisible.dart
counter_view.dart
counter.dart
main.dart
counter_controller.dart
In this example, checks if the count state from CounterController is divisible by a specified number (byNum).
The RtSelector widget is used to perform this check and rebuild the widget tree whenever the divisibility status changes.
Using child
RtSelector offers the child property to pass a widget that is independent of the RtSelector and is passed to the builder function.
This is useful when you want to reuse a widget.
Here’s an example of how to use it:
counter_divisible.dart
counter_view.dart
counter.dart
main.dart
counter_controller.dart
In this example, checks if the count state from CounterController is divisible by a specified number (byNum).
The child property in RtSelector is a RtConsumer widget that listens to changes in the count state and displays its value.
This child widget remains static and does not rebuild when the divisibility status changes, optimizing performance.
Using id
RtSelector offers the id property to locate the T dependency by its identifier.
This is useful when you have multiple instances of the same dependency type and need to specify which one to use.
Here’s an example of how to use it:
counter_divisible.dart
counter_view.dart
counter.dart
main.dart
counter_controller.dart
In this example, every CounterDivisible widget checks if the count state from CounterController identified by id is divisible by a specified number (byNum).
This flexibility ensures that the correct state data is used and displayed for each instance.