get_controllers.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// ignore: prefer_mixin
import 'package:flutter/widgets.dart';
import '../../../instance_manager.dart';
import '../rx_flutter/rx_disposable.dart';
import '../rx_flutter/rx_notifier.dart';
import 'list_notifier.dart';
// ignore: prefer_mixin
abstract class GetxController extends DisposableInterface
with ListenableMixin, ListNotifierMixin {
/// Rebuilds `GetBuilder` each time you call `update()`;
/// Can take a List of [ids], that will only update the matching
/// `GetBuilder( id: )`,
/// [ids] can be reused among `GetBuilders` like group tags.
/// The update will only notify the Widgets, if [condition] is true.
void update([List<Object>? ids, bool condition = true]) {
if (!condition) {
return;
}
if (ids == null) {
refresh();
} else {
for (final id in ids) {
refreshGroup(id);
}
}
}
}
mixin ScrollMixin on GetLifeCycleBase {
final ScrollController scroll = ScrollController();
@override
void onInit() {
super.onInit();
scroll.addListener(_listener);
}
bool _canFetchBottom = true;
bool _canFetchTop = true;
void _listener() {
if (scroll.position.atEdge) {
_checkIfCanLoadMore();
}
}
Future<void> _checkIfCanLoadMore() async {
if (scroll.position.pixels == 0) {
if (!_canFetchTop) return;
_canFetchTop = false;
await onTopScroll();
_canFetchTop = true;
} else {
if (!_canFetchBottom) return;
_canFetchBottom = false;
await onEndScroll();
_canFetchBottom = true;
}
}
Future<void> onEndScroll();
Future<void> onTopScroll();
@override
void onClose() {
scroll.removeListener(_listener);
super.onClose();
}
}
abstract class RxController extends DisposableInterface {}
abstract class SuperController<T> extends FullLifeCycleController
with FullLifeCycleMixin, StateMixin<T> {}
abstract class FullLifeCycleController extends GetxController
with
// ignore: prefer_mixin
WidgetsBindingObserver {}
mixin FullLifeCycleMixin on FullLifeCycleController {
@mustCallSuper
@override
void onInit() {
super.onInit();
ambiguate(WidgetsBinding.instance)?.addObserver(this);
}
@mustCallSuper
@override
void onClose() {
ambiguate(WidgetsBinding.instance)?.removeObserver(this);
super.onClose();
}
@mustCallSuper
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
onResumed();
break;
case AppLifecycleState.inactive:
onInactive();
break;
case AppLifecycleState.paused:
onPaused();
break;
case AppLifecycleState.detached:
onDetached();
break;
case AppLifecycleState.hidden:
onHidden();
break;
}
}
void onResumed();
void onPaused();
void onInactive();
void onDetached();
void onHidden();
}