我需要在应用重启时恢复列表视图中的最后一个滚动位置。我正在使用 listview.builder和滚动控制器来保存和恢复偏移量,但 listview 不会滚动到上一个位置并从顶部开始。我什至尝试在共享首选项中保存滚动位置并在应用重启时检索但没有结果。如果有人可以帮助我并使其工作,我将不胜感激。谢谢
class DoubleHolder {
double value = 0.0;
}
class ListViewScreen3 extends StatefulWidget {
String itemHolder;
final DoubleHolder offset = new DoubleHolder();
ListViewScreen3({Key key, @required this.itemHolder}) : super(key: key);
double getOffsetMethod() {
return offset.value;
}
void setOffsetMethod(double val) {
offset.value = val;
}
ListViewScreenState3 createState() => new ListViewScreenState3();
}
@override
class ListViewScreenState3 extends State<ListViewScreen3> {
ScrollController _controller;
@override
void initState() {
super.initState();
getAllSavedData();
_controller = new ScrollController(
initialScrollOffset: widget.getOffsetMethod()
);
}
@override
Widget build(BuildContext context) {
// TODO: implement build
// Future.delayed(Duration(seconds: 2), () {load();});
final makeBody = Container(
decoration: BoxDecoration(
color: light_mode ? Color(0xFFFFFFFF) : Color(0xFF6D6D6D)),
child: returnListView());
}
Widget returnListview() {
return Column(
textDirection: TextDirection.rtl,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
new Expanded(
child: SizedBox(
height: 200.0,
child: new NotificationListener(
child: ListView.builder(
controller: _controller,
itemCount: ((surahcountlist.length)/2).toInt(),
itemBuilder: (context, index) {
return GestureDetector(
child: Card(
color: light_mode
? Colors.white
: Color(0xFF6D6D6D),
child: Column(
textDirection: TextDirection.rtl,
mainAxisAlignment:
MainAxisAlignment.start,
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: <Widget>[
showBSMLSV(index),
Wrap(
direction: Axis.horizontal,
alignment: WrapAlignment.start,
runAlignment: WrapAlignment.center,
textDirection: TextDirection.rtl,
spacing: 2.0,
// gap between adjacent chips
runSpacing: 5.0,
children: makeSurahListview(
surahcountlist[index].surah_no,
surahcountlist[index].ayah_no,
surahcountlist[index].count,
index)),
])));
}),
onNotification: (notification) {
if (notification is ScrollNotification) {
widget.setOffsetMethod(notification.metrics.pixels);
}}
)
))],
);
}