get_view.dart
2.9 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
import 'package:flutter/widgets.dart';
import '../../../get_instance/src/get_instance.dart';
import '../../../instance_manager.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({Key key}) : super(key: key);
final String tag = null;
T get controller => GetInstance().find<T>(tag: tag);
@override
Widget build(BuildContext context);
}
// abstract class GetView<A, B> extends StatelessWidget {
// const GetView({Key key}) : super(key: key);
// A get controller => GetInstance().find();
// B get controller2 => GetInstance().find();
// @override
// Widget build(BuildContext context);
// }
// abstract class GetView2<A, B, C> extends StatelessWidget {
// const GetView2({Key key}) : super(key: key);
// A get controller => GetInstance().find();
// B get controller2 => GetInstance().find();
// C get controller3 => GetInstance().find();
// @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 GetLifeCycleBase> extends GetWidgetCache {
const GetWidget({Key key}) : super(key: key);
@protected
final String tag = null;
S get controller => GetWidget._cache[this] as S;
static final _cache = <GetWidget, GetLifeCycleBase>{};
@protected
Widget build(BuildContext context);
@override
WidgetCache createWidgetCache() => _GetCache<S>();
}
class _GetCache<S extends GetLifeCycleBase> extends WidgetCache<GetWidget<S>> {
S _controller;
@override
void onInit() {
_controller = Get.find<S>(tag: widget.tag);
GetWidget._cache[widget] = _controller;
super.onInit();
}
@override
void onClose() {
GetWidget._cache.remove(widget);
super.onClose();
}
@override
Widget build(BuildContext context) {
return widget.build(context);
}
}