Flutter onTap get particular Image source from htmlWidget - flutter

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

Related

Flutter PDF Rotation

I use this package (https://pub.dev/packages/pdf) to create PDF files with app.. Now I can't find a way to rotate the page of PDF.
Try this. Comments in code.
pdf.addPage(
pw.Page(
pageTheme: pw.PageTheme(
// orientation: pw.PageOrientation.landscape, //this set the orientation of the content of the page, not the page itself. Which confuses most people
pageFormat: PdfPageFormat.a4.landscape, //this is what you want
...
),
build: (ctx) => ChildWidget(),
...
Source: https://github.com/DavBfr/dart_pdf/issues/557

how to create zoomable image slider in flutter?

i needs zoomable image slider with cached network image something like this:
user can zoom on it and back
You can try this plugin from Flutter to create the slider "Carousel Slider"
https://pub.dev/packages/carousel_slider
and Include photos in Photo view widget
PhotoView(
imageProvider: AssetImage(photos),
backgroundDecoration: BoxDecoration(color: Colors.white),
),

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 to stop Flutter Web HtmlElementView from rebuilding/rerunning?

I have built a video calling app in Flutter using the Agora SDK. This is only available for iOS/Android and so for the web build I had to build a wrapper around the existing Agora web SDK. Due to the way Flutter renders web elements inside a shadow DOM, you cannot access elements by document.getElementById(), which is what the Agora SDK uses to inject their video player. To get around this I am rendering an IFrame that has the div and Agora SDK script bundled together.
It is all working nicely but when any event is triggered inside the browser window, such as the mouse entering a button or clicking anything, the IFrame refreshes and rebuilds the video view which takes 1-2 seconds to initialize.
Is there anyway I can unlink the IFrame from the browser events? I tried marking the HtmlElementView as const and putting it inside a StatefulWidget that only registers the platform view once. The widget isn't actually running the build() method again but the IFrame still refreshes.
Flutter code
ui.platformViewRegistry.registerViewFactory(
'video-container',
(int viewId) => IFrameElement()
..id = 'my-iframe'
..width = '100%'
..height = '100%'
..src = 'assets/web_elements/agora_container.html'
..allow = "camera; microphone"
..style.border = 'none');
#override
Widget build(BuildContext context) {
print("*****\n\n\nBuilding the web host container\n\n\n*****"); // this is only printing once
return const HtmlElementView(
viewType: 'video-container',
);
}
Agora code
<div id="local-video"></div>
<div id="remote-video"></div>
<div id="video-canvas"></div>
<script src="scripts/AgoraSDK.js"></script>
<script src="scripts/agora_controller.js"></script>
What worked for me was this:
Key key = UniqueKey(); // before the build method
HtmlElementView(
key: key,
viewType: 'iframeElement',
);
Now there is no rebuilding/rerunning. Note I am using the iframeElement, not video-container, but this should work. Please comment.
This is one of the active problem with the Flutter Web. I guess a workaround to this is mentioned here: https://github.com/flutter/flutter/issues/53253#issuecomment-607197549

I can't use svg on flutter web how can i do it?

Hi i cant seem to get svg images to showup on flutter web
i saw two questions on stackoverflow with green tick to the answer that it says
we can use Image.asset() as svg container but it doesn't work
what is the solution
Until there is a proper solution you can use a HtmlElementView to show a svg on flutter web.
HtmlElementView can be used like this:
HtmlElementView(
viewType: 'img-svg-${hashCode}',
)
You have to register your viewType first before you can use it:
platformViewRegistry.registerViewFactory('img-svg-${hashCode}',
(int viewId) {
final String base64 = base64Encode(utf8.encode(svgString));
final String base64String = 'data:image/svg+xml;base64,$base64';
final html.ImageElement element = html.ImageElement(
src: base64String, height: width.toInt(), width: width.toInt());
return element;
});
I created an example project that shows how to load svg from asset and how to do the web import dance if you want to use it for mobile and for web.