这个很nb 可以不指定版本
在项目的yaml文件中引入:get:
//需要flutter版本1.20.0以上才支持±*/操作符
不然会报错 报错请升级你的flutter版本
示例:
只需要简单的几步就可以完成状态管理
创建一个模型类:
class Controller extends GetxController{
var count = 0.obs;
increment() => count++;
}
然后创建页面 需要使用的地方:
class Home extends StatelessWidget {
@override
Widget build(context) {
// Instantiate your class using Get.put() to make it available for all "child" routes there.
final Controller c = Get.put(Controller());
return Scaffold(
// Use Obx(()=> to update Text() whenever count is changed.
appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))),
// Replace the 8 lines Navigator.push by a simple Get.to(). You don't need context
body: Center(child: RaisedButton(
child: Text("Go to Other"), onPressed: () => Get.to(Other()))),
floatingActionButton:
FloatingActionButton(child: Icon(Icons.add), onPressed: c.increment));
}
}
class Other extends StatelessWidget {
// You can ask Get to find a Controller that is being used by another page and redirect you to it.
final Controller c = Get.find();
@override
Widget build(context){
// Access the updated count variable
return Scaffold(body: Center(child: Text("${c.count}")));
}
}
此致 get的状态管理完成了 是不是很简单,