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?
IntermediateAnswer
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.