113 lines
3.4 KiB
Dart
113 lines
3.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'native_channel.dart';
|
|
import 'native_web_view.dart';
|
|
|
|
/// Drives a [WebBrowserView] from Dart.
|
|
class WebBrowserController {
|
|
int? _viewId;
|
|
|
|
/// Whether the controller is attached to a live web view.
|
|
bool get isAttached => _viewId != null;
|
|
|
|
int get _id {
|
|
final id = _viewId;
|
|
if (id == null) {
|
|
throw StateError('WebBrowserController is not attached to a view');
|
|
}
|
|
return id;
|
|
}
|
|
|
|
/// Navigates to [url].
|
|
Future<void> loadUrl(String url) =>
|
|
CesiumNativeChannel.instance.loadUrl(_id, url);
|
|
|
|
/// Reloads the current page.
|
|
Future<void> reload() => CesiumNativeChannel.instance.reload(_id);
|
|
|
|
/// Runs [script] in the current page and returns the result as a string
|
|
/// (null for undefined/null). The last expression must not be a Promise.
|
|
Future<String?> runJavaScript(String script) =>
|
|
CesiumNativeChannel.instance.runJavaScript(_id, script);
|
|
|
|
/// Opens the dev tools (no-op on Android, use chrome://inspect).
|
|
Future<void> openInspector() async {
|
|
final id = _viewId;
|
|
if (id != null) await CesiumNativeChannel.instance.openInspector(id);
|
|
}
|
|
}
|
|
|
|
/// A web page, rendered by the same engine as `CesiumViewer`: WebKitGTK on
|
|
/// Linux, the system WebView on Android, WebView2 on Windows.
|
|
///
|
|
/// Changing [url] navigates the view; links that would open a new window
|
|
/// load in place. The same layering rules as `CesiumViewer` apply on Linux
|
|
/// and Windows (nothing can be drawn over the page).
|
|
class WebBrowserView extends StatefulWidget {
|
|
const WebBrowserView({
|
|
super.key,
|
|
required this.url,
|
|
this.controller,
|
|
this.onError,
|
|
this.visible = true,
|
|
});
|
|
|
|
final String url;
|
|
final WebBrowserController? controller;
|
|
|
|
/// Page load failures and web process crashes.
|
|
final ValueChanged<String>? onError;
|
|
|
|
/// Hides the web view without destroying it (keeps the page).
|
|
final bool visible;
|
|
|
|
@override
|
|
State<WebBrowserView> createState() => _WebBrowserViewState();
|
|
}
|
|
|
|
class _WebBrowserViewState extends State<WebBrowserView> {
|
|
late WebBrowserController _controller =
|
|
widget.controller ?? WebBrowserController();
|
|
String? _createdUrl;
|
|
|
|
@override
|
|
void didUpdateWidget(WebBrowserView oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
final newController = widget.controller ?? _controller;
|
|
if (newController != _controller) {
|
|
newController._viewId = _controller._viewId;
|
|
_controller._viewId = null;
|
|
_controller = newController;
|
|
}
|
|
if (widget.url != oldWidget.url && _controller.isAttached) {
|
|
_controller.loadUrl(widget.url);
|
|
}
|
|
}
|
|
|
|
void _onNativeEvent(String event, String message) {
|
|
if (event == 'onError') widget.onError?.call(message);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return NativeWebViewHost(
|
|
create: (onEvent) {
|
|
_createdUrl = widget.url;
|
|
return CesiumNativeChannel.instance.create(onEvent, url: widget.url);
|
|
},
|
|
onEvent: _onNativeEvent,
|
|
onCreated: (id) {
|
|
_controller._viewId = id;
|
|
// The url may have changed while the view was being created.
|
|
if (!mounted) return;
|
|
if (widget.url != _createdUrl) _controller.loadUrl(widget.url);
|
|
},
|
|
onDisposed: () => _controller._viewId = null,
|
|
visible: widget.visible,
|
|
background: Colors.white,
|
|
unsupportedMessage: 'WebBrowserView supports Linux, Windows and '
|
|
'Android only.',
|
|
);
|
|
}
|
|
}
|