main.dart
2.21 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/material.dart';
import 'package:get/get.dart';
void main() {
//MyBindings().dependencies();
runApp(
Binds(
binds: [
Bind.lazyPut(() => Controller()),
Bind.lazyPut(() => Controller2()),
],
child: GetMaterialApp(
home: Home(),
),
),
);
}
class MyBindings extends Binding {
@override
List<Bind> dependencies() {
return [
Bind.put(Controller()),
Bind.put(Controller2()),
];
}
}
class Controller extends GetxController {
final count = 0.obs;
void increment() {
count.value++;
update();
}
}
class Controller2 extends GetxController {
final count = 0.obs;
Controller2();
void increment() {
count.value++;
update();
}
}
class Home extends ObxStatelessWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
print('sasasasa');
return Scaffold(
appBar: AppBar(title: Text("counter")),
body: Builder(builder: (context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Builder(builder: (context) {
print('builder');
final controller = context.listen<Controller>();
return Text('${controller.count.value}');
}),
ElevatedButton(
child: Text('Next Route'),
onPressed: () {
Get.to(() => Second());
},
),
],
),
);
}),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
Get.find<Controller>().increment();
},
),
);
}
}
class Second extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
floatingActionButton: FloatingActionButton(
onPressed: () {
context.get<Controller2>().increment();
},
),
body: Center(
child: Builder(builder: (context) {
final ctrl = context.listen<Controller2>();
return Text("${ctrl.count}");
}),
),
);
}
}