Back to results for “Animations”
Flutter › Custom Widgets & Reusable Components

How does properly using the child parameter, rather than a builder function, on a custom reusable widget let Flutter optimize rebuilds by reusing an already built widget subtree?

Intermediate

Answer

When a custom widget accepts a child parameter representing an already fully constructed widget, rather than a builder function that gets called anew on every single rebuild, Flutter can recognize that the exact same already built child widget instance is simply being passed through unchanged, allowing it to skip rebuilding that specific child subtree entirely even if the surrounding parent widget rebuilds frequently, which is exactly the same underlying optimization technique used internally by many of Flutter's own official built in widgets, such as AnimatedBuilder.
class FadeWrapper extends StatelessWidget {
  final Widget child;
  const FadeWrapper({required this.child});

  @override
  Widget build(BuildContext context) => Opacity(opacity: 0.8, child: child);
}
Real-world example A custom animated wrapper widget accepts a child parameter representing the expensive content it wraps, allowing that expensive child content to be constructed just once and reused efficiently across many rebuilds of the surrounding animation logic.

Common follow-ups: What is the difference between passing a child versus passing a builder callback to a custom widget?;How does this same pattern relate to how AnimatedBuilder itself is actually implemented?