用户滚动到列表FLUTTER的末尾后,创建一个新元素 [英] Create a new element once a user scrolled to the end of the list FLUTTER

查看:38
本文介绍了用户滚动到列表FLUTTER的末尾后,创建一个新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 listview ,其中包含一些TextField,我认为一旦用户向下滚动,添加文本字段会很酷,例如:

I have a listview with some TextFields inside, I think that it will be cool to add the textfield once a user scrolls down, For example:

在用户到达第五个元素并继续向下滚动创建一个新元素之后,ListView中有5个元素.我找不到此任务的解决方案,我尝试检查元素的滚动位置,但不幸的是,我遇到了错误

We have 5 Elements in ListView after a user reaches the fifth element and continued scrolling down a new element is created. I couldn't find a solution for this task, I tried to check scrollposition for elements but unfortunately, I got an error

flutter:引发了另一个异常:NoSuchMethodError:吸气剂在空值上调用了"position".

flutter: Another exception was thrown: NoSuchMethodError: The getter 'position' was called on null.

这里是代码.

ScrollController _controller;

  @override
  void initState() {
    super.initState();
    _controller = ScrollController();
  }

FlatButton(
                    onPressed: () {
                      print(_controller.position.minScrollExtent);
                    },
                    child: Text('ds'),
                  ),

ListView
 - controller: _controller

推荐答案

如果要在列表视图的末尾添加新元素,这是一种解决方案

Here is the one solution if you want to add the new element on the end of the list view

ScrollController _controller;

ListView.builder(
              controller: _controller,
              itemCount: _items.length,
              itemBuilder: (context, index) {
                return ListTile(title: Text("Index : $index"));
              },
            )


@override
  void initState() {
    _controller = ScrollController();
    _controller.addListener(_scrollListener);
    super.initState();
  }


_scrollListener() {
    if (_controller.offset >= _controller.position.maxScrollExtent &&
        !_controller.position.outOfRange) {
      setState(() {
        message = "reach the bottom";
      });
    }
    if (_controller.offset <= _controller.position.minScrollExtent &&
        !_controller.position.outOfRange) {
      setState(() {
        message = "reach the top";
      });
    }
  }

您需要在 maxScrollExtent 中的

将该元素添加到列表视图中.希望对您有帮助.

in the maxScrollExtent you need to add the element into the listview. I hope it will helps you.

有关更多信息,请访问这里

For more information visit here

这篇关于用户滚动到列表FLUTTER的末尾后,创建一个新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆