0

我目前正在开发一个颤振应用程序,这个应用程序显示一个文章列表,并有一个由 a 管理的“延迟加载”列表视图构建器_scrollController和一个获取更多_getMoreArticle_scrollController.addListener.

当我转到页面底部时,新文章加载正常,但是当我向上滚动时,列表回到顶部(靠近顶部)

我不明白为什么,我尝试使用lazy_load_scrollview 1.3.0但我得到了相同的结果。

这是我使用的代码的一部分lazy_load_scrollview

    @override
  void initState() {
    super.initState();
    _getArticle();
    /*_scrollController.addListener(() {            // uncomment to get the version without lazy_load_scrollview                  
      double maxScroll = _scrollController.position.maxScrollExtent; 
      double currentScroll = _scrollController.position.pixels;
      double delta = MediaQuery.of(context).size.height * 0.25;
      print('maxScroll = ' + maxScroll.toString() + ' currentScroll = ' + currentScroll.toString());
      print(docs.length);
      if(/*maxScroll - currentScroll < delta*/ currentScroll == maxScroll){
        _getMoreArticle();
      }
    });*/
  }

 @override
      Widget build(BuildContext context) {
        final user = Provider.of<AppUser?>(context);
         if (user == null) {
          return const Connection();
        }
        var section = widget.section;
        AppFilters filters = widget.filters;
        return((loadingArticles == true) ? 
          ListView.separated(
            itemBuilder: (context, index) => Shimmer(),
            separatorBuilder: (context, index) => const SizedBox(height: 10),
            itemCount: 5
          )
          :Container(
            child:(docs.length == 0)  ? EmptyCard() :
            LazyLoadScrollView(                 // remove to get the version without LazyLoadScrollView
              onEndOfPage: () => _getMoreArticle(),
              child: ListView.builder(
              scrollDirection: Axis.vertical,
              //controller: _scrollController,  // uncomment to get the version without LazyLoadScrollView
              shrinkWrap: true,
              itemCount: docs.length + 1,
              itemBuilder: (BuildContext context, int index) {
                if (index == docs.length) {
                  return(Shimmer());
                }
                return (MainCard(docs: docs, index: index, userId: user.uid)
              );
            })  
          )
        ));
      }
    }

这是用于获取文章的 2 个函数:

  _getMoreArticle() async {
    if (_moreArticleAvailable == false || _gettingMoreArticle == true) {
      print('more article not called' + _moreArticleAvailable.toString() +  ' ' + _gettingMoreArticle.toString());
      return;
    }
    print('more article called');
    _gettingMoreArticle = true;
    var resp = await DatabaseService(widget.userId).getArticleBySection(widget.section, widget.filters, docs[offset]);
    print("getted " + resp.length.toString());
    if (resp.length < 5) {
      _moreArticleAvailable = false;
    }
    docs.addAll(resp);
    if (mounted) {
      setState(() {
        offset = docs.length - 1;
      });
    }
    _gettingMoreArticle = false;
  }

  _getArticle() async { 
    if (mounted) {
      setState(() {
        loadingArticles = true;
      });
    }
    docs = await DatabaseService(widget.userId).getArticleBySection(widget.section, widget.filters, null);
    if(mounted) {
      setState(() {
        offset = docs.length - 1;
        loadingArticles = false;
      });
    }}

我还通过打印以下数据注意到_scrollController.addListener

  1. 该列表有适量的文章
  2. 该列表double maxScroll = _scrollController.position.maxScrollExtent;正在下降,然后备份查看日志: 日志

但我只是向上移动了 2 个像素。

如果有人可以向我解释或告诉我们为什么我可以尝试解决这个问题,但我有 0 条线索!

谢谢 :)

4

0 回答 0