Skip to content

Lesson 4 of 9 · 23m

Widget Composition and Layout Without a Widget Junk Drawer

Compose small, honest widgets, build a design system at the right altitude, and diagnose layout overflow by understanding constraints.

Most Flutter UI code does not fail because the developer chose the wrong widget. It fails because composition boundaries are dishonest: a screen knows twenty layout details, every component accepts a grab bag of callbacks, and nobody knows which widget owns spacing. Senior Flutter work looks almost boring: small widgets, narrow parameters, consistent layout contracts.

Compose around responsibility

A good component answers one question. OrderCard presents an order. OrderList owns ordering and empty state. OrdersScreen wires routes and data. It should not also format currency, choose snackbars, and inspect network exceptions:

class OrderCard extends StatelessWidget {
  const OrderCard({required this.order, this.onOpen, super.key});

  final Order order;
  final VoidCallback? onOpen;

  @override
  Widget build(BuildContext context) {
    return Card(
      clipBehavior: Clip.antiAlias,
      child: ListTile(
        title: Text(order.number),
        subtitle: Text(order.statusLabel),
        trailing: Text(order.totalLabel),
        onTap: onOpen,
      ),
    );
  }
}

Notice what is absent: repository access, global navigation assumptions, and a dozen booleans. If a component needs twelve flags, it is usually several components wearing a trench coat.

Theme first, but do not theme everything through strings

Flutter's ThemeData is a strong baseline: color scheme, text theme, input decoration, component defaults. Extend it with typed theme extensions when the design system has tokens the framework does not know:

class AppSpacing extends ThemeExtension<AppSpacing> {
  const AppSpacing({required this.gutter, required this.section});

  final double gutter;
  final double section;

  @override
  AppSpacing copyWith({double? gutter, double? section}) => AppSpacing(
        gutter: gutter ?? this.gutter,
        section: section ?? this.section,
      );

  @override
  AppSpacing lerp(AppSpacing? other, double t) => this;
}

The goal is not abstraction tourism. The goal is that a designer changing section spacing does not create 400 diffs across feature screens.

Overflow is a constraint conversation

The yellow-and-black overflow stripe is not a bug in Row; it is a report that the children could not fit the constraints they were given. Debug by asking:

What constraint did the parent send?
Which child is flexible?
Which child is choosing intrinsic width or height?
Can text wrap, shrink, or truncate?
Should the row become a wrap or scroll container?

Expanded divides remaining space inside a flex. Flexible allows a child to be smaller than its ideal size. LayoutBuilder reveals actual constraints but is easy to abuse. Reach for it when behavior truly depends on available size, not as a substitute for understanding the parent.

Accessibility belongs in composition

A beautiful control that screen readers announce as "button" is unfinished. Use semantic labels, sufficient touch targets, focus order, and contrast. Interactive custom widgets need Semantics, keyboard support on desktop/web, and visible focus. These are composition requirements, not polish tasks assigned after release.

Field notes

  • SafeArea is contextual; wrapping every screen blindly can create awkward desktop and web spacing.
  • Text scaling will break layouts that assume fixed-height rows. Test at 1.3x and larger.
  • Intrinsic measurement widgets can be expensive in long lists; measure before scattering them everywhere.
  • Prefer const widgets where configuration is truly constant, especially in list item composition.
  • If a reusable component starts importing feature-specific models, the boundary has already failed.

Was this lesson helpful?

Sign in to rate