基础库
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

89 satır
2.8 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:zhiying_comm/pages/empty_page/empty_page.dart';
  4. import 'package:zhiying_comm/zhiying_comm.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.flutter.url';
  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<void> notifyInitSuccess() async {
  61. return NativeUtil._invokeChannel('notifyInitSuccess');
  62. }
  63. // static Future<Map<String, dynamic>> onPagePop(bool isTop) async {
  64. // NativeUtil._invokeChannel('pageDidPop',
  65. // params: new Map<String, dynamic>.from({'isTop': isTop}));
  66. // }
  67. //
  68. // static Future<Map<String, dynamic>> onPagePush(bool isTop) async {
  69. // NativeUtil._invokeChannel('pageDidPush',
  70. // params: new Map<String, dynamic>.from({'isTop': isTop}));
  71. // }
  72. static Future<Map<String, dynamic>> invokeMethod(
  73. String method, Map<String, dynamic> params) {
  74. Map<String, dynamic> request = {"method": method, "params": params};
  75. return NativeUtil._invokeChannel('invokeMethod', params: request);
  76. }
  77. }