基础库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

87 rader
2.7 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:zhiying_comm/zhiying_comm.dart';
  4. import 'package:zhiying_comm/pages/empty_page/empty_page.dart';
  5. class NativeUtil {
  6. // 工厂模式
  7. factory NativeUtil() => _getInstance();
  8. static NativeUtil get instance => _getInstance();
  9. static NativeUtil _instance;
  10. MethodChannel _methodChannel;
  11. NativeUtil._internal() {
  12. // 初始化
  13. _methodChannel = MethodChannel('zhiying_comm://method');
  14. }
  15. static NativeUtil _getInstance() {
  16. if (_instance == null) {
  17. _instance = new NativeUtil._internal();
  18. }
  19. return _instance;
  20. }
  21. static Future<Map<String, dynamic>> _invokeChannel(String method,
  22. {Map<String, dynamic> params}) async {
  23. var res;
  24. try {
  25. res = await NativeUtil.instance._methodChannel.invokeMethod(method, params);
  26. } catch (e) {
  27. res = {'Failed': e.message};
  28. }
  29. print(res);
  30. return Map<String, dynamic>.from(res);
  31. }
  32. // 通过原生webview打开url
  33. static Future openUrl(BuildContext context, String url) {
  34. Map<String, dynamic> params = Map<String, dynamic>();
  35. params['url'] = url ?? '';
  36. params['SkipUIIdentifier'] = 'pub_wailian';
  37. NativeUtil.openPage(context, params);
  38. }
  39. // 公共跳转方法
  40. static Future openPage(BuildContext context, Map<String, dynamic> params) {
  41. return Navigator.of(context)
  42. .push(MaterialPageRoute(builder: (BuildContext context) {
  43. if (!params.containsKey('SkipUIIdentifier')) {
  44. return EmptyPage();
  45. }
  46. return PageFactory.create(params['SkipUIIdentifier'], params);
  47. }));
  48. }
  49. //跳转原生普通页面,非公共跳转
  50. static Future openNativePage(NativeRouter router, {Map params}) {
  51. String method = enumToString(router);
  52. return NativeUtil._invokeChannel('openNativePage', params: {
  53. 'method': method,
  54. 'params': params == null ? {} : Map<String, dynamic>.from(params)
  55. });
  56. }
  57. static Future<Map<String, dynamic>> getSetting() async {
  58. return NativeUtil._invokeChannel('getSetting');
  59. }
  60. // static Future<Map<String, dynamic>> onPagePop(bool isTop) async {
  61. // NativeUtil._invokeChannel('pageDidPop',
  62. // params: new Map<String, dynamic>.from({'isTop': isTop}));
  63. // }
  64. //
  65. // static Future<Map<String, dynamic>> onPagePush(bool isTop) async {
  66. // NativeUtil._invokeChannel('pageDidPush',
  67. // params: new Map<String, dynamic>.from({'isTop': isTop}));
  68. // }
  69. static Future<Map<String, dynamic>> invokeMethod(
  70. String method, Map<String, dynamic> params) {
  71. Map<String, dynamic> request = {"method": method, "params": params};
  72. return NativeUtil._invokeChannel('invokeMethod', params: request);
  73. }
  74. }