Flutter / webview_flutter too big to fit screen - flutter

I am running flutter 1.17.1, using webview_flutter: ^0.3.21
dependencies added to pubspec.yaml and added this to the end of info.plist
<key>io.flutter.embedded_views_preview</key>
<string>YES</string>
Problem: Webpage loaded into webview is too big to fit mobile phone screen.
screenshot
Here is the code with the webview:
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebViewContainer extends StatefulWidget {
final url;
WebViewContainer(this.url);
#override
createState() => _WebViewContainerState(this.url);
}
class _WebViewContainerState extends State<WebViewContainer> {
var _url;
final _key = UniqueKey();
_WebViewContainerState(this._url);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: [
Expanded(
child: WebView(
key: _key,
javascriptMode: JavascriptMode.unrestricted,
initialUrl: _url))
],
));
}
}
Link to the full app:
https://github.com/bi-samson/mreader

I've tried a minimal repro with the url "https://www.businessinsider.jp/" and the latest version of webview_flutter, this is the output:
Minimal repro:
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:async';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final Completer<WebViewController> _controller =
Completer<WebViewController>();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: WebView(
initialUrl: "https://www.businessinsider.jp/",
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
javascriptMode: JavascriptMode.unrestricted,
),
);
}
}
It seems that the issue doesn't occur in the latest version.

Use InAppWebView instead of WebView.
This will add a line like this to your package's pubspec.yaml:
dependencies:
flutter_inappwebview: ^5.3.2
Here is a code example:
return InAppWebView(
initialOptions: InAppWebViewGroupOptions(
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
textZoom: 100 * 2 // it makes 2 times bigger
)
),
onWebViewCreated: (InAppWebViewController controller) {
webViewController = controller;
controller.loadData(data: widget.html, mimeType: "text/html");
},
);
If you want to make it smaller, you just need to divide it as below:
textZoom: 100 / 2 // it makes 2 times smaller

Related

Deploy docusaurus site (generated build files) offline inside flutter webview

I just started develop a website using docusaurus and I want to run this website within flutter on mobile (android or ios) ,I thought of running the build files (index.html) through flutter webview, but by search i found it diffucult to run docusaurus site without webserver, I have tried way to host a local build of an docusaurus website in flutter webview by use local_assets_server flutter library
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:local_assets_server/local_assets_server.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Local Assets Server Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Local Assets Server Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key? key, required this.title}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isListening = false;
String? address;
int? port;
WebViewController? controller;
void _incrementCounter() {
controller?.evaluateJavascript('window.increment()');
}
#override
initState() {
_initServer();
super.initState();
}
_initServer() async {
final server = new LocalAssetsServer(
address: InternetAddress.loopbackIPv4,
assetsBasePath: 'web',
logger: DebugLogger(),
);
final address = await server.serve();
setState(() {
this.address = address.address;
port = server.boundPort!;
isListening = true;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: isListening
? WebView(
debuggingEnabled: true,
initialUrl: 'http://$address:$port',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (c) {
controller = c;
},
)
: Center(child: CircularProgressIndicator()),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
but flutter webview give this message

status bar overlapping my WebView screen, how to fix this?

I am building APK for my web app but the upper part of the web app is overlapped by the status bar
I want to show the status bar but not overlap my Webview, Webview must use screen size except status bar
check screenshot of the problem
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(const myapp());
}
// ignore: camel_case_types
class myapp extends StatelessWidget {
const myapp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "Sample App",
theme: ThemeData(
primarySwatch: Colors.red,
),
home: const Scaffold(
appBar: null ,
body: WebView(
initialUrl: 'https://primexop.com/motap/appw1/index.php?app_v=1.001010&ow_id=101',
javascriptMode: JavascriptMode.unrestricted,
)),
);
}
}
wrap your Scaffold or WebView with SafeArea Widget
check the code below which fixes this issue
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(const myapp());
}
// ignore: camel_case_types
class myapp extends StatelessWidget {
const myapp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "Sample App",
theme: ThemeData(
primarySwatch: Colors.red,
),
home: const SafeArea(
child: Scaffold(
appBar: null,
body: WebView(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: "https://primexop.com/motap/appw1/index.php?app_v=1.001010&ow_id=101",
)),
),
);
}
}
I used SafeArea to wrap the Scaffold
Result screenshot

Flutter Webview : Alternate screen for inital url not loaded

I need an alternate screen when my webpage not loaded;
Note: Webpage was hosting on localhost, so internet connectivity based changeover not possible.
Query: Can we ping IP and change over the screens
Any Possible ways to achieve this ?
** main.dart **
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter webview',
home: Scaffold(
appBar: AppBar(
title: const Text('flutter webview'),
),
body: WebView(
initialUrl: "http://192.168.29.8/public/_weblogin.html",
javascriptMode: JavascriptMode.unrestricted)),
);
}
}
You can view alternative designs on onWebResourceError
full code:
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomeView(),
);
}
}
class HomeView extends StatefulWidget {
const HomeView({Key? key}) : super(key: key);
#override
_HomeViewState createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
bool isError = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Home"),
),
body: Stack(
children: [
if (!isError)
WebView(
initialUrl: 'http://192.168.29.8/public/_weblogin.html',
javascriptMode: JavascriptMode.unrestricted,
onWebResourceError: (error) => setState(() {
isError = true;
}),
),
if (isError) const Center(child: Text("WEB PAGE ERROR")),
],
),
);
}
}

How to get rid of address bar and bottom bar in Flutter 'WebBrowser' Plugin?

My App is a Flutter WEB app.
This is the plugin I am using to display some HTML coming from the server:
https://pub.dev/packages/web_browser
Here is the code I am using:
WebBrowser(
initialUrl: 'https://${Provider.of<CP>(context).getPaymentURL()+Provider.of<CP>(context).getPaymentPATH()}?O=${Provider.of<CP>(context).getOrder().orderUID}&Ro=${Provider.of<CP>(context).getOrder().res_key}',
),
My screen looks like below:
I would like to rid of the address bar and bottom bar but not sure how. Can someone help please?
You can copy paste run full code below
You can use attribute interactionSettings and set WebBrowserInteractionSettings's topBar and bottomBar to SizedBox.shrink()
code snippet
return WebBrowser(
initialUrl: 'https://dart.dev/',
interactionSettings: WebBrowserInteractionSettings(
topBar: SizedBox.shrink(), bottomBar: SizedBox.shrink()));
working demo
full code
import 'package:flutter/material.dart';
import 'package:web_browser/web_browser.dart';
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return WebBrowser(
initialUrl: 'https://dart.dev/',
interactionSettings: WebBrowserInteractionSettings(
topBar: SizedBox.shrink(), bottomBar: SizedBox.shrink()));
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text("Demo"),
),
body: MyWidget()),
);
}
}

How to get final url from webview flutter

I am making Downloader app which has a Webview where user can browse when there is the video playing he can download by click float button .but the problem is that it has only initial URL I want final URL to send it to my API to download that video.How to get Final Url in webview
import 'dart:html';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main(){
runApp(myapp());
}
class myapp extends StatefulWidget {
#override
_myappState createState() => _myappState();
}
final Completer<WebViewController> _controller = Completer<WebViewController>();
class _myappState extends State<myapp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: new Text('Fb Video Downloader'),
backgroundColor: Colors.blueAccent,
),
body: Column(
children: <Widget>[
new Text(''),
WebView(
initialUrl: "https://example.com",
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webcontroller){
_controller.complete(webcontroller);
},
),
],
),
),
);
}
}`
Looking at the documentation for WebViewController it seems like it has a method called currentUrl() that probably will give you the result you're looking for.
currentUrl = await _yourWebController.currentUrl()