Why does Flutter favor composition over inheritance for building widgets?
// short answer
Flutter widgets are meant to be combined (composed) into new widgets rather than extended via subclassing, because composition keeps widgets small, reusable, and independently testable — Flutter's own framework widgets are built this way.
You could subclass ElevatedButton and override its build method, but Flutter's own widgets are deliberately shallow and specific — a Container, for example, is itself just composed internally of Padding, DecoratedBox, and other primitives. The idiomatic pattern for a 'custom button with extra behavior' is to wrap or combine existing widgets into a new one, not extend a concrete widget class.
This matters practically: composed widgets can mix and match capabilities freely, while inheritance would force a rigid single-parent hierarchy. It also plays well with Flutter's rebuild model — small, focused widgets rebuild cheaply and independently.
// common mistakes
- ×Subclassing StatelessWidget/StatefulWidget solely to extend framework widget behavior, when composing a new widget from existing ones would be idiomatic.
- ×Building a single giant widget class instead of splitting it into smaller composed pieces.
// interview tips
- →Reference that Container itself is a composed widget internally — a concrete example that shows real understanding, not just theory.