1 基本上所有的滑动组件都有这个restorationId属性,设置这个属性可以让页面不是用户主动关闭的情况下,再次打开能保持滑动view的滑动偏移。
2 如何通过 restoration 机制存储其他数据 2.1 State混入RestorationMixin 2.2 重写 get restorationId,返回唯一id 2.3 定义需要保存的数据,比如RestorableInt等官方提供的一些列数据。 2.4 重写 restoreState,注册定义好的数据。 2.5 在Widget中直接使用 _counter.value 2.6 自定义类型,实现RestorableProperty
class CD extends State<StatefulWidget> with RestorationMixin {
@override
Widget build(BuildContext context) {
return Text("${_counter.value}");
}
final RestorableInt _counter = RestorableInt(0);
@override
String? get restorationId => "restorationId";
@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
registerForRestoration(_counter, "count");
}
}