1 Get.put() Get.find()

父界面负责 put 放入 controller
子界面负责 find 取出 controller

  1. import 'package:get/get.dart';
  2. class CountController extends GetxController {
  3. final count = 0.obs;
  4. add() => count.value++;
  5. @override
  6. void onInit() {
  7. super.onInit();
  8. print("onInit");
  9. }
  10. @override
  11. void onClose() {
  12. super.onClose();
  13. print("clode");
  14. }
  15. }
  1. class StateDependencyPutFindView extends StatelessWidget {
  2. StateDependencyPutFindView({Key? key}) : super(key: key);
  3. final controller = Get.put<CountController>(CountController());
  4. @override
  5. Widget build(BuildContext context) {
  6. return Scaffold(
  7. appBar: AppBar(
  8. title: Text("Dependency"),
  9. ),
  10. body: Center(
  11. child: Column(
  12. children: [
  13. GetX<CountController>(
  14. init: controller,
  15. initState: (_) {},
  16. builder: (_) {
  17. return Text('value -> ${_.count}');
  18. },
  19. ),
  20. Divider(),
  21. // 按钮
  22. ElevatedButton(
  23. onPressed: () {
  24. controller.add();
  25. },
  26. child: Text('add'),
  27. ),
  28. // 跳转
  29. ElevatedButton(
  30. onPressed: () {
  31. Get.to(() => NextPageView());
  32. },
  33. child: Text('next page'),
  34. ),
  35. ],
  36. ),
  37. ),
  38. );
  39. }
  40. }
  1. class NextPageView extends StatelessWidget {
  2. NextPageView({Key? key}) : super(key: key);
  3. final controller = Get.find<CountController>();
  4. @override
  5. Widget build(BuildContext context) {
  6. return Scaffold(
  7. appBar: AppBar(
  8. title: Text("NextPage"),
  9. ),
  10. body: Center(
  11. child: Column(
  12. children: [
  13. GetX<CountController>(
  14. init: controller,
  15. initState: (_) {},
  16. builder: (_) {
  17. return Text('value -> ${_.count}');
  18. },
  19. ),
  20. Divider(),
  21. ],
  22. ),
  23. ),
  24. );
  25. }
  26. }

2 Get.lazyput() GetView

lazyput, 会先注册回调函数, 当调用Get.find()时自动触发该回调, put一个controller实例

  1. 同上
  1. import 'package:get/get.dart';
  2. import 'controller.dart';
  3. class DependencyLazyPutBinding implements Bindings {
  4. @override
  5. void dependencies() {
  6. Get.lazyPut<CountController>(() => CountController());
  7. }
  8. }
  1. class StateDependencyLazyPutView extends StatelessWidget {
  2. StateDependencyLazyPutView({Key? key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text("Dependency - LazyPut"),
  8. ),
  9. body: Center(
  10. child: Column(
  11. children: [
  12. GetX<CountController>(
  13. init: Get.find<CountController>(),
  14. initState: (_) {},
  15. builder: (_) {
  16. return Text('value -> ${_.count}');
  17. },
  18. ),
  19. Divider(),
  20. // 按钮
  21. ElevatedButton(
  22. onPressed: () {
  23. Get.find<CountController>().add();
  24. },
  25. child: Text('add'),
  26. ),
  27. // 跳转
  28. ElevatedButton(
  29. onPressed: () {
  30. Get.to(NextPageView());
  31. },
  32. child: Text('Next GetView Page'),
  33. ),
  34. ],
  35. ),
  36. ),
  37. );
  38. }
  39. }
  1. class NextPageView extends GetView<CountController> {
  2. NextPageView({Key? key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text("GetView Page"),
  8. ),
  9. body: Center(
  10. child: Column(
  11. children: [
  12. Obx(() => Text('value -> ${controller.count}')),
  13. Divider(),
  14. // 按钮
  15. ElevatedButton(
  16. onPressed: () {
  17. controller.add();
  18. },
  19. child: Text('add'),
  20. ),
  21. ],
  22. ),
  23. ),
  24. );
  25. }
  26. }
  1. GetPage(
  2. name: AppRoutes.Lazy,
  3. page: () => StateDependencyLazyPutView(),
  4. binding: DependencyLazyPutBinding()
  5. )