buildContent<T extends ContentItem> method

  1. @override
Widget buildContent<T extends ContentItem>(
  1. BuildContext context,
  2. T content, {
  3. LayoutConfiguration<T>? layout,
})
override

Builds a Widget for the given content. You can pass in an optional layout to override the default layout

Implementation

@override
Widget buildContent<T extends ContentItem>(
  BuildContext context,
  T content, {
  LayoutConfiguration<T>? layout,
}) {
  final builder = _extensionBuilder!.contentBuilder(content.schemaType);

  assert(builder != null,
      'Failed to retrieve builder for schemaType: ${content.schemaType}. Is the ContentBuilder registered for this schemaType?');

  Widget? contentWidget;
  try {
    contentWidget =
        layout?.build(context, content) ?? builder?.build(context, content);
  } catch (e) {
    final possibleLayouts = [
      layout?.schemaType,
      content.layout?.schemaType,
      builder?.defaultLayout.schemaType
    ].nonNulls;
    return vyuh.widgetBuilder.errorView(context,
        error: e,
        title: 'Failed to build layout',
        subtitle:
            'Possible Layouts: "${possibleLayouts.join(', ')}" for Content: "${content.schemaType}"');
  }

  if (contentWidget != null) {
    final modifiers = content.getModifiers();

    if (modifiers != null && modifiers.isNotEmpty) {
      try {
        return modifiers.fold<Widget>(
          contentWidget,
          (child, modifier) => modifier.build(context, child, content),
        );
      } catch (e) {
        return vyuh.widgetBuilder.errorView(context,
            error: e,
            title: 'Failed to apply modifiers',
            subtitle:
                'Modifier Chain: "${modifiers.map((m) => m.schemaType).join(' -> ')}" for Content: "${content.schemaType}"');
      }
    }

    return contentWidget;
  }

  return const SizedBox.shrink();
}