Skip to content

RtSignalWatcher

The RtSignalWatcher widget provides a mechanism to rebuild a widget tree whenever a Signal within its scope changes. It acts as a listener for all Signal instances within its subtree, triggering a rebuild when any of these signals emit new values.

Syntax

RtSignalWatcher({
Key? key,
Widget child,
Widget builder(BuildContext context, Widget? child),
})

Properties

  • key: An optional Key to use for identifying the widget.
  • child: An optional Widget that remains static while the widget tree is rebuilt. It is passed to the builder function if it is defined.
  • builder : A required function that builds a widget tree based on the current state of the signals. It receives the following arguments:
    • context: The BuildContext of the RtSignalWatcher widget.
    • child: The child widget passed to RtSignalWatcher .

Usage

The following example demonstrates how to use the RtSignalWatcher widget to listen for changes in a Signal and rebuild the widget tree accordingly.

1
import 'package:flutter/material.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
4
class Counter extends StatelessWidget {
5
const Counter({Key? key}) : super(key: key);
6
7
@override
8
Widget build(BuildContext context) {
9
final count = Signal(0);
10
11
return RtSignalWatcher(
12
// This widget remains static and does not rebuild when `count` changes.
13
// It is passed as a `child` to the `builder` function.
14
child: Row(
15
mainAxisAlignment: MainAxisAlignment.center,
16
children: [
17
ElevatedButton(
18
child: const Icon(Icons.remove),
19
onPressed: () => count.value--,
20
),
21
SizedBox(width: 8),
22
ElevatedButton(
23
child: const Icon(Icons.add),
24
onPressed: () => count.value++,
25
),
26
],
27
),
28
// Rebuid the widget when `count` changes
29
builder: (context, child) {
30
return Column(
31
mainAxisAlignment: MainAxisAlignment.center,
32
children: [
33
Text("Count: $count"),
34
SizedBox(height: 8),
35
child!,
36
],
37
);
38
},
39
);
40
}
41
}
1
import 'package:flutter/material.dart';
2
import 'counter.dart';
3
4
class CounterView extends StatelessWidget {
5
const CounterView({Key? key}) : super(key: key);
6
7
@override
8
Widget build(BuildContext context) {
9
return Scaffold(
10
appBar: AppBar(
11
title: const Text("Counter"),
12
),
13
body: const Center(
14
child: Counter(),
15
),
16
);
17
}
18
}
1
import 'package:flutter/material.dart';
2
import 'counter_view.dart';
3
4
void main() {
5
runApp(MyApp());
6
}
7
8
class MyApp extends StatelessWidget {
9
@override
10
Widget build(BuildContext context) {
11
return MaterialApp(
12
home: CounterView(),
13
);
14
}
15
}