0

There are two types of code markdown one is inline code(1 backticks) and another is codeblock (3 backticks)

Markdown(
      controller: scrollController,
      selectable: true,
      builders: {
        'code': CodeElementBuilder()
       },
      styleSheet: MarkdownStyleSheet(
        textScaleFactor: 1.0,
        codeblockPadding: EdgeInsets.all(10),
      ),
      data: data,
      styleSheetTheme: MarkdownStyleSheetBaseTheme.material,
   );

I create a custom codeElementBuilder by using this example Codeblock Syntax Builder

but it also apply to inline code how can I separate both style inlineCode and codeblock.

I am using flutter_markdown package to add markdown

1 Answer 1

1

You can add a builder to pre instead of code and it will apply only to code blocks. For example:

MarkdownBody(
...
 builders: {
   "pre": CodeBlockMarkdownElementBuilder(),
 },
...
);
class CodeMarkdownElementBuilder extends MarkdownElementBuilder {
  ScrollController verticalController = ScrollController();
  ScrollController horizontalController = ScrollController();
  @override
  Widget visitText(md.Text text, TextStyle? preferredStyle) {
    return ConstrainedBox(
      constraints: BoxConstraints(maxHeight: 200),
      child: Scrollbar(
        controller: verticalController,
        child: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          controller: verticalController,
          child: Scrollbar(
            controller: horizontalController,
            child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              controller: horizontalController,
              child: Text.rich(formatText(preferredStyle, text.text)),
            ),
          ),
        ),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It is working but when we use visitText(md.Text text .. we only get text code but I also want to get language Ex cpp,java at the starting of Element. Which is exist in visitElementBefore(md.Element e) or visitElementAfter(md.Element e) but these function not working with pre tag.
yeah, I'm afraid you won't be able to accomplish this without forking the package. Here is where you pre custom MarkdownBuilder's visitText is called: github.com/flutter/packages/blob/… Notice you are calling your custom builder instead of Scrollbar(...). That's the only place you can overwrite pre behavior.
I successfully did it with visit before and visitText dart var language = ''; @override void visitElementBefore(md.Element element) { if (element.tag == 'pre') { language = ''; final codeTag = element.children?.first; if (codeTag is md.Element && codeTag.attributes['class'] != null) { String lg = codeTag.attributes['class'] as String; language = lg.substring(9); } } super.visitElementBefore(element); } And then in visitText you now have the language.
Where formatText comes from?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.