Skip to content

Commit a0a297c

Browse files
committed
doc: Update ReactterSelector documentation with usage examples
1 parent 3774ec8 commit a0a297c

1 file changed

Lines changed: 130 additions & 3 deletions

File tree

website/src/content/docs/widgets/reactter_selector.mdx

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,133 @@ sidebar:
55
order: 5
66
---
77

8-
:::tip[TODO]
9-
This page is still in progress.
10-
:::
8+
import { HM, HT } from '@/components/Highlight';
9+
import { Code } from "@astrojs/starlight/components";
10+
import counterControllerCode from '@/examples/counter/lib/counter_controller.dart?raw';
11+
12+
The <HT>`ReactterSelector`</HT> widget is an similarly to <HT>[`ReactterConsumer`](/reactter/widgets/reactter_consumer)</HT>. It obtains the dependency provided by the closest <HT>[`ReactterProvider`](/reactter/widgets/reactter_provider)</HT> widget and allows you to select a specific value from the state to rebuild the widget tree when the value changes.
13+
14+
15+
## Syntax
16+
17+
```dart showLineNumbers=false
18+
ReactterSelector<T, V>({
19+
Key? key,
20+
String? id,
21+
Widget? child,
22+
required V selector(
23+
T instance,
24+
ReactterState select(ReactterState state),
25+
),
26+
required Widget builder(
27+
BuildContext context,
28+
T instance,
29+
V value,
30+
Widget? child,
31+
),
32+
})
33+
```
34+
35+
## Properties
36+
37+
- `key`: An optional <HT>`Key`</HT> to use for identifying the widget.
38+
- `id`: An optional <HT>`String`</HT> to identify the selector.
39+
- `child`: An optional <HT>`Widget`</HT> which is independent of the <HT>`ReactterSelector`</HT>.
40+
If defined, it is passed to the <HM>`builder`</HM> function.
41+
- <HM>`selector`</HM>: A function that computes a value <HT>`V`</HT> from one or more states and listens for changes to rebuild the widget tree when the value changes.
42+
It receives the following arguments:
43+
- `instance`: The instance of <HT>`T`</HT> dependency provided by the closest <HT>`ReactterProvider`</HT> widget.
44+
- `select`: A function that allows you to wrap the state to be listened for changes and returns it.
45+
- <HM>`builder`</HM>: A function that builds a widget depending on the <HT>`ReactterSelector`</HT>.
46+
It receives the following arguments:
47+
- `context`: The <HT>`BuildContext`</HT> of the widget. A handle to the location of <HT>`ReactterSelector`</HT> in the widget tree.
48+
- `instance`: The instance of <HT>`T`</HT> dependency provided by the closest <HT>`ReactterProvider`</HT> widget.
49+
- `value`: The selected value computed <HT>`V`</HT> by the <HM>`selector`</HM> function.
50+
- `child`: The `child` widget passed to the <HT>`ReactterSelector`</HT> widget.
51+
52+
53+
## Usage
54+
55+
### Basic Usage
56+
57+
In the following example, we have a simple counter application that uses the <HT>`ReactterSelector`</HT> widget to select a specific value from the state to rebuild the widget tree when the value changes.
58+
59+
<Code title="counter_controller.dart" code={counterControllerCode} lang="dart" mark={["count", "Signal"]} collapse={["1-100"]} />
60+
61+
```dart title="counter_view.dart" collapse={1-24, 29-32, 35-58} "ReactterSelector" "context.use" "ReactterConsumer" "ReactterProvider" "CounterController"
62+
import 'package:flutter/material.dart';
63+
import 'package:flutter_reactter/flutter_reactter.dart';
64+
import 'counter_controller.dart';
65+
66+
class CounterView extends StatelessWidget {
67+
const CounterView({Key? key}) : super(key: key);
68+
69+
@override
70+
Widget build(BuildContext context) {
71+
// Provides the `CounterController` dependency to the widget tree
72+
return ReactterProvider<CounterController>(
73+
() => CounterController(),
74+
child: Scaffold(
75+
appBar: AppBar(
76+
title: const Text("Counter"),
77+
),
78+
body: const Center(
79+
child: Counter(),
80+
),
81+
),
82+
);
83+
}
84+
}
85+
86+
class Counter extends StatelessWidget {
87+
const Counter({Key? key}) : super(key: key);
88+
89+
Widget build(BuildContext context) {
90+
// Obtains the `CounterController` dependency
91+
// provided by the closest `ReactterProvider` widget
92+
final counterController = context.use<CounterController>();
93+
94+
return Column(
95+
children: [
96+
Row(
97+
mainAxisAlignment: MainAxisAlignment.center,
98+
children: [
99+
ElevatedButton(
100+
onPressed: counterController.decrement,
101+
child: const Icon(Icons.remove),
102+
),
103+
const SizedBox(width: 8),
104+
// Listens the `CounterController`
105+
// and rebuilds the widget tree when it changes
106+
ReactterConsumer<CounterController>(
107+
listenAll: true,
108+
builder: (context, counterController, child) {
109+
return Text("${counterController.count}");
110+
},
111+
),
112+
const SizedBox(width: 8),
113+
ElevatedButton(
114+
onPressed: counterController.increment,
115+
child: const Icon(Icons.add),
116+
),
117+
],
118+
),
119+
const SizedBox(height: 8),
120+
// Selects the `count` state from the `CounterController`,
121+
// calculates if the `count` is divided by 3
122+
// and rebuilds the widget tree when the value(`isDividedBy3`) changes
123+
ReactterSelector<CounterController, bool>(
124+
selector: (counterController, select) {
125+
return select(counterController.count).value % 3 == 0;
126+
},
127+
builder: (context, counterController, isDividedBy3, child) {
128+
return Text(
129+
isDividedBy3 ? "Divided by 3" : "Not divided by 3",
130+
);
131+
},
132+
),
133+
],
134+
);
135+
}
136+
}
137+
```

0 commit comments

Comments
 (0)