Saltearse al contenido

RtWatcher

Esta página aún no está disponible en tu idioma.

The RtWatcher widget allows you to watch states and automatically rebuilds the widget tree whenever a monitored state changes.

Syntax

The RtWatcher widget has two ways to use it:

  • Using the default constructor:
RtWatcher(
Widget builder(
BuildContext context,
RtState watch(RtState state),
), {
Key? key,
},
)
  • Using the Builder constructor:
RtWatcher.builder({
Key? key,
Widget? child,
required Widget builder(
BuildContext context,
RtState watch(RtState state),
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. Useful for parts of the UI that don’t depend on the watched states and can remain unchanged. It is passed to the builder function if it is defined.
  • builder : A required function that returns a widget. This function is called whenever any of the states being watched change. It takes three parameters:
    • context: The BuildContext of the RtWatcher widget.
    • watch : A function to watch the state that should trigger a rebuild when it changes.
    • child: The child widget passed to RtWatcher .

Usage

The following example demonstrates how to use the RtlWatcher widget to listen for changes in a state 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 = UseState(0);
10
11
return RtWatcher.builder(
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, watch, child) {
30
return Column(
31
mainAxisAlignment: MainAxisAlignment.center,
32
children: [
33
Text("Count: ${watch(count).value}"),
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
}