How to make html in HTML() widget selectable - flutter

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?

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);
}
},
)

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',
),
),

Library flutter_html:
In code:
SelectableHtml(
data: 'Your text with html tag',
);

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.

Related

Is there a way to copy text from html package

I use the fluter_html package. Is there a way, to make the text Copyable? The Html Widget is in a CustomScrollView with a SliverList. I want to copy the text without the html stuff. As an example, I want to copy "Hello, copy me" and not "Hello <br> copy me"
Edit: I want to copy on Android and MacOs Desktop
This is my Html Widget.
Html(
data: singleNews.text,
style: {
"body": Style(
fontSize: FontSize(
(MediaQuery.of(context).size.width) / 70),
)
},
onLinkTap: (url, _, __, ___) async {
if (await canLaunch(url!)) {
await launch(url);
_log.i("Opening $url...");
} else {
_log.e('Could not launch $url');
}
},
),
From looking at the docs it looks like the package has a 'Selectable' option, so just change your widget from Html to SelectableHtml, which will give you the desired function, you will be able to select the text and a tooltip will appear with the 'Copy' option.
https://pub.dev/documentation/flutter_html/latest/flutter_html/SelectableHtml-class.html

How to show emoji in flutter

I have an api from which I am fetching messages. These messages might contain emoji.
When it does contain emoji Text Widget is not displaying it correctly.
I just want to display them on the screen. Help if possible.
Thank you :)
class Message{
String text;
Message(){
this.text = get_data_from_api()['text'];
}
}
message = Message();
return Container(
child: Text(message.text),
);
No Displaying text with Emojis on Flutter
won't do it. I can do that but it only works if you have emoji in a string, that is explicitly plasing an emoji in string works fine for me. but when I am using something similar to the code above. I am getting, this
enter image description here
digging more into it I found that if I print my response on console it is
"ð­ð­ð­ð­"
and if I print a emoji it is 😭
So
I am using
Response.fromStream(await request.send())
from http/http.dart
So is that the problem ?
I did figured it out
I was taking data from an API. So while decoding for some reason it was not decoding utf-8 so what I needed to do was add
utf8.decode(response.bodyBytes);
in place of response.body and it was solved
Reference: Emoji and accent encoding in dart/flutter
If you have an emoji image explicitly present in the response from your API and you want to display it in your text, your should try adding WidgetSpan inside your RichText below is an example of how to do it :
RichText(
text: TextSpan(
children: [
TextSpan(
text: "Click ",
),
WidgetSpan(
child: Icon(Icons.add, size: 14),
),
TextSpan(
text: " to add",
),
],
),
),
In the above example they have used the material icon( Icon(Icons.add)). If you want to replace that from the image your get from the API, you can use ImageIcon instead of Icon widget. Follow this link to know how to use ImageIcon : https://www.woolha.com/tutorials/flutter-using-imageicon-widget-examples
Reference : Add image to RichText element

Flutter onTap get particular Image source from htmlWidget

I have html coming from Json and saved it in the String and thereafter used flutter_widget_from_html dependency to view html as webview.
I want to get particular Image url onTap particular image in htmlWidget.
In android i have onTouchListener from where we can easily extract the image but I don't know how to do it in Flutter.
Directionality(textDirection: TextDirection.rtl,
child: HtmlWidget("<p><img class="alignnone size-full wp-image-231636" src="http://abc/wp-content/uploads/2019/09/big13-48.jpg" alt="" width="600" height="800" srcset="http://abc/wp-content/uploads/2019/09/big13-48.jpg 600w, http://abc/wp-content/uploads/2019/09/big13-48-450x600.jpg 450w" sizes="(max-width: 600px) 100vw, 600px" /></p>",
webView: true,
),
),
You can do that by using builderCallback. It provide you the functionality to customize the UI widgets while its rendering.
Follow this url.
https://github.com/daohoangson/flutter_widget_from_html/issues/50#issuecomment-500928520
dependencies:
flutter_html: ^0.11.0
You will receive that image inside it.
onImageTap: (src) {
// Display the image in large form.
}
Reference link:
https://github.com/Sub6Resources/flutter_html

How can I parse and replace a word as a hashtag link when user types some text using flutter?

I want when a user types some text to make a post. But the thing is when the user types a hashtag (e.g. #avengers) I can parse and replace that hashtag with a link or ontap widget... Same goes with URL links
Assuming that you want to do this once the user has finished posting and not inside the typing bar:
The smart_text_view 0.1.0 package contains something called LinkTextSpan, which you can use to easily implement this on the view. Their example folder contains an example that was implemented.
As shown in the Flutter's Official Implementation here for the Flutter Gallery Drawyer, you can do this:
_LinkTextSpan(
style: linkStyle,
url: 'https://something.com',
text: 'flutter github repo',
),
and
_LinkTextSpan({ TextStyle style, String url, String text }) : super(
style: style,
text: text ?? url,
recognizer: TapGestureRecognizer()..onTap = () {
launch(url, forceSafariVC: false);
}
);

How can I justify text with Flutter markdown?

I started using flutter markdown, however I'd like to justify the content, and I couldn't until now.
I tried using a Center and Alignment but didn't work.
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
class OutsideBankHourDescription extends StatelessWidget {
#override
Widget build(BuildContext context) {
String text =
"Antecipações em __horários__ bancários acontecem em 1h na média. __Fora do horário bancário__ o saldo estará em sua conta __no dia seguinte.__";
return Expanded(
child: Container(
alignment: Alignment.center,
child: Markdown(
styleSheet: MarkdownStyleSheet.fromTheme(Theme.of(context)),
data: text,
),
),
);
}
}
It's not available for now to change text alignment in flutter_markdown 0.2.0.
You must contact the authors of this plugin to request this feature.
But if you need fast fix, you can add textAlign: TextAlign.center attribute in source code of this file: https://github.com/flutter/flutter_markdown/blob/master/lib/src/builder.dart
Line of code: 345
mergedTexts.add(new RichText(text: mergedSpan, textAlign: TextAlign.center));
Result:
For more elegant way you must clone git repository of this plugin, attach to your project directly and work to add text alignment feature on your own.
It becomes possible to set up proper text alignment with flutter_markdown.
var style = MarkdownStyleSheet(
textAlign: WrapAlignment.center,
h1Align: WrapAlignment.center,
);
Markdown(
styleSheet: style,
data: '# header1 is center-aligned\ntext body is also center-aligned'
);
You can customized your styles with all the other optional parameters provided by MarkdownStyleSheet class.