0


flutter 封装webview和使用本地网页

最先看到flutter_webview_plugin 用法特别简单

flutter_webview_plugin | Flutter PackagePlugin that allow Flutter to communicate with a native Webview.https://pub-web.flutter-io.cn/packages/flutter_webview_plugin缺点: 没有实现js sdk的功能 没有办法 使用JavaScriptChannel 的功能

后面使用webview_flutter

webview_flutter | Flutter packageA Flutter plugin that provides a WebView widget on Android and iOS.https://pub-web.flutter-io.cn/packages/webview_flutter组件

  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:webview_flutter/webview_flutter.dart';
  7. class AppWebView extends StatefulWidget {
  8. final String url;
  9. final Function(dynamic)? onMessageReceived;
  10. const AppWebView({
  11. super.key,
  12. required this.url,
  13. this.onMessageReceived,
  14. });
  15. @override
  16. State<AppWebView> createState() => _AppWebViewState();
  17. }
  18. class _AppWebViewState extends State<AppWebView> {
  19. late final WebViewController controller;
  20. int progress = 0;
  21. @override
  22. void initState() {
  23. super.initState();
  24. controller = WebViewController()
  25. ..setJavaScriptMode(JavaScriptMode.unrestricted)
  26. ..addJavaScriptChannel("lh", onMessageReceived: onMessageReceived)
  27. ..enableZoom(true)
  28. ..setBackgroundColor(const Color(0x00000000))
  29. ..setNavigationDelegate(
  30. NavigationDelegate(
  31. onProgress: (int progress) {
  32. // Update loading bar.
  33. this.progress = progress;
  34. setState(() {});
  35. },
  36. onPageStarted: (String url) {},
  37. onPageFinished: (String url) {},
  38. onWebResourceError: (WebResourceError error) {},
  39. onNavigationRequest: (NavigationRequest request) {
  40. if (request.url.startsWith('https://www.youtube.com/')) {
  41. return NavigationDecision.prevent;
  42. }
  43. return NavigationDecision.navigate;
  44. },
  45. ),
  46. )
  47. ..loadRequest(Uri.parse(widget.url));
  48. }
  49. // 接受h5发送来的数据
  50. onMessageReceived(message) async {
  51. widget.onMessageReceived?.call(message);
  52. //接收H5发过来的数据
  53. String sendMesStr = message.message;
  54. print("onMessageReceived sendMesStr:${sendMesStr}");
  55. Map<String, dynamic> msg = json.decode(sendMesStr);
  56. String method = msg["method"] ?? "";
  57. // Map<String, dynamic> data = msg["data"] ?? {};
  58. if (method.isNotEmpty) {
  59. switch (method) {
  60. case "back":
  61. controller.goBack();
  62. break;
  63. }
  64. }
  65. }
  66. @override
  67. Widget build(BuildContext context) {
  68. if (!kIsWeb && (Platform.isIOS || Platform.isAndroid)) {
  69. return Scaffold(
  70. // appBar: AppBar(title: const Text('Flutter Simple Example')),
  71. body: Stack(children: [
  72. WebViewWidget(controller: controller),
  73. if (progress != 100)
  74. const Center(
  75. child: CupertinoActivityIndicator(),
  76. )
  77. ]),
  78. );
  79. } else {
  80. return const Center(
  81. child: Text('WebView control is not supported on this platform yet.'),
  82. );
  83. }
  84. }
  85. }

使用

  1. Get.to(() => AppWebView(
  2. url: 'http://localhost',
  3. onMessageReceived: onMessageReceived,
  4. ));

注意:需要重启项目才会生效

加载本地网页

String html =

  1. await rootBundle.loadString('assets/index.html');

String localHtmlFilePath = Uri.dataFromString(

html,

mimeType: 'text/html',

encoding: Encoding.getByName('utf-8'),

).toString();

pubspec.yaml

assets:

  1. - assets/index.html

使用

AppWebView(

url: localHtmlFilePath,

onMessageReceived: onMessageReceived,

)

标签: flutter

本文转载自: https://blog.csdn.net/weixin_43575775/article/details/136341605
版权归原作者 肥肥呀呀呀 所有, 如有侵权,请联系我们删除。

“flutter 封装webview和使用本地网页”的评论:

还没有评论