【问题标题】:Create builder with pre-built subtree in Flutter在 Flutter 中使用预先构建的子树创建构建器
【发布时间】:2020-12-12 17:45:48
【问题描述】:

在 Flutter doc https://api.flutter.dev/flutter/widgets/ValueListenableBuilder-class.html 中,在性能优化下它指出:

If your builder function contains a subtree that does not depend on the value of the ValueListenable, it's more efficient to build that subtree once instead of rebuilding it on every animation tick.

If you pass the pre-built subtree as the child parameter, the ValueListenableBuilder will pass it back to your builder function so that you can incorporate it into your build.

Using this pre-built child is entirely optional, but can improve performance significantly in some cases and is therefore a good practice.

Flutter Widgets Catalog 中是否有一个更“通用”的构建器小部件可以接受预构建的子树(类似于提到的ValueListenableBuilder)?如果没有,这样的东西如何工作,以便我可以创建自己的?

看了源码没看懂

【问题讨论】:

  • AnimatedBuilder 更通用,因为它需要更通用的Listenable 输入(而不是ValueListenable

标签: flutter


【解决方案1】:

我最终创建了自己的。

class SubtreeBuilder extends StatelessWidget {
  const SubtreeBuilder({
    Key key,
    @required this.builder,
    this.child,
  }) : super(key: key);

  final Widget Function(BuildContext context, Widget child) builder;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return builder(context, child);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    相关资源
    最近更新 更多