flutter_html icon indicating copy to clipboard operation
flutter_html copied to clipboard

[FEATURE] align support

Open TheCarpetMerchant opened this issue 3 years ago • 7 comments

I'm not seeing the align tag in the Supported list, but text-align is, for example. Could it be possible to provide support for align, ie this being centered :

Html(
        data: '<p align="center">Some text</p>',
      ),

TheCarpetMerchant avatar May 24 '22 19:05 TheCarpetMerchant

This property is deprecated and not (officially) supported in html5. I am not sure if we should therefor support it in our package. On the other hand, we did decide to add support for the similarly deprecated font tag (see #277) so I guess we could?

erickok avatar Jun 10 '22 09:06 erickok

Well, there's probably an immense amount of HTML out there that still uses it, so I'd say it makes sense.

TheCarpetMerchant avatar Jun 10 '22 10:06 TheCarpetMerchant

Wouldn't hurt for backwards compatibility. Obviously we must draw the line somewhere when it comes to deprecated items. I'll look into this.

Sub6Resources avatar Jun 10 '22 17:06 Sub6Resources

@Sub6Resources I think it should have to do with usefulness and willingness. In this case, I suppose you already have what's needed to handle alignment ; it would just be a matter to use that what this attribute is present in addition to the other tags/attributes triggering that functionality. I hope so at least :)

TheCarpetMerchant avatar Jun 10 '22 18:06 TheCarpetMerchant

I agree. Shouldn't be too hard using those parameters. Thanks @TheCarpetMerchant

Sub6Resources avatar Jun 10 '22 21:06 Sub6Resources

Following up on this ; At first glance I'm seeing that Style.alignment is a thing. So parsing the align into that (I suppose from the HtmlParser.build method) seems like an easy solution. The question would be : does this override the potentially already-provided alignment (from the custom styles) ? Another solution would be to adapt the style directly : if the css doesn't provide an alignment, create one from the align parameter if it exists before the parsing of css_parser.dart.

TheCarpetMerchant avatar Jun 18 '22 14:06 TheCarpetMerchant

Yes, there are a couple other elements that use the inline attributes for styling purposes. I'm pretty busy and can't promise a quick implementation, but you are more than welcome to submit a pull request!

Sub6Resources avatar Jun 21 '22 04:06 Sub6Resources

Used such workaround, seems to be working Version 3.0.0-alpha5

class AppHtmlView extends StatelessWidget {
  final String html;
  final EdgeInsets? margin;
  final TextAlign? textAlign;

  const AppHtmlView({
    super.key,
    required this.html,
    this.margin,
    this.textAlign,
  });

  @override
  Widget build(BuildContext context) {
    TextAlign? textAlign;

    return Html(
      data: html,
      customRenders: {
        (RenderContext c) {
          final el = c.tree.element;
          final isCenterElement = el?.localName == 'center';
          if (isCenterElement) {
            textAlign = TextAlign.center;
            return isCenterElement;
          }

          final style = el?.attributes['style'];
          if (style == null) return false;

          textAlign = null;
          for (final s in style.split(';')) {
            if (!s.startsWith('text-align')) continue;
            textAlign = TextAlign.values
                .firstWhereOrNull((e) => e.name == s.split(':').last.trim());
            break;
          }

          return textAlign != null;
        }: CustomRender.widget(
          widget: (c, _) => SizedBox(
            width: MediaQuery.of(context).size.width,
            child: AppHtmlView(
              html: c.tree.element!.innerHtml,
              textAlign: textAlign,
              margin: EdgeInsets.zero,
            ),
          ),
        ),
      },
      shrinkWrap: true,
      style: {
        '*': Style(
          textAlign: this.textAlign,
          margin: margin,
        ),
      },
    );
  }
}

Andreigr0 avatar Nov 18 '22 14:11 Andreigr0