daohoangson / flutter_widget_from_html

Flutter package to render html as widgets that supports hyperlink, image, audio, video, iframe and many other tags.
https://pub.dev/packages/flutter_widget_from_html
MIT License
624 stars 229 forks source link

Unable to align images to center #265

Closed theneshofficial closed 4 years ago

theneshofficial commented 4 years ago

I'm trying to align images but unable to get done. I have no control of the HTML code so I had to do it on application side. Below is the HTML code:

<p>Shahril said young leaders need to show that when they are in positions of power, they do not continue “traditions” that have made voters cynical and resentful of politicians.</p>
<figure id="attachment_1462396" style="width: 200px" class="wp-caption alignright">
   <img class="size-full wp-image-1462396" src="https://s3media.freemalaysiatoday.com/wp-content/uploads/2020/08/Shahril-Hamdan-ms-100820-1.jpg" alt="" width="200" height="200" srcset="https://s3media.freemalaysiatoday.com/wp-content/uploads/2020/08/Shahril-Hamdan-ms-100820-1.jpg 200w, https://s3media.freemalaysiatoday.com/wp-content/uploads/2020/08/Shahril-Hamdan-ms-100820-1-150x150.jpg 150w" sizes="(max-width: 200px) 100vw, 200px" />
   <figcaption class="wp-caption-text">Shahril Hamdan</figcaption>
</figure>
<p>Wan Ahmad Fayhsal Ahmad Kamal of PPBM, who seeks election as party Youth leader, has come under scrutiny after a video clip made the rounds showing him purportedly offering support letters in exchange for votes.</p>

Please advise how do I align center? I'm using latest RC version 0.5.0-rc.2020071301 of flutter_widget_from_html. Thank you.

Output

Simulator Screen Shot - iPhone 11 Pro - 2020-08-11 at 09 41 44

daohoangson commented 4 years ago

Do you want to align all images in the HTML or only this specific instance?

theneshofficial commented 4 years ago

@daohoangson all images

daohoangson commented 4 years ago

If you want to center all images, it's quite easy. Just provide a custom WidgetFactory and wrap the image in Center widget. However, your HTML specify width: 200px so doing that alone won't work, you need to ignore the width too. See the full example below:

test.dart ```dart class Test extends StatelessWidget { @override Widget build(BuildContext context) => Scaffold( appBar: AppBar(title: Text('Issue 265')), body: SingleChildScrollView( child: HtmlWidget( kHtml, factoryBuilder: () => _WidgetFactory(), ), ), ); } class _WidgetFactory extends WidgetFactory { @override Widget buildImage(Object provider, ImgMetadata img) { var built = super.buildImage(provider, img); if (built == null) return built; return Center(child: built); } @override void parseStyle(NodeMetadata meta, String key, String value) { if (key == 'width') return; super.parseStyle(meta, key, value); } } const kHtml = """

Shahril said young leaders need to show that when they are in positions of power, they do not continue “traditions” that have made voters cynical and resentful of politicians.

Shahril Hamdan

Wan Ahmad Fayhsal Ahmad Kamal of PPBM, who seeks election as party Youth leader, has come under scrutiny after a video clip made the rounds showing him purportedly offering support letters in exchange for votes.

"""; ```

It will render like this:

theneshofficial commented 4 years ago

Thanks @daohoangson it works great.