Reputation: 8636
Assume we have a complex Model with ChangeNotifer
, so how should I automatically update the UI when model changes?
Here is what I came up with, but looks weird, is it right, is there a better way of doing this?
class Playground extends HookWidget {
const Playground({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
print("built");
final dataState = useState(DataClass(0));
final data = useListenable(dataState.value);
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: ListView(
children: [
ListTile(
title: Text("data: ${data.cnt}"),
onTap: () {
data.cnt++;
},
),
],
),
);
}
}
class DataClass with ChangeNotifier {
int _cnt;
int get cnt => _cnt;
set cnt(int val) {
_cnt = val;
notifyListeners();
}
DataClass(int cnt) : _cnt = cnt;
}
ps: something like useStateListenable
or useListenableProvider
Upvotes: 1
Views: 1645
Reputation: 8636
you can combine useState
and useListenable
to something like useListenableState
T useListenableState<T extends ChangeNotifier>(T data) {
final state = useState<T>(data);
return useListenable(state.value);
}
class Playground extends HookWidget {
const Playground({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
print("built");
final data = useListenableState(DataClass(0));
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: ListView(
children: [
ListTile(
title: Text("data: ${data.cnt}"),
onTap: () {
data.cnt++;
},
),
],
),
);
}
}
class DataClass with ChangeNotifier {
int _cnt;
int get cnt => _cnt;
set cnt(int val) {
_cnt = val;
notifyListeners();
}
DataClass(int cnt) : _cnt = cnt;
}
Upvotes: 1