Padding( padding: EdgeInsets.all(10), child: ElevatedButton( child: Text("判断是否是手机号"), onPressed: () async { if (GetUtils.isPhoneNumber(textFieldController.text)) { Get.snackbar("正确", "恭喜你, 完全正确", backgroundColor: Colors.greenAccent); } else { Get.snackbar( "手机号错误", "请输入正确的手机号", backgroundColor: Colors.pink ); } }, ), ),总共有几十个方法,使用的时候可以点进源码查看。
void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return GetMaterialApp( title: "GetX", home: GetViewAndGetWidgetExample(), ); } }2、GetViewCountController
class GetViewCountController extends GetxController{ var count = 0.obs; void increments(){ count++; } }3、GetViewAndGetWidgetExample继承自GetView并使用状态管理
import 'package:flutter/material.dart'; import 'package:flutter_getx_example/GetViewAndGetWidgetExample/GetViewCountController.dart'; import 'package:get/get.dart'; class GetViewAndGetWidgetExample extends GetView<GetViewCountController> { @override Widget build(BuildContext context) { Get.put(GetViewCountController()); //Get.create(() => GetViewCountController()); 不同之处在这里 return Scaffold( appBar: AppBar( title: Text("GetX GetView"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Obx(() => Text( "count的值为: ${controller?.count}", style: TextStyle( color: Colors.red, fontSize: 30 ), )), SizedBox(height: 20,), ElevatedButton( onPressed: () { controller.increment(); }, child: Text("点我加1")) ], ), ), ); } }三、GetWidget(较少使用)
import 'package:flutter/material.dart'; import 'package:flutter_getx_example/GetViewAndGetWidgetExample/GetViewCountController.dart'; import 'package:get/get.dart'; class GetViewAndGetWidgetExample extends GetWidget<GetViewCountController> { @override Widget build(BuildContext context) { // 堆代码 duidaima.com // Get.put(GetViewCountController());不同之处在这里 Get.create(() => GetViewCountController()); return Scaffold( appBar: AppBar( title: Text("GetX GetView"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Obx(() => Text( "count的值为: ${controller?.count}", style: TextStyle( color: Colors.red, fontSize: 30 ), )), SizedBox(height: 20,), ElevatedButton( onPressed: () { controller.increment(); }, child: Text("点我加1")) ], ), ), ); } }
在我们平时的开发过程中基本上很少会用到GetWidget,因为我们在大部分情况下都不需要缓存Controller。 当我们的页面中只依赖了一个控制器的情况话,那么使用GetView将是很好的选择,因为他大大简化了我们的代码。
import 'package:flutter_getx_example/ObxCustomClassExample/Teacher.dart'; import 'package:get/get.dart'; class MyController extends GetxController { // 第一种 // var teacher = Teacher(); // // void convertToUpperCase() { // teacher.name.value = teacher.name.value.toUpperCase(); // } // 第二种 // var teacher = Teacher(name: "Jimi", age: 18).obs; // void convertToUpperCase() { // teacher.update((val) { // teacher.value.name = teacher.value.name.toString().toUpperCase(); // }); // } // 第三种 var teacher = Teacher(); void convertToUpperCase() { teacher.name.value = teacher.name.value.toUpperCase(); update(); } }2、实例化控制器并使用
import 'package:flutter_getx_example/ObxCustomClassExample/Teacher.dart'; import 'package:get/get.dart'; class MyController extends GetxController { // 第一种 // var teacher = Teacher(); // // void convertToUpperCase() { // teacher.name.value = teacher.name.value.toUpperCase(); // } // 第二种 // var teacher = Teacher(name: "Jimi", age: 18).obs; // void convertToUpperCase() { // teacher.update((val) { // teacher.value.name = teacher.value.name.toString().toUpperCase(); // }); // } // 第三种 var teacher = Teacher(); void convertToUpperCase() { teacher.name.value = teacher.name.value.toUpperCase(); update(); } } ###第三步:实例化控制器并使用 import 'package:flutter/material.dart'; import 'package:flutter_getx_example/GetXControllerExample/MyController.dart'; import 'package:get/get.dart'; // 这里也可以继承自GetView进行处理 class GetXControllerExample extends StatelessWidget { // 第一种 MyController myController = Get.put(MyController()); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("GetX Obx---GetXController"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ // 第一种 // Obx(() => Text( // "我的名字是 ${myController.teacher.name}", // style: TextStyle(color: Colors.red, fontSize: 30), // )), // 第二种 // GetX<MyController>( // init: MyController(), // builder: (controller) { // return Text( // "我的名字是 ${controller.teacher.name}", // style: TextStyle(color: Colors.green, fontSize: 30), // ); // }, // ), // 第三种 GetBuilder<MyController>( init: myController, builder: (controller) { return Text( "我的名字是 ${controller.teacher.name}", style: TextStyle(color: Colors.green, fontSize: 30), ); }, ), SizedBox(height: 20,), ElevatedButton( onPressed: () { // 第一种 myController.convertToUpperCase(); // 第二种 // Get.find<MyController>().convertToUpperCase(); }, child: Text("转换为大写")) ], ), ), ); } }四、GetxController事件监听
import 'package:get/get.dart'; class WorkersController extends GetxController { var count = 0.obs; void increment() { count++; } }2、重写onInit并监听事件
@override void onInit() { // TODO: implement onInit // 监听count的值,当它发生改变的时候调用 ever(count, (callback) => print("ever----$count")); // 监听多个值,当它们发生改变的时候调用 everAll([count], (callback) => print("everAll----$count")); // count值改变时调用,只执行一次 once(count, (callback) => print("once----$count")); // 用户停止打字时1秒后调用,主要是防DDos debounce(count, (callback) => print("debounce----$count")); // 忽略3秒内的所有变动 interval(count, (callback) => print("interval----$count")); super.onInit(); }3、实例化控制器并使用
import 'package:flutter/material.dart'; import 'package:flutter_getx_example/GetXControllerWorkersExample/WorkersConroller.dart'; import 'package:get/get.dart'; class GetXControllerWorkersExample extends StatelessWidget { WorkersController workersController = Get.put(WorkersController()); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("GetXWorkersController"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ ElevatedButton( onPressed: () => workersController.increment(), child: Text("增加") ), Padding( padding: EdgeInsets.all(16), child: TextField( onChanged: (val) { workersController.increment(); }, ), ) ], ), ), ); } }4、多次点击输入结果
flutter: ever----1 flutter: everAll----1 flutter: once----1 [GETX] Worker [once] called flutter: debounce----1 flutter: interval----1 flutter: ever----2 flutter: everAll----2 flutter: debounce----2 flutter: interval----2 flutter: ever----3 flutter: everAll----3 flutter: ever----4 flutter: everAll----4 flutter: ever----5 flutter: everAll----5 flutter: interval----5 flutter: debounce----5
import 'package:get/get.dart'; class MyLifecycleController extends GetxController { var count = 0; void increment() async { await Future.delayed(Duration(milliseconds: 3000));//延时函数 count++; update(); } void cleanTask() { print("清除了任务"); } }2、重写GetxController生命周期方法
@override void onInit() { // TODO: implement onInit print("初始化"); super.onInit(); } @override void onReady() { // TODO: implement onReady print("加载完成"); super.onReady(); } @override void onClose() { // TODO: implement onClose print("控制器被释放"); super.onClose(); }3、实例化控制器并使用
import 'package:flutter/material.dart'; import 'package:flutter_getx_example/GetXControllerLifecycleMethodsExample/MyLifecycleController.dart'; import 'package:get/get.dart'; class GetXControllerLifecycleMethodExample extends StatelessWidget { MyLifecycleController myLifecycleController = Get.put(MyLifecycleController()); @override Widget build(BuildContext context) { print("build"); return Scaffold( appBar: AppBar( title: Text("GetXControllerLifecycleMethod"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ GetBuilder<MyLifecycleController>( initState: (data) => myLifecycleController.increment(), dispose: (_) => myLifecycleController.cleanTask(), builder: (controller) { return Text( "计数器值为: ${myLifecycleController.count}", style: TextStyle(color: Colors.green, fontSize: 30), ); }, ), ], ), ), ); } }
import 'package:get/get.dart'; class CountController extends GetxController { var count = 0; void increment() { count++; update(['jimi_count']); //更新特定的uniqueID } }2、实例化控制器并使用
import 'package:flutter/material.dart'; import 'package:flutter_getx_example/GetXControllerUniqueIDExample/CountConroller.dart'; import 'package:get/get.dart'; class GetXControllerUniqueIDExample extends StatelessWidget { CountController countController = Get.put(CountController()); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("GetX Obx---GetXController"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ GetBuilder<CountController>( builder: (controller) { return Text( "计数器值为: ${controller.count}", style: TextStyle(color: Colors.red, fontSize: 30), ); }, ), GetBuilder<CountController>( id: 'jimi_count', //设置特定的uniqueID builder: (controller) { return Text( "计数器值为: ${controller.count}", style: TextStyle(color: Colors.green, fontSize: 30), ); }, ), SizedBox(height: 20,), ElevatedButton( onPressed: () => countController.increment(), child: Text("增加")) ], ), ), ); } }七、GetStorage存储
GetStorage box = GetStorage(); box.write('key','value'); box.read('key');扩展知识,还有另一种三方存储shared_preferences:
final prefs = await SharedPreferences.getInstance(); /// 设置值 prefs.setInt('counter', counter); /// 获取值 final counter = prefs.getInt('counter') ?? 0;} ///移除 prefs.remove('counter');