get_view.dart
2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import 'package:flutter/widgets.dart';
import '../../../instance_manager.dart';
import '../../../utils.dart';
import 'get_state.dart';
import 'get_widget_cache.dart';
/// GetView is a great way of quickly access your Controller
/// without having to call Get.find<AwesomeController>() yourself.
///
/// Sample:
/// ```
/// class AwesomeController extends GetxController {
/// final String title = 'My Awesome View';
/// }
///
/// class AwesomeView extends GetView<AwesomeController> {
/// /// if you need you can pass the tag for
/// /// Get.find<AwesomeController>(tag:"myTag");
/// @override
/// final String tag = "myTag";
///
/// AwesomeView({Key key}):super(key:key);
///
/// @override
/// Widget build(BuildContext context) {
/// return Container(
/// padding: EdgeInsets.all(20),
/// child: Text( controller.title ),
/// );
/// }
/// }
///``
abstract class GetView<T> extends StatelessWidget {
const GetView({super.key});
final String? tag = null;
T get controller => Get.find<T>(tag: tag)!;
@override
Widget build(BuildContext context);
}
/// GetWidget is a great way of quickly access your individual Controller
/// without having to call Get.find<AwesomeController>() yourself.
/// Get save you controller on cache, so, you can to use Get.create() safely
/// GetWidget is perfect to multiples instance of a same controller. Each
/// GetWidget will have your own controller, and will be call events as `onInit`
/// and `onClose` when the controller get in/get out on memory.
abstract class GetWidget<S extends GetLifeCycleMixin> extends GetWidgetCache {
const GetWidget({super.key});
@protected
final String? tag = null;
S get controller => GetWidget._cache[this] as S;
// static final _cache = <GetWidget, GetLifeCycleBase>{};
static final _cache = Expando<GetLifeCycleMixin>();
@protected
Widget build(BuildContext context);
@override
WidgetCache createWidgetCache() => _GetCache<S>();
}
class _GetCache<S extends GetLifeCycleMixin> extends WidgetCache<GetWidget<S>> {
S? _controller;
bool _isCreator = false;
InstanceInfo? info;
@override
void onInit() {
info = Get.getInstanceInfo<S>(tag: widget!.tag);
_isCreator = info!.isPrepared && info!.isCreate;
if (info!.isRegistered) {
_controller = Get.find<S>(tag: widget!.tag);
}
GetWidget._cache[widget!] = _controller;
super.onInit();
}
@override
void onClose() {
if (_isCreator) {
Get.asap(() {
widget!.controller.onDelete();
Get.log('"${widget!.controller.runtimeType}" onClose() called');
Get.log('"${widget!.controller.runtimeType}" deleted from memory');
// GetWidget._cache[widget!] = null;
});
}
info = null;
super.onClose();
}
@override
Widget build(BuildContext context) {
return Binder(
init: () => _controller,
child: widget!.build(context),
);
}
}