flutter_inappwebview_web 1.2.0-beta.3
flutter_inappwebview_web: ^1.2.0-beta.3 copied to clipboard
Web implementation of the flutter_inappwebview plugin.
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
import 'package:flutter_inappwebview_web/flutter_inappwebview_web.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final GlobalKey webViewKey = GlobalKey();
WebPlatformInAppWebViewController? webViewController;
InAppWebViewSettings settings = InAppWebViewSettings(
iframeAllow: "camera; microphone", iframeAllowFullscreen: true);
String url = "";
double progress = 0;
final urlController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Official InAppWebView website")),
body: SafeArea(
child: Column(children: <Widget>[
TextField(
decoration: const InputDecoration(prefixIcon: Icon(Icons.search)),
controller: urlController,
keyboardType: TextInputType.url,
onSubmitted: (value) {
var url = WebUri(value);
if (url.scheme.isEmpty) {
url = WebUri("https://www.google.com/search?q=$value");
}
webViewController?.loadUrl(urlRequest: URLRequest(url: url));
},
),
Expanded(
child: Stack(
children: [
WebPlatformInAppWebViewWidget(
WebPlatformInAppWebViewWidgetCreationParams(
key: webViewKey,
initialUrlRequest:
URLRequest(url: WebUri("https://inappwebview.dev/")),
initialSettings: settings,
onWebViewCreated: (controller) {
webViewController =
controller as WebPlatformInAppWebViewController;
},
onLoadStart: (controller, url) {
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
shouldOverrideUrlLoading:
(controller, navigationAction) async {
var uri = navigationAction.request.url!;
if (![
"http",
"https",
"file",
"chrome",
"data",
"javascript",
"about"
].contains(uri.scheme)) {
// Cancel navigation for non-standard schemes
return NavigationActionPolicy.CANCEL;
}
return NavigationActionPolicy.ALLOW;
},
onLoadStop: (controller, url) async {
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
onReceivedError: (controller, request, error) {
// Handle error
},
onProgressChanged: (controller, progress) {
setState(() {
this.progress = progress / 100;
urlController.text = url;
});
},
onUpdateVisitedHistory: (controller, url, isReload) {
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
onConsoleMessage: (controller, consoleMessage) {
if (kDebugMode) {
print(consoleMessage);
}
},
),
).build(context),
progress < 1.0
? LinearProgressIndicator(value: progress)
: Container(),
],
),
),
OverflowBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: const Icon(Icons.arrow_back),
onPressed: () {
webViewController?.goBack();
},
),
ElevatedButton(
child: const Icon(Icons.arrow_forward),
onPressed: () {
webViewController?.goForward();
},
),
ElevatedButton(
child: const Icon(Icons.refresh),
onPressed: () {
webViewController?.reload();
},
),
],
),
])));
}
}