简体   繁体   English

Flutter - ListView.builder 不可滚动

[英]Flutter - ListView.builder not scrollable

I have my ListView.builder inside Expanded widget which render widgets correctly on the screen but I cannot scroll the widgets rendered by it.我的ListView.builder位于Expanded小部件内,它可以在屏幕上正确呈现小部件,但我无法滚动由它呈现的小部件。

 Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder(
        future: getPostsForUid(),
        builder: (_, snap) {
          return Expanded(
            child: ListView.builder(
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: snap.data.length,
              itemBuilder: (_, index) {
                if (snap.data[index]['post_type'] == 'real_estate') {
                  return realEstate(snap, index);
                }
                else if (snap.data[index]['post_type'] == 'video_sharing') {
                  return videoSharing(snap, index);
                }
                else {
                  return Text('');
                }
              },
            ),
          );
        },
      ),
    );
  }

Try using ScrollPhysics() class, physics: ScrollPhysics() , here is the link for reference for the same.尝试使用 ScrollPhysics() class, physics: ScrollPhysics() ,这里是相同的参考链接

You should set your physics to AlwaysScrollableScrollPhysics() .您应该将物理设置为AlwaysScrollableScrollPhysics() The docs state the following:文档 state 以下内容:

Scroll physics that always lets the user scroll.始终让用户滚动的滚动物理。 This overrides the default behavior which is to disable scrolling when there is no content to scroll.这会覆盖默认行为,即在没有内容可滚动时禁用滚动。 It does not override the handling of overscrolling.它不会覆盖过度滚动的处理。 On Android, overscrolls will be clamped by default and result in an overscroll glow.在 Android 上,默认情况下会限制过度滚动并导致过度滚动发光。 On iOS, overscrolls will load a spring that will return the scroll view to its normal range when released.在 iOS 上,overscrolls 将加载一个 spring,它会在释放时将滚动视图返回到其正常范围。

Here is an image of "overscroll glow" for you to understand what that means.这是一张“过度滚动发光”的图像,供您理解这意味着什么。 https://i.stack.imgur.com/6mMn4.png

Consider using shrinkWrap: false to expand your contents in case they are bounded.考虑使用shrinkWrap: false来扩展你的内容,以防它们被限制。

Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder(
        future: getPostsForUid(),
        builder: (_, snap) {
          return Expanded(
            child: ListView.builder(
              physics: AlwaysScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: snap.data.length,
              itemBuilder: (_, index) {
                if (snap.data[index]['post_type'] == 'real_estate') {
                  return realEstate(snap, index);
                }
                else if (snap.data[index]['post_type'] == 'video_sharing') {
                  return videoSharing(snap, index);
                }
                else {
                  return Text('No data available.');
                }
              },
            ),
          );
        },
      ),
    );
  }

See the docs:请参阅文档:

Add Physics:AlwaysScrollable() in your Listview .在您的Listview中添加Physics:AlwaysScrollable()

I have my ListView.builder inside Expanded widget which render widgets correctly on the screen but I cannot scroll the widgets rendered by it.我的ListView.builderExpanded小部件内,它可以在屏幕上正确呈现小部件,但我无法滚动它呈现的小部件。

 Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder(
        future: getPostsForUid(),
        builder: (_, snap) {
          return Expanded(
            child: ListView.builder(
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: snap.data.length,
              itemBuilder: (_, index) {
                if (snap.data[index]['post_type'] == 'real_estate') {
                  return realEstate(snap, index);
                }
                else if (snap.data[index]['post_type'] == 'video_sharing') {
                  return videoSharing(snap, index);
                }
                else {
                  return Text('');
                }
              },
            ),
          );
        },
      ),
    );
  }

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

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