简体   繁体   English

Flutter ListView优化

[英]Flutter ListView optimization

Displaying large/infinitive list with scrolling offset in Flutter framework can be unacceptably slow (depending on the offset size). Flutter框架中显示具有滚动偏移量的大/不定式列表可能会令人无法接受地缓慢(取决于偏移量大小)。

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

Is there any way to indicate to Flutter ListView that all list items have the same size without knowing what exactly the size is? 有什么方法可以向Flutter ListView指示所有列表项的大小都相同,而又不知道大小是多少?

If not is there any way to calculate list item widget size first (and then use it to set itemExtend )? 如果没有,有什么办法可以先计算列表项小部件的大小(然后使用它来设置itemExtend )?

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. 您可以使用DefaultTextStyle.of(context)来获取封闭上下文的字体大小,并使用LayoutBuilder来获取列表的最大宽度(如果重要)。 You can then use those pieces of information to set the itemExtent . 然后,您可以使用这些信息来设置itemExtent

However, you can get exactly what you want if you use a CustomScrollView and a SliverPrototypeExtentList . 但是,如果使用CustomScrollView和SliverPrototypeExtentList ,则可以得到所需的结果 You simply provide your prototype along with a SliverChildDelegate (probably SliverChildBuilderDelegate ) and it should figure out the rest. 您只需提供您的原型以及SliverChildDelegate(可能是SliverChildBuilderDelegate ),其余的就应该弄清楚。

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. 以我的经验, 当您处于发布模式时,您可以在ListView上扔很多东西,并且仍然保持顺畅,因此您正在执行的其他操作可能会导致速度变慢。 In debug mode it's very different of course. 在调试模式下,这当然是非常不同的。

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

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