Observer.withChild constructor

const Observer.withChild({
  1. required Widget builder(
    1. BuildContext context,
    2. Widget child
    ),
  2. required Widget child,
  3. Key? key,
  4. String? name,
})

Creates an Observer that rebuilds only the part of the subtree builder itself constructs, reusing the same, already-built child widget on every rebuild instead of reconstructing it — a static child subtree, a common technique for avoiding rebuilds of expensive widgets that don't depend on any observable. child is passed back to builder on every rebuild so it can be placed anywhere in the returned subtree (e.g. wrapped by a Row/Padding/Center that does change).

Cria um Observer que reconstrói apenas a parte da subárvore que o próprio builder constrói, reaproveitando o mesmo widget child já construído a cada rebuild em vez de reconstruí-lo — uma subárvore estática, uma técnica comum para evitar reconstruções de widgets caros que não dependem de nenhum observável. child é repassado para builder a cada rebuild, para que possa ser posicionado em qualquer lugar da subárvore retornada (ex.: envolvido por um Row/Padding/ Center que muda).

Example / Exemplo:

Observer.withChild(
  builder: (context, child) => Row(
    children: [Text('${count.value}'), child],
  ),
  child: const ExpensiveStaticWidget(),
);

Implementation

const Observer.withChild({
  required Widget Function(BuildContext context, Widget child) builder,
  required Widget child,
  super.key,
  this.name,
}) : _staticChild = child,
     builder = _unusedBuilder,
     _childBuilder = builder;