Flutter ListView优化 [英] Flutter ListView optimization

查看:427
本文介绍了Flutter ListView优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Flutter 框架中显示具有滚动偏移量的大型/不定式列表可能会令人不可接受地缓慢(取决于偏移量大小).

Displaying large/infinitive list with scrolling offset in Flutter framework can be unacceptably slow (depending on the offset size).

这可以通过在所有项目都具有相同大小的情况下指定 itemExtend 来解决.

This can be solved by specifying itemExtend when all items have the same size.

但是,即使所有列表项的大小相同,通常也不可能预先知道 itemExtend ,因为高度可能取决于字体大小,而字体大小取决于用户电话的可访问性设置.

But it is not usually possible to know itemExtend in advance even when all list items have the same size because the height might depend on font size which depends on user phone accessibility settings.

有什么方法可以指示 Flutter ListView 物品的尺寸相同,却不知道尺寸到底是多少?

Is there any way to indicate to Flutter ListView that all list items have the same size without knowing what exactly the size is?

如果没有任何方法,首先要计算列表项小部件的大小(然后使用它来设置 itemExtend )?

If not is there any way to calculate list item widget size first (and then use it to set itemExtend)?

推荐答案

我不相信有一种方法可以首先自动计算窗口小部件的大小,但是您可以根据需要进行任意手动计算. /s>您可以使用 DefaultTextStyle.of(context) 来获取字体大小封闭上下文,并使用 LayoutBuilder 来获取最大宽度如果重要的话,清单.然后,您可以使用这些信息来设置itemExtent.

I don't believe there is a way to automatically calculate the widget size first, however you can do manual calculations as much as you want. You can use DefaultTextStyle.of(context) to get the font size of the enclosing context and use a LayoutBuilder to get the maximum width of the list if that matters. You can then use those pieces of information to set the itemExtent.

但是,如果您使用CustomScrollView和 SliverPrototypeExtentList .您只需提供原型以及SliverChildDelegate(可能是 SliverChildBuilderDelegate ),然后它应该找出其余部分.

However, you can get exactly what you want if you use a CustomScrollView and a SliverPrototypeExtentList. You simply provide your prototype along with a SliverChildDelegate (probably SliverChildBuilderDelegate) and it should figure out the rest.

最后...我有点好奇您是否要共享代码的一部分,以实际实例化列表(以及其中显示的内容).根据我的经验,当您处于发布模式时,您可以在ListView上扔很多东西,并且仍然保持顺畅,因此您正在做的其他事情可能会导致速度变慢.在调试模式下,这当然是非常不同的.

And finally... I'm a little bit curious whether you'd share the part of your code where you actually instantiate the list (and what's shown within it). In my experience, when you're in release mode you can throw an awful lot of stuff at a ListView and still have it be smooth, so it's possible there's something else you're doing that's causing the slowdown. In debug mode it's very different of course.

这篇关于Flutter ListView优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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