iosWebViewFix/example/lib/headless_in_app_webview.scr...

116 lines
3.4 KiB
Dart
Raw Normal View History

2022-04-25 21:21:26 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'main.dart';
class HeadlessInAppWebViewExampleScreen extends StatefulWidget {
@override
_HeadlessInAppWebViewExampleScreenState createState() =>
new _HeadlessInAppWebViewExampleScreenState();
}
2021-03-01 19:26:57 +00:00
class _HeadlessInAppWebViewExampleScreenState
extends State<HeadlessInAppWebViewExampleScreen> {
HeadlessInAppWebView? headlessWebView;
String url = "";
@override
void initState() {
super.initState();
2022-04-25 21:21:26 +00:00
var url = !kIsWeb
? Uri.parse("https://flutter.dev")
: Uri.parse("http://localhost:${Uri.base.port}/page.html");
headlessWebView = new HeadlessInAppWebView(
2022-04-25 21:21:26 +00:00
initialUrlRequest: URLRequest(url: url),
2022-04-19 23:31:14 +00:00
initialSettings: InAppWebViewSettings(),
onWebViewCreated: (controller) {
print('HeadlessInAppWebView created!');
},
onConsoleMessage: (controller, consoleMessage) {
print("CONSOLE MESSAGE: " + consoleMessage.message);
},
onLoadStart: (controller, url) async {
setState(() {
this.url = url.toString();
});
},
onLoadStop: (controller, url) async {
setState(() {
this.url = url.toString();
});
},
2022-10-11 08:10:13 +00:00
onUpdateVisitedHistory: (controller, url, isReload) {
setState(() {
this.url = url.toString();
});
},
);
}
@override
void dispose() {
super.dispose();
headlessWebView?.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"HeadlessInAppWebView",
)),
drawer: myDrawer(context: context),
body: SafeArea(
2021-03-01 19:26:57 +00:00
child: Column(children: <Widget>[
Container(
padding: EdgeInsets.all(20.0),
child: Text(
"CURRENT URL\n${(url.length > 50) ? url.substring(0, 50) + "..." : url}"),
),
Center(
child: ElevatedButton(
2021-03-01 19:26:57 +00:00
onPressed: () async {
await headlessWebView?.dispose();
await headlessWebView?.run();
},
child: Text("Run HeadlessInAppWebView")),
),
2022-04-25 21:21:26 +00:00
Container(
height: 10,
),
Center(
child: ElevatedButton(
2021-03-01 19:26:57 +00:00
onPressed: () async {
2022-05-08 23:51:21 +00:00
if (headlessWebView?.isRunning() ?? false) {
2021-03-01 19:26:57 +00:00
await headlessWebView?.webViewController.evaluateJavascript(
source: """console.log('Here is the message!');""");
2022-05-08 23:51:21 +00:00
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'HeadlessInAppWebView is not running. Click on "Run HeadlessInAppWebView"!'),
));
2021-03-01 19:26:57 +00:00
}
},
child: Text("Send console.log message")),
),
2022-04-25 21:21:26 +00:00
Container(
height: 10,
),
Center(
child: ElevatedButton(
2021-03-01 19:26:57 +00:00
onPressed: () {
headlessWebView?.dispose();
setState(() {
this.url = "";
});
2021-03-01 19:26:57 +00:00
},
child: Text("Dispose HeadlessInAppWebView")),
)
2021-03-01 19:26:57 +00:00
])));
}
}