扑.在非常大的ListView上设置ListView的初始位置 [英] Flutter. Set ListView initial position on an very large ListView

查看:167
本文介绍了扑.在非常大的ListView上设置ListView的初始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个几乎无限的元素列表,但是我想将列表的初始位置设置为某个特定元素.像这样的图片:

其中索引0为初始位置,并且此列表在两个方向上的扩展可能会很长,也可能不会很长.

我可以创建如下元素:

  Widget build_tile(int i){
    return Container(child:Text(i.toString()));
  }

我的清单就是这样

ListView.builder(
                  itemBuilder: (context, i) {
                    return build_tile(i-offset);
                  },
              scrollDirection: Axis.horizontal,
            ),

图中示例的偏移量为2,但是如何设置列表的起始位置?

TLDR;如何在 1 中创建一个很长的ListView来在某些元素中开始?

谢谢!

解决方案

您可以设置 initialScrollOffset

但是在您知道目标项目/索引的偏移位置的情况下.

ScrollController _controller = ScrollController(initialScrollOffset: itemHeight * index)

ListView.builder(
  controller: _controller,
  ...
),

(请注意,此示例假定列表的窗口小部件的大小是恒定大小,但是如果列表中没有恒定的窗口小部件大小,那么您必须能够计算的最终偏移位置您的目标商品-这是另外一个问题)

或者,如果您希望它始终是最后一项/索引,那么它会容易得多:

ScrollController _controller = ScrollController(initialScrollOffset: _controller.position.maxScrollExtent)

I want to create an almost infinite list of element, but I want to set the initial position of the list to some specific element. Something like this image:

Where index 0 would be the initial position, and this list may or may not extend very long in both directions.

I can create my elements like:

  Widget build_tile(int i){
    return Container(child:Text(i.toString()));
  }

and my list would be something like this

ListView.builder(
                  itemBuilder: (context, i) {
                    return build_tile(i-offset);
                  },
              scrollDirection: Axis.horizontal,
            ),

where offset would be 2 for the example in the picture, but how can I set where the list should start?

TLDR; How to make a very long list ListView like in 1 to start in certain element?

Thanks!

解决方案

You can set one of ScrollController's property: initialScrollOffset

But on the condition that you know the offset position of the target item/index.

ScrollController _controller = ScrollController(initialScrollOffset: itemHeight * index)

ListView.builder(
  controller: _controller,
  ...
),

(note that this example assumes that the sizes of your list's widgets are of constant size, but if you don't have constant widget sizes in your list, then you must be able to calculate the final offset position of your target item - and that is a whole other issue)

Or, if you want it to always be the last item/index, then it's much easier:

ScrollController _controller = ScrollController(initialScrollOffset: _controller.position.maxScrollExtent)

这篇关于扑.在非常大的ListView上设置ListView的初始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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