8

I have a flutter blog application, and i am using this package to render the blog HTML content, I want this content to be selectable, I know of the SelectableText() widget, but this cant be used to render HTML.

Anyone has an idea how I can make my HTML text selectable from mobile?

4 Answers 4

6

Just wrap it in SelectionArea().

For example:

SelectionArea(
  child: Html(
    data: '<p>Some data</p>',
  ),
);

The rendered html content will now be selectable.

The SelectableHtml() widget suggested by other answers has shortcomings as stated in their own docs on pub.dev and did not work well for me.

Sign up to request clarification or add additional context in comments.

1 Comment

This is by far the best option
4

Several hours ago, in flutter_html merged to master commit that solves this problem. Right not you can import plugin to your project like this:

flutter_html:
    git:
      url: git://github.com/Sub6Resources/flutter_html.git
      ref: master

Maybe for the time you are reading this post, the last changes will be published to pub.dev

Then, use SelectableHtml instead of Html:

SelectableHtml(
  data: menuButton.content ?? "",
  style: {
    "body": Style(
      margin: EdgeInsets.zero,
      color: Theme.of(context).primaryColor,
    ),
  },
  onLinkTap: (link, renderContext, map, element) async {
    if (link != null && link.isNotEmpty) {
      await launch(link);
    }
  },
)

1 Comment

Not good, SelectableHtml doesn't support customRender and onImageTap, kinda makes it useless for rich html.
1

Edit - It is now possible without any workarounds. Use K.Amanov's answer.

I achieved this using flutter_markdown and html2md packages. Not an ideal solution as markdown doesn't support all html tags.

import 'package:html2md/html2md.dart' as html2md;
import 'package:flutter_markdown/flutter_markdown.dart';
...
...
MarkdownBody(
  selectable: true,
  data: html2md.convert(
    'Your HTML Text here',
    ),
),

Comments

0

Library flutter_html:

In code:

SelectableHtml(
     data: 'Your text with html tag',
);

Comments

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.