Saltearse al contenido

RtScope

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

The RtScope widget serves as an entry point for Reactter, allowing you to use any consumer(such as RtConsumer , RtSelector , RtComponent , BuildContext.watch , BuildContext.select ) to observe state changes within the subtree of the widget it wraps, without the need to explicitly define dependencies.

Syntax

RtScope({
Key? key,
required Widget child,
})

Properties

  • key: An optional Key to use for identifying the widget.
  • child: The Widget that is to be wrapped by the RtScope widget.

Usage

In the following example, we have a simple counter application that uses the RtScope widget to listen to the state changes of the count value.

1
import 'package:flutter/material.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
import 'counter_view.dart';
4
5
void main() {
6
runApp(
7
const RtScope(
8
child: MyApp(),
9
),
10
);
11
}
12
13
class MyApp extends StatelessWidget {
14
const MyApp({Key? key}) : super(key: key);
15
16
@override
17
Widget build(BuildContext context) {
18
return const MaterialApp(
19
home: CounterView(),
20
);
21
}
22
}
1
import 'package:flutter/material.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
4
final count = Signal(0);
5
6
class CounterView extends StatelessWidget {
7
const CounterView({Key? key}) : super(key: key);
8
9
@override
10
Widget build(BuildContext context) {
11
return Scaffold(
12
appBar: AppBar(
13
title: const Text("Counter"),
14
),
15
body: Center(
16
child: Row(
17
mainAxisAlignment: MainAxisAlignment.center,
18
children: [
19
ElevatedButton(
20
onPressed: () => count.value--,
21
child: const Icon(Icons.remove),
22
),
23
const SizedBox(width: 8),
24
RtConsumer(
25
listenStates: (_) => [count],
26
builder: (_, __, ___) {
27
return Text('$count');
28
},
29
),
30
const SizedBox(width: 8),
31
ElevatedButton(
32
onPressed: () => count.value++,
33
child: const Icon(Icons.add),
34
),
35
],
36
),
37
),
38
);
39
}
40
}