简体   繁体   English

Listview 强制滚动到 flutter 的顶部

[英]Listview force scrolling to the top in flutter

Listview Code列表视图代码

    return FutureBuilder<bool>(
      future: getData(),
      builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
        if (!snapshot.hasData) {
          return SizedBox();
        } else {
          return Container(
            margin: PHONE ? EdgeInsets.only(top:AppBar().preferredSize.height + MediaQuery.of(context).padding.top + 90, bottom: 90)
            : EdgeInsets.only(top:AppBar().preferredSize.height + MediaQuery.of(context).padding.top + 140, bottom: 100),
            child: ListView.builder(
              physics: BouncingScrollPhysics(),
              itemCount: data.length,
              addAutomaticKeepAlives: true,
              scrollDirection: Axis.vertical,
              itemBuilder: (BuildContext context, int index) {
                widget.animationController.forward();
                return AnimatedBuilder(
                  animation: widget.animationController,
                  builder: (BuildContext context, Widget child) {
                    return ProgramsTitleView(
                      animation: Tween<double>(begin: 0.0, end: 1.0).animate(
                        CurvedAnimation(
                          parent: widget.animationController,
                          curve: Interval((1 / data.length) * index, 1.0,
                              curve: Curves.fastOutSlowIn),
                        ),
                      ),
                      animationController: widget.animationController,
                      titleText: data[index]['Heading'],
                      type: widget.screen,
                    );
                  },
                );
              },
            ),
          );
        }
      },
    );

I have a listview which whose data is populated from http request.我有一个列表视图,其数据是从 http 请求中填充的。 The weird thing about this is that it loads the data properly and scrolls down properly but if i scroll up a little bit, it force scrolls to the top no matter how far down i have scrolled before.奇怪的是,它可以正确加载数据并正确向下滚动,但是如果我向上滚动一点,无论我之前向下滚动多远,它都会强制滚动到顶部。 I looked everywhere but didn't find anybody with the same issue.我到处找,但没有找到有同样问题的人。 Am i missing somethings which is causing this error?我是否遗漏了导致此错误的某些内容?

In this case, I think it's better to use StreamBuilder instead of FutureBuilder since it only rebuilds the screen when there's change in data.在这种情况下,我认为最好使用StreamBuilder而不是FutureBuilder ,因为它只在数据发生变化时重建屏幕。 So in your getData() function, you can add the data to Stream instead.因此,在您的getData() function 中,您可以将数据添加到Stream中。

final controller = StreamController();

getData() async {
  final data = await _someCallToApi();
  controller.add(data);
}

Then listen to it in the UI:然后在 UI 中收听:

 return StreamBuilder(
      stream: controller.stream,
      builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
        if (!snapshot.hasData) {
          return SizedBox();
 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM