Flutter load webview inside the fragment - flutter

// Here is my flutter code
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
class HairtipsPage extends StatefulWidget {
#override
_HairtipsPageState createState() => _HairtipsPageState();
}
class _HairtipsPageState extends State<HairtipsPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child : WebviewScaffold(
url: "https://www.google.com",
appBar: new AppBar(
// title: new Text('Hairtips'),
),
withZoom: true,
withLocalStorage: true,
)
),
);
}
}
I am using bottom navigation in my app and trying to implement webview inside the fragment.i know how to acheive the same in android also i dont want the webview should open in a browser.i am expecting the webview should load inside the app and within the fragment.

You can use the Flutter webview plugin. Here is the URL for the plugin https://pub.dartlang.org/packages/webview_flutter
The webview will load inside the App with CircularProgressIndicator.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebView extends StatefulWidget {
#override
_WebViewState createState() => _WebViewState();
}
class _WebViewState extends State<WebView> {
final Completer<WebViewController> _controller =
Completer<WebViewController>();
num _stackToView = 1;
void _handleLoad(String value) {
setState(() {
_stackToView = 0;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Builder(builder: (BuildContext context) {
return IconButton(
icon: Icon(Icons.volume_up, color: Colors.black,),
onPressed: () {
Navigator.pop(context);
},
);
}),
backgroundColor: Colors.white10,
elevation: 0,
),
body: IndexedStack(
index: _stackToView,
children: [
Column(
children: <Widget>[
Expanded(
child: WebView(
initialUrl: "https://www.google.co.in/",
javascriptMode: JavascriptMode.unrestricted,
onPageFinished: _handleLoad,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
)),
],
),
Container(
child: Center(child: CircularProgressIndicator(),)
),
],
));
}
}

Related

Web view doesn't show the web page in Flutter application

I have this code in my flutter app :
class WebViewWidget1 extends StatefulWidget {
WebViewWidget1({Key? key}) : super(key: key);
#override
State<WebViewWidget1> createState() => _WebViewWidget1State();
}
class _WebViewWidget1State extends State<WebViewWidget1> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('title'),
),
body: const WebView(
initialUrl: "https://github.com/ArjunMalhotra07",
javascriptMode: JavascriptMode.unrestricted,
),
);
}
}
I want to implement web view and this was the code I took from the internet. This just doesn't work. It shows a blank white screen with app bar only. Where am I wrong? can't deduce.
please add pubspec.yaml
flutter pub add flutter_inappwebview
inappwebview
Example:
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
class InAppWebViewPage extends StatefulWidget {
final String title, uri;
const InAppWebViewPage({Key? key, required this.title, required this.uri})
: super(key: key);
#override
_InAppWebViewPageState createState() => _InAppWebViewPageState();
}
class _InAppWebViewPageState extends State<InAppWebViewPage> {
int _stackIndex = 1;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: EdgeInsets.all(12),
child: Expanded(
child: IndexedStack(
index: _stackIndex,
children: [
InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse(widget.uri)),
initialOptions: InAppWebViewGroupOptions(
crossPlatform:
InAppWebViewOptions(useShouldOverrideUrlLoading: true),
),
onLoadStop: (_, __) {
setState(() {
_stackIndex = 0;
});
},
onLoadError: (_, __, ___, ____) {
setState(() {
_stackIndex = 0;
});
//TODO: Show error alert message (Error in receive data from server)
},
onLoadHttpError: (_, __, ___, ____) {
setState(() {
_stackIndex = 0;
});
//TODO: Show error alert message (Error in receive data from server)
},
),
const SizedBox(
height: 50,
child: CircularProgressIndicator(),
),
],
),
),
),
);
}
}
Try to wrap WebView with SizedBox and give width and height to it.
SizedBox(
width: MediaQuery.of(context).size.width, // it uses all the width of screen, you can change it
height: MediaQuery.of(context).size.height, // it uses all the height of screen, you can change it
child: WebView(
initialUrl: "https://github.com/ArjunMalhotra07",
javascriptMode: JavascriptMode.unrestricted,
),
),
And don't forget to add webview_flutter package to your pubspec.yaml file.

Flutter display blank web page with webview_flutter

I have a problem with the visualization of a web page with webview_flutter, I have to display a web view, when I run the code on ios the view is displayed correctly, when I run the same code on android I have a blank page, how do I solve this thing? To what it is due, I report below the source codes
Pubspec.yaml
ReportView.dart
import 'dart:io';
import 'package:appsafetyeurope/model/Manutenzione.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
String URLLOAD = "https://marconisoftware.com";
class ReportView extends StatefulWidget {
final Manutenzione man;
final String token;
final String url;
const ReportView(this.man, this.token, this.url);
#override
createState() => ReportViewState();
}
class ReportViewState extends State<ReportView> {
void _handleLoad(String value) {}
#override
void initState() {
super.initState();
// Enable virtual display.
if (Platform.isAndroid) {
//WebView.platform = SurfaceAndroidWebView();
WebView.platform = AndroidWebView();
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
iconSize: 20.0,
onPressed: () {
Navigator.pop(context);
},
),
centerTitle: true,
title: Text('Report')),
body: IndexedStack(
index: 0,
children: [
Column(
children: <Widget>[
Expanded(
child: WebView(
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
Map<String, String> headers = {"Cmdbuild-authorization": widget.token};
webViewController.loadUrl(
widget.url +
"/services/rest/v3/reports/286763/Activity%20report.pdf?extension=pdf&parameters=%7B%22ProcessId%22%3A" +
widget.man.id.toString() +
"%7D",
headers: headers);
},
onPageFinished: _handleLoad,
)),
],
),
Container(
color: Colors.white,
child: Center(
child: CircularProgressIndicator(),
),
),
],
));
}
}

How to show Loading Indicator background transparent in WebView Flutter?

I'm new to flutter and making my first webview app. Here I'm trying to add a spinner every time when a user tries to click the link or page load. I want to make spinner background opacity a bit low just like the given example(right picture) but opacity doesn't work at all.
Here is my approach:
// ignore_for_file: prefer_const_constructors
// ignore: use_key_in_widget_constructors
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(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: WebViewClass()
)
);
}
}
class WebViewClass extends StatefulWidget {
WebViewState createState() => WebViewState();
}
class WebViewState extends State<WebViewClass>{
int position = 1 ;
final key = UniqueKey();
doneLoading(String A) {
setState(() {
position = 0;
});
}
startLoading(String A){
setState(() {
position = 1;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: null,
body: SafeArea(child: IndexedStack(
index: position,
children: <Widget>[
WebView(
initialUrl: 'https://google.com',
javascriptMode: JavascriptMode.unrestricted,
key: key ,
onPageFinished: doneLoading,
onPageStarted: startLoading,
),
Container(
color: Colors.white70,
child: Center(
child: CircularProgressIndicator()),
),
]))
);
}
}
Any help or guidance will highly appreciated.
You can try like this. every time when you come in the web view screen. the loader will show.
class WebViewClass extends StatefulWidget {
WebViewState createState() => WebViewState();
}
class WebViewState extends State<WebViewClass> {
bool isLoading = false;
final key = UniqueKey();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: null,
body: SafeArea(
child: IgnorePointer(
ignoring: isLoading,
child: Stack(
children: [
WebView(
initialUrl: 'https://google.com',
javascriptMode: JavascriptMode.unrestricted,
key: key,
onPageFinished: (value) {
setState(() {
isLoading = false;
});
},
onPageStarted: (value) {
setState(() {
isLoading = true;
});
},
),
isLoading ? Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
color: Colors.grey.withOpacity(0.5),
child: const Center(child: CircularProgressIndicator()) ,
) : Container(),
],
),
)),);
}
}

Stacking Widgets Over InAppWebView

I have a page that displays an InAppWebView. I want to stack a widget on top of it. When I open the page, it displays the stacked widget for a split second but once the web view loads, the stacked widget disappears. I opened the flutter inspector and the widget does exist on the page, it just looks like it is hidden underneath the web view. Do I need to take a different approach here when stacking widgets on top of the InAppWebView?
class UnityFormViewPage extends StatefulWidget {
final UnityForm _selectedForm;
UnityFormViewPage(this._selectedForm);
#override
_UnityFormViewPageState createState() => _UnityFormViewPageState();
}
class _UnityFormViewPageState extends State<UnityFormViewPage> {
String currentUrl = '';
#override
Widget build(BuildContext context) {
return Material(
child: Scaffold(
appBar: AppBar(
title: Text(widget._selectedForm.title),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
}),
),
body: Stack(
children: [
OfflineBar(),
InAppWebView(
initialUrl: widget._selectedForm.formUrl,
onLoadStart: (InAppWebViewController controller, String url) {
setState(() {
this.currentUrl = url;
});
},
),
],
)),
);
}
}
You can copy paste run full code below
You can change sequence and use Positioned to position OfflineBar
code snippet
Stack(
children: [
InAppWebView(
initialUrl: widget._selectedForm.formUrl,
onLoadStart: (InAppWebViewController controller, String url) {
setState(() {
this.currentUrl = url;
});
},
),
Positioned(left: 0, right: 0, top: 20, child: OfflineBar()),
],
)
working demo
full code
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
class UnityForm {
String title;
String formUrl;
UnityForm({this.title, this.formUrl});
}
class UnityFormViewPage extends StatefulWidget {
final UnityForm _selectedForm;
UnityFormViewPage(this._selectedForm);
#override
_UnityFormViewPageState createState() => _UnityFormViewPageState();
}
class _UnityFormViewPageState extends State<UnityFormViewPage> {
String currentUrl = '';
#override
Widget build(BuildContext context) {
return Material(
child: Scaffold(
appBar: AppBar(
title: Text(widget._selectedForm.title),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
}),
),
body: Stack(
children: [
InAppWebView(
initialUrl: widget._selectedForm.formUrl,
onLoadStart: (InAppWebViewController controller, String url) {
setState(() {
this.currentUrl = url;
});
},
),
Positioned(left: 0, right: 0, top: 20, child: OfflineBar()),
],
)),
);
}
}
class OfflineBar extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(color: Colors.purple, child: Text("OfflineBar"));
}
}
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: UnityFormViewPage(
UnityForm(title: "test", formUrl: "https://flutter.dev/")),
);
}
}

How to add CircularProgressIndicator when loading web page using flutter_inappwebview?

I am new in flutter,
I tried to add a CircularProgressIndicator while waiting to the web page to load,
and then the page display and the circular indicator dosen't show on screen.
where it's should be? maybe in onLoadStart function below?
Source code:
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
class CoursesInformation extends StatefulWidget {
#override
_CoursesInformationState createState() => _CoursesInformationState();
}
class _CoursesInformationState extends State<CoursesInformation> {
InAppWebViewController webView;
#override
void initState() {
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(
title: const Text('מידע על קורסים'),
centerTitle: true,
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: InAppWebView(
initialUrl:
"https://shoham.biu.ac.il/BiuCoursesViewer/MainPage.aspx",
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
debuggingEnabled: true,
preferredContentMode: UserPreferredContentMode.DESKTOP),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) async {
},
))
])),
),
);
}
}
You can onProgressChanged parameter and show the loader. You can also use LinearProgressIndicator (check commented code). Check the following example
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
void main() {
// it should be the first line in main method
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark()
.copyWith(scaffoldBackgroundColor: Color.fromARGB(255, 18, 32, 47)),
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(body: CustomPage());
}
}
class CustomPage extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return CustomPageState();
}
}
class CustomPageState extends State<CustomPage> {
InAppWebViewController webView;
double progress = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('מידע על קורסים'),
centerTitle: true,
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: Stack(
children: <Widget>[
Align(
alignment: Alignment.center,
child: InAppWebView(
initialUrl:
"https://shoham.biu.ac.il/BiuCoursesViewer/MainPage.aspx",
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
debuggingEnabled: true,
preferredContentMode: UserPreferredContentMode.DESKTOP),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop:
(InAppWebViewController controller, String url) async {
},
onProgressChanged: (InAppWebViewController controller, int progress) {
setState(() {
this.progress = progress / 100;
});
},
),
),
Align(
alignment: Alignment.center,
child: _buildProgressBar()
),
],
))
])),
);
}
Widget _buildProgressBar() {
if (progress != 1.0) {
return CircularProgressIndicator();
// You can use LinearProgressIndicator also
// return LinearProgressIndicator(
// value: progress,
// valueColor: new AlwaysStoppedAnimation<Color>(Colors.orange),
// backgroundColor: Colors.blue,
// );
}
return Container();
}
}