Saltearse al contenido

ReactterScope

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

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

Syntax

ReactterScope({
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 ReactterScope widget.

Example

In the following example, we have a simple counter application that uses the ReactterScope 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 ReactterScope(
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
ReactterConsumer(
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
}