Skip to content

Commit

Permalink
webview bug removed
Browse files Browse the repository at this point in the history
  • Loading branch information
lkrjangid1 committed Sep 14, 2022
1 parent 53022d8 commit d1876c7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 190 deletions.
207 changes: 20 additions & 187 deletions lib/Presentation/Screens/Dashboard/material/web_view_material_page.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import 'dart:async';
import 'dart:collection';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:mbm_elearning/Data/googleAnalytics.dart';
import 'package:mbm_elearning/Presentation/Constants/Colors.dart';
import 'package:mbm_elearning/Presentation/Constants/constants.dart';
import 'package:mbm_elearning/Presentation/Constants/utills.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tutorial_coach_mark/tutorial_coach_mark.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html_all/flutter_html_all.dart';
import 'package:url_launcher/url_launcher.dart';

class WebViewMaterialPage extends StatefulWidget {
Expand All @@ -23,189 +14,31 @@ class WebViewMaterialPage extends StatefulWidget {

class _WebViewMaterialPageState extends State<WebViewMaterialPage> {
final GlobalKey webViewKey = GlobalKey();
InAppWebViewController? webViewController;
InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: true),
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
),
ios: IOSInAppWebViewOptions(
allowsInlineMediaPlayback: true,
),
);

late PullToRefreshController pullToRefreshController;
final StreamController<WebViewButtonStatus?> showButtonStatus =
StreamController<WebViewButtonStatus?>();
final StreamController<int> isPageLoading = StreamController<int>();

@override
void initState() {
super.initState();
pullToRefreshController = PullToRefreshController(
options: PullToRefreshOptions(
color: Colors.blue,
),
onRefresh: () async {
if (Platform.isAndroid) {
webViewController?.reload();
} else if (Platform.isIOS) {
webViewController?.loadUrl(
urlRequest: URLRequest(url: await webViewController?.getUrl()));
}
},
);
}

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
bottomNavigationBar: ButtonBar(
alignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
SizedBox(),
ElevatedButton(
child: Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
webViewController?.goBack();
},
),
ElevatedButton(
child: Icon(Icons.arrow_forward, color: Colors.white),
onPressed: () {
webViewController?.goForward();
},
),
ElevatedButton(
child: Icon(Icons.refresh, color: Colors.white),
onPressed: () {
webViewController?.reload();
},
),
StreamBuilder<WebViewButtonStatus?>(
stream: showButtonStatus.stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data == WebViewButtonStatus.yt) {
return ElevatedButton(
child:
const Icon(Icons.open_in_browser, color: Colors.white),
onPressed: () async {
var url = await webViewController?.getUrl();
if (url != null) {
launchUrl(url);
}
},
);
} else if (snapshot.data == WebViewButtonStatus.dl) {
return ElevatedButton(
child: const Icon(Icons.download, color: Colors.white),
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Coming soon')),
);
},
);
} else {
return SizedBox();
}
} else {
return SizedBox();
}
},
),
appBar: AppBar(
title: Text(widget.title),
actions: [
if (widget.url.contains("youtube.com") ||
widget.url.contains("you.tube"))
IconButton(
onPressed: () {
launch(widget.url);
},
icon: const Icon(Icons.open_in_new),
),
],
),
body: SafeArea(
child: Expanded(
child: Stack(
children: [
InAppWebView(
key: webViewKey,
initialUrlRequest: URLRequest(url: Uri.parse(widget.url)),
initialUserScripts: UnmodifiableListView<UserScript>([]),
initialOptions: options,
pullToRefreshController: pullToRefreshController,
onWebViewCreated: (controller) {
webViewController = controller;
},
onLoadStart: (controller, url) {
if (url.toString().contains('youtube.com')) {
showButtonStatus.add(WebViewButtonStatus.yt);
} else if (url.toString().contains('/file/')) {
showButtonStatus.add(WebViewButtonStatus.dl);
} else {
showButtonStatus.add(null);
}
},
androidOnPermissionRequest:
(controller, origin, resources) async {
return PermissionRequestResponse(
resources: resources,
action: PermissionRequestResponseAction.GRANT);
},
onLoadStop: (controller, url) async {
pullToRefreshController.endRefreshing();
if (url.toString().contains('youtube.com')) {
showButtonStatus.add(WebViewButtonStatus.yt);
} else if (url.toString().contains('/file/')) {
showButtonStatus.add(WebViewButtonStatus.dl);
} else {
showButtonStatus.add(null);
}
},
onLoadError: (controller, url, code, message) {
pullToRefreshController.endRefreshing();
},
onProgressChanged: (controller, progress) {
if (progress == 100) {
pullToRefreshController.endRefreshing();
}
isPageLoading.add(progress);
},
onUpdateVisitedHistory: (controller, url, androidIsReload) {
if (url.toString().contains('youtube.com')) {
showButtonStatus.add(WebViewButtonStatus.yt);
} else if (url.toString().contains('/file/')) {
showButtonStatus.add(WebViewButtonStatus.dl);
} else {
showButtonStatus.add(null);
}
},
onConsoleMessage: (controller, consoleMessage) {
print(consoleMessage);
},
),
StreamBuilder<int>(
stream: isPageLoading.stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data != null) {
if (snapshot.data != 100) {
return LinearProgressIndicator(
value: snapshot.data! / 100.0,
);
} else {
return const SizedBox();
}
} else {
return const SizedBox();
}
} else {
return const SizedBox();
}
},
),
],
),
child: Html(
data: """
<iframe src="${widget.url}" width="${size.width}" height="${size.height * 0.88}">Loading...</iframe>""",
customRenders: {
iframeMatcher(): iframeRender(),
},
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ packages:
source: hosted
version: "3.0.0-alpha.4"
flutter_inappwebview:
dependency: "direct main"
dependency: transitive
description:
name: flutter_inappwebview
url: "https://pub.dartlang.org"
Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: mbm_elearning
description: mbm_elearning, A notes sharing platform.

version: 7.1.0+17
version: 7.1.2+19

environment:
sdk: ">=2.15.1 <3.0.0"
Expand Down Expand Up @@ -51,7 +51,6 @@ dependencies:
chips_choice_null_safety: ^2.0.6
lottie: any
update_available: ^2.2.0-dev.1 # android and ios
flutter_inappwebview: ^5.4.3+7
path_provider: ^2.0.11


Expand Down

0 comments on commit d1876c7

Please sign in to comment.