@tank/flutter-widget-architecture
Flutter Widget Architecture & Composition Patterns
Core Philosophy
-
Composition Over Inheritance — Build complex UIs by combining simple, focused widgets rather than creating deep inheritance hierarchies. Each widget should have a single responsibility.
-
Immutability as Default — Widgets are immutable configuration objects. State lives in State classes, not widgets. This enables efficient rebuilds and predictable behavior.
-
BuildContext is Scope — BuildContext represents a location in the widget tree. Use it to access ancestors (Theme, MediaQuery, InheritedWidgets) and manage lifecycle. Never store BuildContext across frames.
-
Keys Solve Identity — Keys explicitly tell Flutter which widget corresponds to which Element when the tree changes. Use them strategically for lists, reordering, and state preservation.
-
Constraints Flow Down, Sizes Flow Up — The layout system is unidirectional. Parent constraints determine child possibilities; children report their sizes. Understand this to debug layout issues.