flatten method Null safety

Iterable<WidgetPlaceholder> flatten(
  1. BuildMetadata meta,
  2. BuildTree tree
)

Flattens a BuildTree into widgets.

Implementation

Iterable<WidgetPlaceholder> flatten(BuildMetadata meta, BuildTree tree) {
  final widgets = <WidgetPlaceholder>[];
  final instance = Flattener(this);
  _flatteners.add(instance);

  for (final flattened in instance.flatten(tree)) {
    if (flattened.widget != null) {
      widgets.add(WidgetPlaceholder.lazy(flattened.widget!));
      continue;
    }

    if (flattened.widgetBuilder != null) {
      widgets.add(
        WidgetPlaceholder<BuildTree>(tree)
            .wrapWith((context, _) => flattened.widgetBuilder!(context)),
      );
      continue;
    }

    if (flattened.spanBuilder == null) {
      continue;
    }
    widgets.add(
      WidgetPlaceholder<BuildTree>(tree).wrapWith((context, _) {
        final tsh = tree.tsb.build(context);
        final span = flattened.spanBuilder!(context, tsh.whitespace);
        if (span == null) {
          return widget0;
        }

        final textAlign = tsh.textAlign ?? TextAlign.start;

        if (span is WidgetSpan && textAlign == TextAlign.start) {
          return span.child;
        }

        return buildText(meta, tsh, span);
      }),
    );
  }

  return widgets;
}