RxInt _counter = 0.obs
RxString _userName = RxString("zhangsan")
RxList list = RxList(["zhangsan","lisi"])
Person person = Person()
var animal = Animal("xiaomao",2).obs
class Person {
RxString name = "".obs
RxInt age = 0.obs
}
class Animal {
String name
int age
Animal(this.name,this.age)
}
@override
void initState() {
super.initState();
person.name.value = "关羽";
person.age.value = 30;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("响应式状态管理")),
body: Column(
children: [
Obx(() => Text(person.name.value)),
Obx(() => Text(animal.value.name)),
Container(
margin: EdgeInsets.all(30),
child: Obx(() => Text('${_counter.value}')),
),
Obx(() => Text(_userName.value)),
Expanded(
child: Obx(() => ListView(
children: list.map((element) {
return ListTile(title: Text(element),);
}).toList(),
)),
),
],
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
_counter++;
_userName.value = "lisi";
list.add("wangwu");
person.name.value = "项羽";
animal.value.name = "xiaogou1";
animal.value = animal.value;
},
),
);
}
}