webview_page.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:auto_route/annotations.dart';
  2. import 'package:eitc_erm_dental_flutter/exts.dart';
  3. import 'package:eitc_erm_dental_flutter/funcs.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_inappwebview/flutter_inappwebview.dart';
  7. import 'package:retrofit/dio.dart';
  8. import '../../http/api_exception.dart';
  9. ///Webview页面
  10. @RoutePage(name: "webviewRoute")
  11. class WebviewPage extends StatefulWidget {
  12. final String url;
  13. final String? title;
  14. final Future<HttpResponse<String>>? htmlFuture;
  15. const WebviewPage(
  16. {super.key, required this.url, this.title, this.htmlFuture});
  17. @override
  18. State<WebviewPage> createState() => _WebviewPageState();
  19. }
  20. class _WebviewPageState extends State<WebviewPage> {
  21. final GlobalKey _webViewKey = GlobalKey();
  22. InAppWebViewController? _webViewController;
  23. final InAppWebViewSettings _settings = InAppWebViewSettings(
  24. isInspectable: kDebugMode,
  25. mediaPlaybackRequiresUserGesture: false,
  26. allowsInlineMediaPlayback: true,
  27. iframeAllow: "camera",
  28. iframeAllowFullscreen: true,
  29. javaScriptEnabled: true);
  30. @override
  31. void initState() {
  32. super.initState();
  33. }
  34. @override
  35. void dispose() {
  36. super.dispose();
  37. }
  38. void _load() async {
  39. if (widget.htmlFuture != null) {
  40. try {
  41. HttpResponse<String> response = await widget.htmlFuture!;
  42. _webViewController?.loadData(data: response.data);
  43. } catch (e) {
  44. loge("加载webview html异常", error: ApiException.from(e));
  45. }
  46. } else {
  47. _webViewController?.loadUrl(
  48. urlRequest: URLRequest(url: WebUri(widget.url)));
  49. }
  50. }
  51. @override
  52. Widget build(BuildContext context) {
  53. return Scaffold(
  54. appBar: AppBar(
  55. title: widget.title.isNullOrEmpty ? null : Text(widget.title!),
  56. centerTitle: true,
  57. ),
  58. //body: SafeArea(child: WebViewWidget(controller: _controller)),
  59. body: SafeArea(
  60. child: InAppWebView(
  61. key: _webViewKey,
  62. initialSettings: _settings,
  63. onWebViewCreated: (controller) {
  64. _webViewController = controller;
  65. _load();
  66. },
  67. onLoadStart: (controller, url) {
  68. logd("onLoadStart,url=$url");
  69. },
  70. onPermissionRequest: (controller, request) async {
  71. return PermissionResponse(
  72. resources: request.resources,
  73. action: PermissionResponseAction.GRANT);
  74. },
  75. onLoadStop: (controller, url) async {
  76. logd("onLoadStop,url=$url");
  77. },
  78. onReceivedError: (controller, request, error) {
  79. loge("onReceivedError", error: error);
  80. },
  81. onProgressChanged: (controller, progress) {
  82. logd("onProgressChanged,progress=$progress");
  83. },
  84. onConsoleMessage: (controller, consoleMessage) {
  85. if (kDebugMode) {
  86. logd(consoleMessage);
  87. }
  88. },
  89. ),
  90. ),
  91. );
  92. }
  93. }