markdown_widget icon indicating copy to clipboard operation
markdown_widget copied to clipboard

Align text to center

Open NicolasLaube opened this issue 1 year ago • 7 comments

Hi,

First I want to thank you for this great package!

I noted that the text align seems no longer to be available with the new package version.

With the old version (1.XX.XX) I was able to center my markdown text using:

pConfig: PConfig(
   textConfig: TextConfig(textAlign: textAlign),
),

I don't see this option with the package version 2.1.10. Did I miss something ?

Thanks in advance!

NicolasLaube avatar Apr 21 '23 09:04 NicolasLaube

Hi @NicolasLaube There is indeed no option to set the TextAlign in markdown_widget 2.x.x, as it would result in the rendered markdown looking strange.

However, if you do want to try this parameter, you can refer to writing a custom ParagraphNode, and in the build() method, return the following code.

SpanNodeGeneratorWithTag(
    tag: MarkdownTag.p.name, generator: (e, config, visitor) => YourParagraphNode (config.p));

class YourParagraphNode extends ElementNode {
  final PConfig pConfig;

  YourParagraphNode (this.pConfig);

  @override
  InlineSpan build() {
    return WidgetSpan(child: Text.rich(TextSpan(
        children: List.generate(children.length, (index) {
          final child = children[index];
          return child.build();
        })), textAlign: TextAlign.center,));
  }
}

then pass the SpanNodeGeneratorWithTag to MarkdownGeneratorConfig generators

asjqkkkk avatar Apr 21 '23 14:04 asjqkkkk

Ok, thank you for your help!

NicolasLaube avatar Apr 21 '23 16:04 NicolasLaube

Hi ! I also want to thank you for your work !

Your solution works well for centering simple text, but not with latex elements containing simple text. (Based on this file: https://github.com/asjqkkkk/markdown_widget/blob/dev/example/lib/markdown_custom/latex.dart)

I tried to mix the two codes, but I can't do it. Is there a way to center the latex content even without the $$ sign?

Thanks in advance :)

xdidx avatar May 22 '23 11:05 xdidx

Hi @xdidx , I currently have no relatively complete method for filting true LaTeX expressions from ordinary text, so I am preparing to file an issue with flutter_math_fork to see if they can address this problem.

asjqkkkk avatar May 23 '23 03:05 asjqkkkk

Ok thanks, and maybe the MarkdownWidget can take only the width needed for the text if it is short, in order to center the whole MarkdownWidget?

xdidx avatar May 23 '23 08:05 xdidx

@xdidx you can use MarkdownBlock widget

asjqkkkk avatar May 23 '23 10:05 asjqkkkk

It works well, thanks !

xdidx avatar May 23 '23 19:05 xdidx