首页 > 解决方案 > 如何在 Flutter 中的列表中添加溢出?

问题描述

我想在列表的末尾和顶部添加一个过度滚动,只有当有人过度滚动时。

所以......我有一些想法,我尝试从使用 GestureDetector 开始,如果列表大于屏幕,它似乎不起作用,然后我添加了一个 ListView Scroller 控制器,但它只有在 List比屏幕大。

我继续尝试通过通知添加它,但它似乎太慢了。

有任何想法吗?

class _PullableListState extends State<PullableList> {
  double overScrollOnEnd = 0;

  @override
  Widget build(BuildContext context) {
    return NotificationListener<ScrollNotification>(
      onNotification: (notification) {
        // print(notification.metrics.pixels);
        if (notification is OverscrollNotification) {
          setState(() {
            overScrollOnEnd += notification.dragDetails.delta.dy;
          });
          print(overScrollOnEnd);
        }
        if (notification is ScrollUpdateNotification) {
          setState(() {
            overScrollOnEnd = 0;
          });
        }
        return true;
      },
      child: Container(
        margin: EdgeInsets.only(top: max(overScrollOnEnd, 0)),
        child: ListView.builder(
          itemBuilder: widget.itemBuilder,
          itemCount: widget.itemCount,
        ),
      ),
    );
  }
}

标签: flutter

解决方案


您希望AlwaysScrollableScrollPhysicsBouncingScrollPhysics结合使用。

class _PullableListState extends State<PullableList> {
  double overScrollOnEnd = 0;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: max(overScrollOnEnd, 0)),
      child: ListView.builder(
        physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
        itemBuilder: widget.itemBuilder,
        itemCount: widget.itemCount,
      ),
    );
  }
}

推荐阅读