visitElementAfterWithContext method

  1. @override
Widget? visitElementAfterWithContext(
  1. BuildContext context,
  2. Element element,
  3. TextStyle? preferredStyle,
  4. TextStyle? parentStyle,
)

Called when an Element has been reached, after its children have been visited.

If MarkdownWidget.styleSheet has a style with this tag, it will be passed as preferredStyle.

If parent element has TextStyle set, it will be passed as parentStyle.

If a widget build isn't needed, return null.

Implementation

@override
Widget? visitElementAfterWithContext(BuildContext context, md.Element element,
    TextStyle? preferredStyle, TextStyle? parentStyle) {
  // --- Debugging Prints ---
  // Print details about every element being visited by this builder
  print('WidgetElementBuilder: Visiting element with tag: "${element.tag}"');
  print('  Element attributes: ${element.attributes}');
  print('  Element children count: ${element.children?.length ?? 0}');
  // --- End Debugging Prints ---

  // Check if the element is our custom widget block.
  if (element.tag == 'widget') {
  // if (element.tag == 'blockquote') {
    // --- Debugging Prints ---
    print('WidgetElementBuilder: Identified element as a widgetBlock.');
    // --- End Debugging Prints ---

    try {
      // Retrieve the label and JSON data stored in the element's attributes.
      final label = element.attributes['label'];
      final jsonData = element.attributes['data'];

      // --- Debugging Print ---
      print(
          'WidgetElementBuilder: Attempting to parse JSON data: "$jsonData"');
      // --- End Debugging Print ---
      // Attempt to parse the JSON string.
      final params = jsonDecode(jsonData!) as Map<String, dynamic>;

      // --- Debugging Print ---
      print(
          'WidgetElementBuilder: JSON parsed successfully. Looking up builder for label: "$label"');
      // --- End Debugging Print ---

      // Access the EmbeddedWidgetRegistry via Provider using the BuildContext.
      // listen: false because the builder itself doesn't need to rebuild when the registry changes.
      final registry =
          Provider.of<EmbeddedWidgetRegistry>(context, listen: false);

      // Look up the widget builder function based on the label using the registry.
      final builder = registry.getBuilder(label!);

      if (builder != null) {
        // --- Debugging Print ---
        print(
            'WidgetElementBuilder: Found builder for label "$label". Calling builder function.');
        // --- End Debugging Print ---
        // If a builder is found, call it to create the widget,
        // passing the BuildContext, parsed parameters, and the command callback.
        return builder(context, params, sendCommandCallback);
      } else {
        // If no builder is registered for the label, show an error message.
        // --- Debugging Print ---
        print('WidgetElementBuilder: No builder found for label "$label".');
        // --- End Debugging Prints ---
        return Text(
          'Error: Unknown widget type "$label"',
          style: TextStyle(color: Colors.red),
        );
      }
    } catch (e) {
      // If JSON parsing fails or any other error occurs during building,
      // show an error message.
      // --- Debugging Print ---
      print('WidgetElementBuilder: Error during widget building: $e');
      // --- End Debugging Prints ---
      return Text(
        'Error rendering widget: ${e.toString()}',
        style: TextStyle(color: Colors.red),
      );
    }
  }

  // For any other element tags, return null to let flutter_markdown handle them
  // with its default builders or other registered custom builders.
  // --- Debugging Print ---
  // print('WidgetElementBuilder: Not a widgetBlock. Returning null.'); // Avoid excessive prints
  // --- End Debugging Print ---
  return null;
}