Browse Source

[Modify]更换webview实现

zhipeng 11 months ago
parent
commit
2e16acf3cf

+ 2 - 0
assets/string/base/string.xml

@@ -131,6 +131,7 @@
     <string name="copy_phone_num_success">手机号复制成功</string>
     <string name="friend_update_remark_title">修改备注</string>
     <string name="friend_update_remark_hint">请输入备注</string>
+    <string name="remark_no_change">备注未修改</string>
     <string name="remark_update_success">备注修改成功</string>
     <string name="friend_add_now">立即添加</string>
 
@@ -221,6 +222,7 @@
     <string name="urgent_contact_delete_success">删除成功</string>
     <string name="urgent_contact_send_all_help">确认向您所有好友发送短信求助?</string>
     <string name="urgent_contact_emergency_help">紧急求助</string>
+    <string name="urgent_contact_add_self">不能添加自己为紧急联系人</string>
     <string name="contact_no_default">未设置默认紧急联系人</string>
     <string name="urgent_contact_help_send_success">已成功发送求助信息</string>
     <string name="urgent_contact_add_max_tip">最多添加5人,请移除后再添加</string>

+ 7 - 6
lib/main.dart

@@ -7,7 +7,6 @@ import 'package:flutter_localizations/flutter_localizations.dart';
 import 'package:flutter_screenutil/flutter_screenutil.dart';
 import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
 import 'package:get/get.dart';
-import 'package:get/get_navigation/src/root/get_material_app.dart';
 import 'package:location/resource/colors.gen.dart';
 import 'package:location/resource/string.gen.dart';
 import 'package:location/resource/string_source.dart';
@@ -23,11 +22,12 @@ import 'package:location/utils/mmkv_util.dart';
 import 'package:location/utils/privacy_compliance.dart';
 import 'package:location/utils/toast_util.dart';
 import 'package:pull_to_refresh/pull_to_refresh.dart';
-import 'data/consts/channel_util.dart';
-import 'di/get_it.dart';
+
 import 'data/consts/build_config.dart';
+import 'data/consts/channel_util.dart';
 import 'data/consts/constants.dart';
 import 'device/device_info_util.dart';
+import 'di/get_it.dart';
 
 void main() {
   FlutterBugly.postCatchedException(() async {
@@ -51,7 +51,8 @@ void main() {
 
 Future<void> initRequired() async {
   //Log
-  AtmobLog.setLogLevel(BuildConfig.isDebug ? ALog.LogLevel.verbose : ALog.LogLevel.none);
+  AtmobLog.setLogLevel(
+      BuildConfig.isDebug ? ALog.LogLevel.verbose : ALog.LogLevel.none);
 
   //存储
   await KVUtil.init();
@@ -78,12 +79,12 @@ class AppInitTask implements EnsurePolicyGrant {
     await deviceInfoUtil.init();
     //bugly
     BuglyHelper.init();
-    //地图sdk
-    MapHelper.init();
     //引力引擎
     GravityHelper.init();
     //友盟
     UmengHelper.initCommon();
+    //地图sdk
+    await MapHelper.init();
   }
 }
 

+ 4 - 55
lib/module/browser/browser_controller.dart

@@ -1,10 +1,8 @@
+import 'package:flutter_inappwebview/flutter_inappwebview.dart';
 import 'package:get/get.dart';
 import 'package:injectable/injectable.dart';
-import 'package:location/utils/atmob_log.dart';
-import 'package:webview_flutter/webview_flutter.dart';
 
 import '../../base/base_controller.dart';
-import '../../resource/colors.gen.dart';
 
 @injectable
 class BrowserController extends BaseController {
@@ -12,64 +10,15 @@ class BrowserController extends BaseController {
 
   String url = (Get.arguments is String) ? (Get.arguments as String) : '';
 
-  final WebViewController webViewController = WebViewController();
+  InAppWebViewController? webViewController;
 
   final title = ''.obs;
 
-  @override
-  void onInit() {
-    super.onInit();
-    _initWebSetting();
-  }
-
-  @override
-  void onReady() {
-    super.onReady();
-    _loadUrl();
-  }
-
-  void _initWebSetting() {
-    webViewController.setJavaScriptMode(JavaScriptMode.unrestricted);
-    webViewController.setBackgroundColor(ColorName.white);
-    webViewController.setNavigationDelegate(
-      NavigationDelegate(
-        onNavigationRequest: (NavigationRequest request) {
-          AtmobLog.d(tag, "onNavigationRequest: ${request.url}");
-          if (request.url.startsWith('http') || request.url.startsWith('https')) {
-            return NavigationDecision.navigate;
-          }
-          return NavigationDecision.prevent;
-        },
-        onUrlChange: (UrlChange urlChange) {
-          _getTitle();
-        },
-        onPageFinished: (String url) {
-          _getTitle();
-        },
-      ),
-    );
-  }
-
-  void _loadUrl() {
-    if (url.isEmpty) {
-      return;
-    }
-    webViewController.loadRequest(Uri.parse(url));
-  }
-
   Future<bool> handleBack() async {
-    if (await webViewController.canGoBack()) {
-      webViewController.goBack();
+    if (await webViewController?.canGoBack() ?? false) {
+      webViewController?.goBack();
       return false;
     }
     return true;
   }
-
-  void _getTitle() async {
-    webViewController.getTitle().then((title) {
-      if (title != null) {
-        this.title.value = title;
-      }
-    });
-  }
 }

+ 8 - 4
lib/module/browser/browser_view.dart

@@ -1,10 +1,8 @@
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
-import 'package:flutter/src/widgets/framework.dart';
+import 'package:flutter_inappwebview/flutter_inappwebview.dart';
 import 'package:flutter_screenutil/flutter_screenutil.dart';
 import 'package:get/get.dart';
-import 'package:get/get_core/src/get_main.dart';
-import 'package:webview_flutter/webview_flutter.dart';
 
 import '../../base/base_page.dart';
 import '../../resource/assets.gen.dart';
@@ -50,6 +48,12 @@ class BrowserPage extends BasePage<BrowserController> {
   }
 
   Widget _buildContentView() {
-    return WebViewWidget(controller: controller.webViewController);
+    return InAppWebView(
+      initialUrlRequest: URLRequest(url: WebUri(controller.url)),
+      onWebViewCreated: (webController) =>
+          controller.webViewController = webController,
+      onTitleChanged: (webController, title) =>
+          controller.title.value = title ?? '',
+    );
   }
 }

+ 4 - 0
lib/module/friend/setting/friend_setting_controller.dart

@@ -38,6 +38,10 @@ class FriendSettingController extends BaseController {
     FriendUpdateRemarkDialog.show(
         remark: userInfo?.remark,
         callback: (remark) {
+          if (remark == userInfo?.remark) {
+            ToastUtil.show(StringName.remarkNoChange);
+            return;
+          }
           _updateRemark(remark);
         });
   }

+ 14 - 5
lib/module/main/main_controller.dart

@@ -21,7 +21,6 @@ import 'package:location/module/news/news_page.dart';
 import 'package:location/module/urgent_contact/urgent_contact_page.dart';
 import 'package:location/resource/string.gen.dart';
 import 'package:location/sdk/map/map_helper.dart';
-import 'package:location/utils/base_expand.dart';
 import 'package:location/utils/mmkv_util.dart';
 import 'package:location/utils/toast_util.dart';
 
@@ -226,8 +225,8 @@ class MainController extends BaseController {
 
   void _requestLocationPermission() async {
     bool isGranted = await PermissionUtil.requestLocationPermission();
-    _showLocationAlways();
     if (isGranted) {
+      _showLocationAlways();
       _updateCurrentLocation();
     }
   }
@@ -245,9 +244,19 @@ class MainController extends BaseController {
   }
 
   void _updateCurrentLocation() {
-    MapHelper.getLastLocation()?.let((it) {
-      animateCamera(latitude: it.latitude, longitude: it.longitude);
-    });
+    var lastLocation = MapHelper.getLastLocation();
+    if (lastLocation == null) {
+      locationListener(MapLocation location) {
+        MapHelper.removeLocationListener(locationListener);
+        animateCamera(
+            latitude: location.latitude, longitude: location.longitude);
+      }
+
+      MapHelper.addLocationListener(locationListener);
+    } else {
+      animateCamera(
+          latitude: lastLocation.latitude, longitude: lastLocation.longitude);
+    }
   }
 
   onFriendClick() {

+ 4 - 4
lib/module/splash/splash_controller.dart

@@ -23,8 +23,8 @@ class SplashController extends BaseController {
     } else {
       AgreementDialog.show(cancelClick: () {
         exitApp();
-      }, sureClick: () {
-        _agreePrivacy();
+      }, sureClick: () async {
+        await _agreePrivacy();
         isAgreePrivacyNextStep(0);
       });
     }
@@ -48,8 +48,8 @@ class SplashController extends BaseController {
     }
   }
 
-  void _agreePrivacy() async {
+  _agreePrivacy() async {
     await UmengHelper.setPolicyGrantResult(true);
-    PrivacyCompliance.setPrivacyPolicy(true);
+    await PrivacyCompliance.setPrivacyPolicy(true);
   }
 }

+ 4 - 0
lib/module/urgent_contact/add_contact/add_urgent_contact_controller.dart

@@ -82,6 +82,10 @@ class AddUrgentContactController extends BaseController {
     if (contactPhone.length < 11) {
       return;
     }
+    if(contactPhone == accountRepository.loginPhoneNum.value) {
+      ToastUtil.show(StringName.urgentContactAddSelf);
+      return;
+    }
     _debounce.onClick(() {
       if (accountRepository.memberIsExpired()) {
         MemberPage.start();

+ 4 - 0
lib/resource/string.gen.dart

@@ -111,6 +111,7 @@ class StringName {
   static final String copyPhoneNumSuccess = 'copy_phone_num_success'.tr; // 手机号复制成功
   static final String friendUpdateRemarkTitle = 'friend_update_remark_title'.tr; // 修改备注
   static final String friendUpdateRemarkHint = 'friend_update_remark_hint'.tr; // 请输入备注
+  static final String remarkNoChange = 'remark_no_change'.tr; // 备注未修改
   static final String remarkUpdateSuccess = 'remark_update_success'.tr; // 备注修改成功
   static final String friendAddNow = 'friend_add_now'.tr; // 立即添加
   static final String goRequestContactsPermission = 'go_request_contacts_permission'.tr; // 去申请
@@ -181,6 +182,7 @@ class StringName {
   static final String urgentContactDeleteSuccess = 'urgent_contact_delete_success'.tr; // 删除成功
   static final String urgentContactSendAllHelp = 'urgent_contact_send_all_help'.tr; // 确认向您所有好友发送短信求助?
   static final String urgentContactEmergencyHelp = 'urgent_contact_emergency_help'.tr; // 紧急求助
+  static final String urgentContactAddSelf = 'urgent_contact_add_self'.tr; // 不能添加自己为紧急联系人
   static final String contactNoDefault = 'contact_no_default'.tr; // 未设置默认紧急联系人
   static final String urgentContactHelpSendSuccess = 'urgent_contact_help_send_success'.tr; // 已成功发送求助信息
   static final String urgentContactAddMaxTip = 'urgent_contact_add_max_tip'.tr; // 最多添加5人,请移除后再添加
@@ -355,6 +357,7 @@ class StringMultiSource {
       'copy_phone_num_success': '手机号复制成功',
       'friend_update_remark_title': '修改备注',
       'friend_update_remark_hint': '请输入备注',
+      'remark_no_change': '备注未修改',
       'remark_update_success': '备注修改成功',
       'friend_add_now': '立即添加',
       'go_request_contacts_permission': '去申请',
@@ -425,6 +428,7 @@ class StringMultiSource {
       'urgent_contact_delete_success': '删除成功',
       'urgent_contact_send_all_help': '确认向您所有好友发送短信求助?',
       'urgent_contact_emergency_help': '紧急求助',
+      'urgent_contact_add_self': '不能添加自己为紧急联系人',
       'contact_no_default': '未设置默认紧急联系人',
       'urgent_contact_help_send_success': '已成功发送求助信息',
       'urgent_contact_add_max_tip': '最多添加5人,请移除后再添加',

+ 29 - 5
plugins/map/lib/src/widget/map_widget.dart

@@ -2,6 +2,8 @@ import 'dart:convert';
 
 import 'package:flutter/cupertino.dart';
 import 'package:flutter/foundation.dart';
+import 'package:flutter/gestures.dart';
+import 'package:flutter/rendering.dart';
 import 'package:flutter/services.dart';
 import 'package:flutter_map/src/consts/map_constants.dart';
 import 'package:flutter_map/src/consts/marker_type.dart';
@@ -30,11 +32,16 @@ class _MapWidgetState extends State<MapWidget> {
     debugPrint(
         'MapWidget...build...mapViewType==>$mapViewType,defaultTargetPlatform==>$defaultTargetPlatform');
     if (defaultTargetPlatform == TargetPlatform.android) {
-      return AndroidView(
-        viewType: mapViewType,
-        onPlatformViewCreated: onPlatformViewCreated,
-        creationParamsCodec: const StandardMessageCodec(),
-      );
+      return PlatformViewLink(
+          surfaceFactory: (context, controller) {
+            return AndroidViewSurface(
+              controller: controller as AndroidViewController,
+              gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
+              hitTestBehavior: PlatformViewHitTestBehavior.opaque,
+            );
+          },
+          onCreatePlatformView: onCreatePlatformView,
+          viewType: mapViewType);
     } else if (defaultTargetPlatform == TargetPlatform.iOS) {
       return UiKitView(
         viewType: mapViewType,
@@ -45,6 +52,23 @@ class _MapWidgetState extends State<MapWidget> {
     return Text('当前平台:$defaultTargetPlatform, 不支持使用地图插件');
   }
 
+  PlatformViewController onCreatePlatformView(
+      PlatformViewCreationParams params) {
+    MethodChannel mapChannel =
+        MethodChannel('${MapConstants.mapViewChannelName}${params.id}');
+    widget.controller?.setChannel(mapChannel);
+    mapChannel.setMethodCallHandler(_mapMethodCallHandler);
+    return PlatformViewsService.initSurfaceAndroidView(
+      id: params.id,
+      viewType: mapViewType,
+      layoutDirection: TextDirection.ltr,
+      creationParams: <String, dynamic>{},
+      creationParamsCodec: const StandardMessageCodec(),
+    )
+      ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
+      ..create();
+  }
+
   Future<void> onPlatformViewCreated(int viewId) async {
     MethodChannel mapChannel =
         MethodChannel('${MapConstants.mapViewChannelName}$viewId');

+ 2 - 1
plugins/map_amap_android/android/src/main/java/com/atmob/map_amap_android/amap/AmapView.java

@@ -67,7 +67,8 @@ public class AmapView implements PlatformView, DefaultLifecycleObserver, MethodC
                 @Override
                 public void onViewAttachedToWindow(@NonNull View v) {
                     LogUtil.i(TAG, "onViewAttachedToWindow() ==> viewId: " + viewId + ", disposed: " + disposed);
-                    v.invalidate();
+                    mapView.postInvalidate();
+                    mapView.getParent().requestLayout();
                 }
 
                 @Override

+ 64 - 0
pubspec.lock

@@ -378,6 +378,70 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "5.9.0"
+  flutter_inappwebview:
+    dependency: "direct main"
+    description:
+      name: flutter_inappwebview
+      sha256: "80092d13d3e29b6227e25b67973c67c7210bd5e35c4b747ca908e31eb71a46d5"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "6.1.5"
+  flutter_inappwebview_android:
+    dependency: transitive
+    description:
+      name: flutter_inappwebview_android
+      sha256: "62557c15a5c2db5d195cb3892aab74fcaec266d7b86d59a6f0027abd672cddba"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "1.1.3"
+  flutter_inappwebview_internal_annotations:
+    dependency: transitive
+    description:
+      name: flutter_inappwebview_internal_annotations
+      sha256: "787171d43f8af67864740b6f04166c13190aa74a1468a1f1f1e9ee5b90c359cd"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "1.2.0"
+  flutter_inappwebview_ios:
+    dependency: transitive
+    description:
+      name: flutter_inappwebview_ios
+      sha256: "5818cf9b26cf0cbb0f62ff50772217d41ea8d3d9cc00279c45f8aabaa1b4025d"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "1.1.2"
+  flutter_inappwebview_macos:
+    dependency: transitive
+    description:
+      name: flutter_inappwebview_macos
+      sha256: c1fbb86af1a3738e3541364d7d1866315ffb0468a1a77e34198c9be571287da1
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "1.1.2"
+  flutter_inappwebview_platform_interface:
+    dependency: transitive
+    description:
+      name: flutter_inappwebview_platform_interface
+      sha256: cf5323e194096b6ede7a1ca808c3e0a078e4b33cc3f6338977d75b4024ba2500
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "1.3.0+1"
+  flutter_inappwebview_web:
+    dependency: transitive
+    description:
+      name: flutter_inappwebview_web
+      sha256: "55f89c83b0a0d3b7893306b3bb545ba4770a4df018204917148ebb42dc14a598"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "1.1.2"
+  flutter_inappwebview_windows:
+    dependency: transitive
+    description:
+      name: flutter_inappwebview_windows
+      sha256: "8b4d3a46078a2cdc636c4a3d10d10f2a16882f6be607962dbfff8874d1642055"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "0.6.0"
   flutter_lints:
     dependency: "direct dev"
     description:

+ 2 - 2
pubspec.yaml

@@ -75,8 +75,8 @@ dependencies:
   #日期格式化等
   intl: 0.19.0
 
-  #网页跳转
-  webview_flutter: 4.10.0
+  #webview
+  flutter_inappwebview: ^6.1.5
 
   #跑马灯
   marquee: 2.3.0