webview_page.dart 3.6 KB

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