0

我需要在应用重启时恢复列表视图中的最后一个滚动位置。我正在使用 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);
                          }}
                )

      ))],
         );
    } 
4

1 回答 1

1

我有两种猜测,只需检查一下,看看其中哪一种有效。

首先,我认为您需要给列表视图一些时间来构建然后跳转到您想要的位置。初始化滚动控制器后,基本上只需在 initState 方法中添加 Future.delayed

Future.delayed(Duration(seconds: 
1),(){controller.jumpto();});

其次,我认为是因为您使用的是 listview.builder。这个小部件在运行时构建它的孩子,所以如果你保持第 10 个孩子的位置,它将无法到达那里,因为它还没有构建它。我认为您需要使用 listview 并为其子级使用 List.generate()

于 2021-09-06T21:57:12.177 回答