Saltearse al contenido

RtDependencyRef

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

RtDependencyRef is a class that represents a reference to a dependency managed by Reactter’s dependency injection system.

Syntax

RtDependencyRef<T>([String? id]);

The RtDependencyRef class accepts this parameter:

  • id : (optional) A unique identifier for the dependency.

Usage

To use RtDependencyRef , you need to instantiate it, pass the type of dependency you want to reference and use for event handling, e.g.:

1
import 'package:flutter/material.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
import 'counter.dart';
4
import 'counter_controller.dart';
5
6
class CounterView extends StatelessWidget {
7
const CounterView({Key? key}) : super(key: key);
8
9
@override
10
Widget build(BuildContext context) {
11
Rt.on(
12
RtDependencyRef<CounterController>(),
13
Lifecycle.unregistered,
14
(_, __) => print('CounterController unregistered'),
15
);
16
17
Rt.on(
18
RtDependencyRef<CounterController>(),
19
Lifecycle.registered,
20
(_, __) => print('CounterController registered'),
21
);
22
23
Rt.on(
24
RtDependencyRef<CounterController>(),
25
Lifecycle.created,
26
(_, __) => print('CounterController created'),
27
);
28
29
Rt.on(
30
RtDependencyRef<CounterController>(),
31
Lifecycle.deleted,
32
(_, __) => print('CounterController deleted'),
33
);
27 collapsed lines
34
35
final showCounter = Signal(false);
36
37
return Scaffold(
38
appBar: AppBar(
39
title: const Text("Counter - Lifecycle using EventHandler"),
40
),
41
body: RtSignalWatcher(
42
builder: (context, child) {
43
return Center(
44
child: Column(
45
mainAxisAlignment: MainAxisAlignment.center,
46
children: [
47
ElevatedButton(
48
onPressed: () => showCounter(!showCounter.value),
49
child: showCounter.value
50
? const Text('Hide counter')
51
: const Text('Show Counter'),
52
),
53
const SizedBox(height: 8),
54
if (showCounter.value) const Counter(),
55
],
56
),
57
);
58
},
59
),
60
);
61
}
62
}
1
import 'package:flutter_reactter/flutter_reactter.dart';
2
3
class CounterController {
4
final count = Signal(0);
5
6
CounterController() {
7
Rt.on(this, Lifecycle.willMount, (_, __) {
8
print('CounterController will mount');
9
});
10
11
Rt.on(this, Lifecycle.didMount, (_, __) {
12
print('CounterController did mount');
13
});
14
15
Rt.on(this, Lifecycle.willUpdate, (_, state) {
16
print('CounterController will update by ${state.runtimeType}');
17
});
18
19
Rt.on(this, Lifecycle.didUpdate, (_, state) {
20
print('CounterController did updated by ${state.runtimeType}');
21
});
22
23
Rt.on(this, Lifecycle.willUnmount, (_, __) {
24
print('CounterController will unmount');
25
});
26
27
Rt.on(this, Lifecycle.didUnmount, (_, __) {
28
print('CounterController did unmount');
29
});
30
}
31
32
void increment() {
33
count.value++;
34
}
35
36
void decrement() {
37
count.value--;
38
}
39
}
1
import 'package:flutter/material.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
4
import 'counter_controller.dart';
5
6
class Counter extends StatelessWidget {
7
const Counter({Key? key}) : super(key: key);
8
9
@override
10
Widget build(BuildContext context) {
11
// Provide the `CounterController` dependency to the widget tree
12
return RtProvider<CounterController>(
13
() => CounterController(),
14
builder: (context, counterController, child) {
15
return Row(
16
mainAxisSize: MainAxisSize.min,
17
children: [
18
ElevatedButton(
19
onPressed: counterController.decrement,
20
child: const Icon(Icons.remove),
21
),
22
const SizedBox(width: 8),
23
// Observe the `count` property of the `counterController`
24
// and rebuild the widget tree when the `count` value changes
25
RtConsumer<CounterController>(
26
listenStates: (counterController) => [counterController.count],
27
builder: (context, counterController, child) {
28
return Text("${counterController.count}");
29
},
30
),
31
const SizedBox(width: 8),
32
ElevatedButton(
33
onPressed: counterController.increment,
34
child: const Icon(Icons.add),
35
),
36
],
37
);
38
},
39
);
40
}
41
}
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
}