Is there a way to copy text from html package - flutter

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

Related

How to directly print a pdf file in assets with flutter?

I'm trying to print a pdf file using flutter app. I have used this Printing package to achieve this but the problem is it has this default page with options to save pdf and select printer. I don't want these options, I just want to print file in one click. How do I achieve this??
This is my code :
Scaffold(
body: Center(
child:
ElevatedButton(onPressed: ()async{
final pdf =await rootBundle.load("assets/dummy.pdf");
await Printing.layoutPdf(onLayout: (_) => pdf.buffer.asUint8List());
}, child: const Text("PRINT")),
),
);
I want print pdf without showing this page

Text widget cannot read html tags Flutter

Text widget in Flutter dose not support a special charterers like &, where sometimes this charterer comes like "first & second" from the database, or "&quot" should be ".
Text widget should read/convert these characters automatically. I have already fixed by following lines, But I need to put this solution in all Text widgets I have in my app.
Is it possible to give all Text widgets the possibility to read these special charterers directly in main function in runAPP or "MaterialApp" without putting the following lines in all Text widgets the app has?
final document = parse(htmlString);
final String parsedString = parse(document.body.text).documentElement.text;
Use this package flutter_html and try as follows:
Html(data: htmlString, style: {
"body": Style(
fontSize: FontSize(18),
color: Colors.black,
fontWeight: FontWeight.normal)
}),

how to dispose current page when using Navigator.pop in flutter

Now I am using Navigator.pop(context); to exit current page, but I want to dispose current page after pop(because current page has some gif animation and may cause other problem, it will rendering in backend will make the GPU busy all the time, and make the device heat quickly), what should I do to make it work like this? I am tried to
Navigator.popAndPushNamed(context, "..");
but I do not know how to specify the second paramter becuase it has many entry with current page, I have to specify the relative path.By the way, I am using flutter html to load the gif animation, the animation did not control by myself but the flutter html component. This is part of the code:
if (item.content != "")
Html(
data: item.content,
style: {
"body": Style(
fontSize: FontSize(19.0),
),
},
customImageRenders: defaultImageRenders,
onLinkTap: (String? url, RenderContext context, Map<String, String> attributes, dom.Element? element) {
CommonUtils.launchUrl(url);
}),

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?
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.

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