繁体   English   中英

如何在颤振中优化变量长度小部件的X文本显示

[英]How can I optimize the display of X Texts of variable lenght widgets in flutter

我有这个包含Container的小部件列表,其中包含Title和A Body文本,其长度是可变的。 例如,我可以有两个单词的文本和30个中的一个,所以行数可以变化。 我尝试用ListView.builder显示它们但是当我显示它们时,性能下降到20帧/秒,我在现实世界中测试它并且性能保持不变。 我尝试在线查找并找到了一个答案( Flutter ListView优化 ),建议使用CustomScrollView和SliverPrototypeExtentList,所以我查看了文档,发现这个小部件构建了它的所有子节点,就像原型小部件的大小一样,所以我不知道我认为这就是我期待使用的东西。 无论如何我尝试了它,但我无法显示任何内容,并且没有找到关于如何在线使用它的教程,这是我尝试过的代码。 (应用程序只是冻结而没有发生任何事情):

CustomScrollView(
    controller: _scrollController,
    shrinkWrap: true,
    semanticChildCount: _actions.length,
    slivers: <Widget>[
      SliverPrototypeExtentList(
          delegate: SliverChildBuilderDelegate((context, index) {
            return Container(
              color: Colors.yellow,
              width: 200.0,
              height: 20 + 100 * Random().nextDouble(),
            );
          }),
          prototypeItem: Container(
            color: Colors.red,
            width: 200.0,
            height: 100.0,
          )),
    ],
  )

其中_actions是近100个元素的列表。

有没有比这更好的选择,还是我只是使用小部件错了?

我可能错过了一些东西,但经过多一点研究后我发现如何使用我链接的答案中陈述的SliverChildBuilderDelegate:

CustomScrollView(
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      slivers: [
        SliverList(
          delegate: SliverChildBuilderDelegate((context, index) {
            GameAction action = _actions[index];
            return LeikunnActionCard(action: action);
          }, childCount: _actions.length),
        ),
      ]),

基本上,SliverList在显示时会懒洋洋地构建项目。 所以没有性能问题。

暂无
暂无

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

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