Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make html in HTML() widget selectable

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?

like image 911
olayemii Avatar asked Oct 16 '22 01:10

olayemii


2 Answers

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);
    }
  },
)
like image 116
K.Amanov Avatar answered Oct 19 '22 01:10

K.Amanov


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',
    ),
),
like image 21
Gibreel Abdullah Avatar answered Oct 19 '22 01:10

Gibreel Abdullah