This is how we can display a webview url in flutter.
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return WebviewScaffold(
appBar: AppBar(
title: Text('Browser'),
),
url: "https://twitter.com/gowithkel/status/1137024385229950976");
}
}
But how could a twitter tweet be embeded in flutter? For example, displaying a twitter tweet that is in oembed format:
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Sunsets
don't get much better than this one over <a
href="https://twitter.com/GrandTetonNPS?
ref_src=twsrc%5Etfw">#GrandTetonNPS</a>. <a
href="https://twitter.com/hashtag/nature?
src=hash&ref_src=twsrc%5Etfw">#nature</a> <a
href="https://twitter.com/hashtag/sunset?
src=hash&ref_src=twsrc%5Etfw">#sunset</a> pic.twitter.com/YuKy2rcjyU</p>—
US Department of the Interior (#Interior) <a
href="https://twitter.com/Interior/status/463440424141459456?
ref_src=twsrc%5Etfw">May 5, 2014</a></blockquote> <script async
src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
Related
What happens is that I made an app with a button that redirects to whatsapp the problem is that in Android if it works and not in iOS, I have done it with the normal method, I think you have to put something in an iOS file but I don't know That is,
Thanks.
ListTile(
leading: Icon(FontAwesomeIcons.headset,color: Colors.black,),
title: Text('Centro de ayuda '),
onTap: () async => await launch("https://wa.me/${numero}?text=Hola mi nombre es "+prefs.name+' y necesito ayuda con mi orden de Timugo')
)
You can add Flutter launch WhatsApp.
INSTALLATION:
First, add flutter_launch_whatsapp as a dependency in your pubspec.yaml file.
In iOS add the following Info.plist :
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
Example:
import 'package:flutter/material.dart';
import 'package:flutter_launch/flutter_launch.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
initState() {
super.initState();
}
void whatsAppOpen() async {
await FlutterLaunch.launchWathsApp(phone: "5534992016545", message: "Hello");
}
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Plugin example app'),
),
body: new Center(
child: FlatButton(
child: Text("Open WhatsApp"),
onPressed: () {
whatsAppOpen();
},
)
),
),
);
}
}
Above reference from:
https://pub.dev/packages/flutter_launch
OR
You can try below code:
var whatsappUrl ="whatsapp://send?phone=$phone";
await canLaunch(whatsappUrl)? launch(whatsappUrl):print("open whatsapp app link or do a snackbar with notification that there is no whatsapp installed");
The webview_flutter plugin is unable to play some YouTube embed videos that do work if played from within a web app. The videos display "Video unavailable". Playing YouTube videos inline in a Flutter app has been an issue for at least a year.
https://github.com/flutter/flutter/issues/13756
I have tried various Flutter plugins to play YouTube video inline but either they only support Android or don't work with YouTube.
I have tried using HTML string (YouTube iframe) directly inside WebView plugin but the video is unavailable. Loading HTML from a webserver allows for some videos to be played which otherwise didn't directly in the flutter app e.g. Some music videos display "Video unavailable. This video contains content from Vevo, who has blocked it from display on the website or application".
But if I launch the same video using the YouTube iframe API (see code) from a web application, it works without any errors i.e. the embed is not disabled but these videos don't play in Flutter app. I have also read similar issues with playing YouTube videos in Android where suggestion was to use a WebChromeClient, that is unavailable in Flutter.
Flutter app that displays a YouTube video inside WebView plugin.
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
// Use IP instead of localhost to access local webserver
const _youTubeUrl = 'http://localhost:8080';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'YouTube Video',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'YouTube Video'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
WebViewController _controller;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Expanded(
child: WebView(
initialUrl: '$_youTubeUrl/videos/IyFZznAk69U',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController controller) =>
_controller = controller,
),
),
],
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.play_arrow),
onPressed: () async {
await _controller.evaluateJavascript('player.loadVideoById("d_m5csmrf7I")');
},
),
);
}
}
Node.js server side code that returns index.html via express routing
const path = require("path");
const express = require("express");
const server = express();
// Define route
api.get("/", (req, res) =>
res.sendFile(path.join(__dirname + "/index.html"))
);
const port = process.env.PORT || 8080;
server.listen(port, () => console.log(`Server listening on ${port}`));
process.on("exit", () => console.log("Server exited"));
index.html file that uses YouTube iframe API served to the Flutter app
<!DOCTYPE html>
<html>
<head>
<style>
#player {
position: relative;
padding-top: 56.25%;
min-width: 240px;
height: 0;
}
iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
<script>
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player("yt", {
videoId: "IyFZznAk69U",
playerVars: {
autoplay: 1
}
});
}
</script>
<script src="https://www.youtube.com/iframe_api" async></script>
</head>
<body>
<div id="player">
<div id="yt"></div>
</div>
</body>
</html>
You can use my plugin flutter_inappwebview to play youtube videos inside a webview.
Full example with your youtube video id:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
void initState() {
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: InAppWebViewPage()
);
}
}
class InAppWebViewPage extends StatefulWidget {
#override
_InAppWebViewPageState createState() => new _InAppWebViewPageState();
}
class _InAppWebViewPageState extends State<InAppWebViewPage> {
InAppWebViewController webView;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: Container(
child: InAppWebView(
initialUrl: "https://www.youtube.com/embed/IyFZznAk69U",
initialHeaders: {},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
),
),
),
]))
);
}
}
UPDATE: This plugin seems better (mentioned on the jira issue url) but not official from the Flutter team
https://github.com/hoanglm4/flutter_youtube_view
I am using flutter Webview plugin which works fine but today i face a issue while displaying a webpage which contains only a image with 100% width and height.
Therefore flutter Webview shows a blank page. When i change the height and width to a specific px for example 320px and 220px then only webview plugin shows the image otherwise with 100% the webpage goes blank.
You can try my plugin flutter_inappbrowser (EDIT: it has been renamed to flutter_inappwebview).
An example loading an html source directly in the InAppWebView widget with an img with width and height to 100%:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
void initState() {
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: InAppWebViewPage()
);
}
}
class InAppWebViewPage extends StatefulWidget {
#override
_InAppWebViewPageState createState() => new _InAppWebViewPageState();
}
class _InAppWebViewPageState extends State<InAppWebViewPage> {
InAppWebViewController webView;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: Container(
child: InAppWebView(
initialData: InAppWebViewInitialData(
data: """
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style>
img {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<img src="https://via.placeholder.com/300" alt="placeholder">
</body>
</html>
"""
),
initialHeaders: {},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
)
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
),
),
),
]))
);
}
}
Is there any possibility to load complete website (including associated files) from local assets ? I tried it with flutter plugin webview_flutter and loaded index.html.
Future<String> loadLocal() async {
return await rootBundle.loadString('assets/mywebsite/index.html');
}
only html code is being rendered and associated javascripts are not working.
You can follow https://github.com/flutter/flutter/issues/27086
In the meantime you can implement a web server in Dart that serves files from assets and point the webview to that integrated web server.
https://medium.com/#segaud.kevin/facebook-oauth-login-flow-with-flutter-9adb717c9f2e is about how to do that in Dart.
I've been able to do this but it's working only in Android. I've used the InAppWebView library in the following way :
child: Container(
child: InAppWebView(
initialFile: "images/test.html",
initialHeaders: {},
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {},
onLoadStop: (InAppWebViewController controller, String url) {},
onConsoleMessage: (InAppWebViewController controller,
ConsoleMessage consoleMessage) {
print(consoleMessage.message);
},
),
),
The html file, including the related files have been added in the project assets.
You can try also my plugin flutter_inappwebview, which is a Flutter plugin that allows you to add inline WebViews or open an in-app browser window and has a lot of events, methods, and options to control WebViews.
To load a complete website from assets, you need to declare the corresponding files in the pubspec.yaml file:
...
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
assets:
- assets/index.html
- assets/page-1.html
- assets/page-2.html
- assets/js/
- assets/css/
- assets/images/
...
Then, you can load your index.html file using initialFile parameter of the InAppWebView widget:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
void initState() {
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: InAppWebViewPage()
);
}
}
class InAppWebViewPage extends StatefulWidget {
#override
_InAppWebViewPageState createState() => new _InAppWebViewPageState();
}
class _InAppWebViewPageState extends State<InAppWebViewPage> {
InAppWebViewController webView;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: Container(
child: InAppWebView(
initialFile: "assets/index.html",
initialHeaders: {},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
onConsoleMessage: (InAppWebViewController controller, ConsoleMessage consoleMessage) {
print(consoleMessage.message);
},
),
),
),
]))
);
}
}
In your index.html you will have something like that:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- this is a css file inside the assets folder -->
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!-- this is a javascript file inside the assets folder -->
<script src="js/main.js"></script>
</body>
</html>
I have this code below, I like to display the videos from facebook to flutter app.
But instead it only display a link to the video, when I click the link it brings me to facebook video. I like to display only the video and not the link, please help.
import 'package:flutter/material.dart';
import 'package:flutter_html_view/flutter_html_view.dart';
//import 'package:html2md/html2md.dart';
//import 'package:flutter_markdown/flutter_markdown.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
String html = '<div class="fb-video" data-href="https://www.facebook.com/FacebookAU/videos/10152167660372415/" data-width="500" data-show-text="true"><blockquote cite="https://www.facebook.com/FacebookAU/videos/10152167660372415/" class="fb-xfbml-parse-ignore">Happy New You<p>Here’s to a healthier, dancier, artier, funner New You in 2014.</p>Posted by Facebook on Wednesday, January 8, 2014</blockquote></div>';
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Plugin example app'),
),
body: new SingleChildScrollView(
child: new Center(
child: new HtmlView(data: html),
),
),
),
);
}
}
Displaying Link
Video is shown
you can try this version
https://pub.dev/packages/flutter_widget_from_html
build HTML content like below:
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
String html = '''
<iframe width="200" height='200'
src="https://www.facebook.com/v2.3/plugins/video.php?
allowfullscreen=false&autoplay=true&href=${FB_VIDEO_URL}" </iframe>
''';
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Plugin example app'),
),
body: new SingleChildScrollView(
child: HtmlWidget(
html,
webView: true,
),
),
),
);
}
}
replace FB_VIDEO_URL by your link
it worked for me
both facebook and youtube (youtube: repace attribute src in tag) video.
hope this helps.