0. get 文档链接地址

1. 安装

  1. dependencies:
  2. get: ^3.23.1

2. update() 数据动态化更新视图-方法1

image.png

  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. // 使用 GetMaterialApp()
  4. void main() => runApp(GetMaterialApp(home: Home()));
  5. // 定义自己的控制器, 想 更新视图,都需要 调用 update() 方法
  6. class Controller extends GetxController {
  7. var count = 0;
  8. increment() {
  9. count++;
  10. update();
  11. }
  12. }
  13. // 在页面中使用定义的数据源
  14. class Home extends StatelessWidget {
  15. // 定义控制器的实例
  16. final Controller controller = Get.put(Controller());
  17. @override
  18. Widget build(context) {
  19. return Scaffold(
  20. appBar: AppBar(title: Text("getx基础使用")),
  21. body: Center(
  22. child: Column(
  23. children: [
  24. GetBuilder<Controller>(
  25. builder: (_) => Text(
  26. "${controller.count}",
  27. style: TextStyle(color: Colors.black, fontSize: 25.0),
  28. ),
  29. ),
  30. RaisedButton(
  31. onPressed: () => controller.increment(),
  32. child: Text("+1"),
  33. ),
  34. ],
  35. ),
  36. ),
  37. );
  38. }
  39. }

2.1 简单计数器案例

  • 更新全部的视图 ```dart import ‘package:flutter/material.dart’; import ‘package:get/get.dart’;

// 创建初始数据源 class Controller extends GetxController { int _counter = 0; int get counter => _counter; void increment() { _counter++; update(); } }

class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { print(‘SimplePage—build’); // 使用 GetBuilder 创建UI return GetBuilder( init: SimpleController(), builder: (controller) { return Scaffold( appBar: AppBar(title: Text(‘Simple’)), body: Center( child: Text(controller.counter.toString()), ), floatingActionButton: FloatingActionButton( onPressed: () { controller.increment(); }, child: Icon(Icons.add), ), ); }, ); } }

  1. <a name="vkSAz"></a>
  2. #### 2.2 计数器案例,只更新局部UI
  3. - GetBuilder 只包裹需要更新的视图部分
  4. - 使用 `Get.find<SimpleController>()` 找到方法
  5. ```dart
  6. import 'package:flutter/material.dart';
  7. import 'package:get/get.dart';
  8. class SimpleController extends GetxController {
  9. int _counter = 0;
  10. int get counter => _counter;
  11. void increment() {
  12. _counter++;
  13. update();
  14. }
  15. }
  16. class HomePage extends StatelessWidget {
  17. @override
  18. Widget build(BuildContext context) {
  19. print('SimplePage--build');
  20. return Scaffold(
  21. appBar: AppBar(title: Text('Simple')),
  22. body: Center(
  23. // GetBuilder 只包裹需要更新的部分
  24. child: GetBuilder<SimpleController>(
  25. init: SimpleController(),
  26. builder: (c) {
  27. return Text("${c.counter}");
  28. },
  29. ),
  30. ),
  31. floatingActionButton: FloatingActionButton(
  32. onPressed: () {
  33. //使用 Get.find<T>() 找到定义的方法
  34. Get.find<SimpleController>().increment();
  35. },
  36. child: Icon(Icons.add),
  37. ),
  38. );
  39. }
  40. }

2.3 GetxController有生命周期的

  1. class SimpleController extends GetxController {
  2. int _counter = 0;
  3. int get counter => _counter;
  4. void increment() {
  5. _counter++;
  6. update();
  7. }
  8. @override
  9. void onInit() {
  10. super.onInit();
  11. print('SimpleController--onInit');
  12. }
  13. @override
  14. void onReady() {
  15. super.onReady();
  16. print('SimpleController--onReady');
  17. }
  18. @override
  19. void onClose() {
  20. super.onClose();
  21. print('SimpleController--onClose');
  22. }
  23. }

3. GetX() || Obx() 数据动态化更新视图-方法2

和第一种方法差不多, 但是需要使用 .obs后缀,和调用 Obx() 方法

  1. 如果想让你的数据 具备动态化, 就需要在数据的后缀加上 .obs
  2. 在需要改变视图的地方,调用Obx 方法,返回一个 widget

image.png

  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. // 使用 GetMaterialApp()
  4. void main() => runApp(GetMaterialApp(home: Home()));
  5. // 定义自己的控制器 , 需要在引起视图变化的数据后加上 .obs
  6. class Controller extends GetxController {
  7. var count = 0.obs;
  8. // var count = RxInt(0);
  9. void increment() => count++;
  10. }
  11. // 在页面中使用定义的数据源
  12. class Home extends StatelessWidget {
  13. // 定义控制器的实例
  14. final Controller controller = Get.put(Controller());
  15. @override
  16. Widget build(context) {
  17. return Scaffold(
  18. appBar: AppBar(title: Text("getx基础使用2")),
  19. body: Center(
  20. child: Column(
  21. children: [
  22. /// 通过使用 Obx()方法
  23. Obx(
  24. () => Text(
  25. "${controller.count}",
  26. ),
  27. ),
  28. RaisedButton(
  29. onPressed: () => controller.increment(),
  30. child: Text("+1"),
  31. ),
  32. ],
  33. ),
  34. ),
  35. );
  36. }
  37. }

4. 使用 Bindings接口 和 GetView类

1. 定义自己的控制器

  1. // 定义自己的控制器
  2. class HomeController extends GetxController {
  3. var count = 0.obs;
  4. void increment() => count++;
  5. }

2. 实现 Bindings 接口, 重写 dependencies 方法

  1. // 实现 Bindings 接口
  2. class HomeBinding implements Bindings {
  3. @override
  4. void dependencies() {
  5. // 使用Get.lazyPut() 方法推入控制器
  6. Get.lazyPut(() => HomeController());
  7. }
  8. }

3.继承 GetView类, 泛型T: 你要使用的控制器

  1. class Home extends GetView<HomeController> {
  2. @override
  3. Widget build(context) => Scaffold(
  4. appBar: AppBar(title: Text("使用Binding 和 GetView")),
  5. body: Center(
  6. child: Obx(() => Text("数据源:${controller.count}")),
  7. ),
  8. floatingActionButton: FloatingActionButton(
  9. child: Icon(Icons.add),
  10. onPressed: controller.increment,
  11. ),
  12. );
  13. }

4. 在 main函数中配置

通过使用 GetPage(name, page,binding) 方法给每个页面注入不同的依赖,

  1. void main() {
  2. runApp(GetMaterialApp(
  3. initialRoute: '/',
  4. getPages: [
  5. GetPage(name: '/', page: () => Home(), binding: HomeBinding()),
  6. ],
  7. ));
  8. }

5.路由

0. 定义路由

  1. void main() {
  2. runApp(
  3. GetMaterialApp(
  4. initialRoute: '/',
  5. getPages: [
  6. GetPage(name: '/', page: () => MyHomePage()),
  7. GetPage(name: '/second', page: () => Second()),
  8. GetPage(
  9. name: '/third',
  10. page: () => Third(),
  11. transition: Transition.zoom
  12. ),
  13. ],
  14. )
  15. );
  16. }

1. 使用Get.to(page,{其它可选参数}) 进行跳转

  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. main(List<String> args) {
  4. runApp(GetMaterialApp(home: Home()));
  5. }
  6. class Home extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. appBar: AppBar(
  11. title: Text("Home- Page 路由跳转"),
  12. ),
  13. body: FlatButton(
  14. onPressed: () {
  15. Get.to(Mine());
  16. },
  17. child: Text("go to mine"),
  18. ),
  19. );
  20. }
  21. }
  22. class Mine extends StatelessWidget {
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. appBar: AppBar(
  27. title: Text("Mine- page"),
  28. ),
  29. body: Text("Mine- page"),
  30. );
  31. }
  32. }

2. Get.toNamed() 进行命名路由跳转

  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. main(List<String> args) {
  4. runApp(GetMaterialApp(
  5. home: Home(),
  6. // 配置命名路由
  7. getPages: [
  8. GetPage(name: "/mine", page: () => Mine()),
  9. ],
  10. ));
  11. }
  12. // Get.toNamed("/mine");
  13. class Home extends StatelessWidget {
  14. @override
  15. Widget build(BuildContext context) {
  16. return Scaffold(
  17. appBar: AppBar(
  18. title: Text("Home- Page 路由跳转"),
  19. ),
  20. body: FlatButton(
  21. onPressed: () {
  22. Get.toNamed("/mine");
  23. },
  24. child: Text("go to mine"),
  25. ),
  26. );
  27. }
  28. }
  29. class Mine extends StatelessWidget {
  30. @override
  31. Widget build(BuildContext context) {
  32. return Scaffold(
  33. appBar: AppBar(
  34. title: Text("Mine- page"),
  35. ),
  36. body: Text("Mine- page"),
  37. );
  38. }
  39. }

3. 使用 Get.back() 返回上个页面

  1. class Mine extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. appBar: AppBar(
  6. title: Text("Mine- page"),
  7. ),
  8. body: FlatButton(
  9. //也可以这样写 onPressed: Get.back,
  10. onPressed: () {
  11. Get.back();
  12. },
  13. child: Text("返回上个页面"),
  14. ),
  15. );
  16. }
  17. }

4. 使用Get.off() || Get.offAll() 跳转页面

  • Get.off() 导航到下一个页面并删除前一个页面
  • Get.offAll() 导航到下一个页面并删除以前所有的页面

    该方法和 Get.to() 实现的效果一样, 但是使用此方法跳转后,在下一个页面,不能再返回前一个页面, 可以用在 登录页,获取退出页面,这样登录后,或者退出页面后,就不能再返回到前一个页面

  1. class Home extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. appBar: AppBar(
  6. title: Text("Home- Page 路由跳转"),
  7. ),
  8. body: FlatButton(
  9. onPressed: () {
  10. Get.off(Mine());
  11. },
  12. child: Text("go to mine"),
  13. ),
  14. );
  15. }
  16. }

5. arguments传参数和arguments参数接收

  1. // Get.to(Mine(), arguments: '我是参数'); 进行传参
  2. class Home extends StatelessWidget {
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text("Home- Page 路由跳转"),
  8. ),
  9. body: FlatButton(
  10. onPressed: () {
  11. Get.to(Mine(), arguments: '我是参数');
  12. },
  13. child: Text("go to mine"),
  14. ),
  15. );
  16. }
  17. }
  18. // 使用 Get.arguments 接收参数
  19. class Mine extends StatelessWidget {
  20. @override
  21. Widget build(BuildContext context) {
  22. String name = Get.arguments;
  23. print(name);
  24. return Scaffold(
  25. appBar: AppBar(
  26. title: Text("Mine- page"),
  27. ),
  28. body: FlatButton(
  29. onPressed: () {
  30. Get.back();
  31. },
  32. child: Text("返回上个页面"),
  33. ),
  34. );
  35. }
  36. }

6. 像web一样携带参数(url?name=李四&age=12) 模式传参 和 参数的接收

  1. class Home extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. appBar: AppBar(
  6. title: Text("Home- Page 路由跳转"),
  7. ),
  8. body: FlatButton(
  9. onPressed: () {
  10. Get.toNamed("/mine?name=李四&age=12");
  11. },
  12. child: Text("go to mine"),
  13. ),
  14. );
  15. }
  16. }
  17. class Mine extends StatelessWidget {
  18. @override
  19. Widget build(BuildContext context) {
  20. // 参数接收
  21. String age = Get.parameters['age'];
  22. print(age);
  23. String name = Get.parameters['name'];
  24. print(name);
  25. return Scaffold(
  26. appBar: AppBar(
  27. title: Text("Mine- page"),
  28. ),
  29. );
  30. }
  31. }

7. 动态命名路由传参(/profile/:user)

  • 同上边那种接收参数方法一样 ```dart main(List args) { runApp(GetMaterialApp( home: Home(), // 配置命名路由 getPages: [
    1. GetPage(name: "/mine/:name/:age", page: () => Mine()),
    ], )); } class Home extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold(
    1. appBar: AppBar(
    2. title: Text("Home- Page 路由跳转"),
    3. ),
    4. body: FlatButton(
    5. onPressed: () {
    6. // 传参
    7. Get.toNamed("/mine/李四/12");
    8. },
    9. child: Text("go to mine"),
    10. ),
    ); } }

class Mine extends StatelessWidget { @override Widget build(BuildContext context) { // 接收参数 String age = Get.parameters[‘age’]; print(age); String name = Get.parameters[‘name’]; print(name);

  1. return Scaffold(
  2. appBar: AppBar(
  3. title: Text("Mine- page"),
  4. ),
  5. );

} }

  1. <a name="wtkBk"></a>
  2. #### 8. await Get.to()导航到下一条路由,并在返回后立即接收或更新数据。
  3. <a name="Xz8Ph"></a>
  4. #
  5. <a name="Ppjbg"></a>
  6. # 5. GetMaterialApp 类
  7. > 该类和 MaterialApp 类用法基本类似,具体参考源码
  8. - 官方例子,参考下
  9. ```dart
  10. void main() {
  11. runApp(GetMaterialApp(
  12. initialRoute: '/home',
  13. defaultTransition: Transition.native,
  14. translations: MyTranslations(),
  15. locale: Locale('pt', 'BR'),
  16. getPages: [
  17. GetPage(name: '/home', page: () => First()),
  18. ],
  19. ));
  20. }