I am learning the Flutter state management with mobx, and I want to change the context of the dynamic listtile without rebuilding the whole page, I tried observable list from Mobx store but seems not to work here is my samples.
This sample I want to click the item the name change to clicked without rebuilding the screen, when I click nothing happens but when I go back to another page and come back then I found out that the name of the animal changed.
This is my stateless widget
Class AnimalPageList extends StatelessWidget{
@override
Widget build(BuildContext context){
var animalStore = context.watch<AnimalStore>();
var animals = animalStore.animalsList;
return Scaffold(
appBar: AppBar(
title: Text("Animals Page"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Observer(builder: (_) =>
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: animals.length,
itemBuilder: (BuildContext context, int index) {
var pet = animalStore.getAnimal(index);
//final petName = context.select((ObservableList<Animal> animals)=>animals[index].name);
return ListTile(
key: ValueKey(pet.id),
enabled: true,
onTap: () async {
var rename = animals[index].name +" clicked";
animals[index].name = rename;
},
title: Observer(builder: (_) => Text(pet.name)),
);
},
),
),
],
),
),
);
And here is my model with the store
class Animal {
final int id;
@observable
String name;
Animal({this.id, this.name});
factory Animal.fromJson(Map<dynamic, dynamic> json) {
return new Animal(
id: json['id'],
name: json['name'],
);
}
Map<dynamic, dynamic> toJson() {
final Map<dynamic, dynamic> data = new Map<dynamic, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
return data;
}
}
class AnimalStore = _AnimalStore with _$AnimalStore;
abstract class _AnimalStore with Store{
@observable
ObservableList animalsList = ObservableList<Animal>.of(
[
Animal(id: 1, name: 'cat'),
Animal(id: 2, name: 'dog'),
Animal(id: 3, name: 'mouse'),
Animal(id: 4, name: 'horse'),
Animal(id: 5, name: 'frog'),
]
);
@action
getAnimal(int i){
return animalsList[i];
}
}
Which way I can arrange, the model, the store and the sreenwidget so that if I click on a button it changes the name as desired whithout requiring to rebuild the whole screen.