/* * 与原生app进行交互 * @Author: fnuoos * @Date: 2020-05-15 11:53:06 * @Last Modified by: mikey.zhaopeng * @Last Modified time: 2020-05-15 11:53:58 */ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; class NativeUtil { // 工厂模式 factory NativeUtil() => _getInstance(); static NativeUtil get instance => _getInstance(); static NativeUtil _instance; MethodChannel _methodChannel; NativeUtil._internal() { // 初始化 _methodChannel = MethodChannel('hairuyi_flutter_comm://method'); } static NativeUtil _getInstance() { if (_instance == null) { _instance = new NativeUtil._internal(); } return _instance; } static Future> _invokeChannel(String method, {Map params}) async { var res; try { res = await NativeUtil.instance._methodChannel.invokeMethod(method, params); } catch (e) { res = {'Failed': e.message}; } print(res); return Map.from(res); } // 通过原生webview打开url static Future openUrl(BuildContext context, String url) { Map params = Map(); params['url'] = url ?? ''; params['SkipUIIdentifier'] = 'pub_wailian'; NativeUtil.openPage(context, params); } // 公共跳转方法 static Future openPage(BuildContext context, Map params) { // if (params.containsKey('is_to_flutter') && // params['is_to_flutter'].toString() == '1') { // BasePageModel model = BasePageModel.fromJson(params); // return Navigator.of(context).push(MaterialPageRoute( // builder: (BuildContext context) => // PageFactory.create(model.SkipUIIdentifier, model))); // } return NativeUtil._invokeChannel('openPage', params: new Map.from(params)); } //跳转原生普通页面,非公共跳转 static Future openNativePage(NativeRouter router, {Map params}) { String method = enumToString(router); return NativeUtil._invokeChannel('openNativePage', params: { 'method': method, 'params': params == null ? {} : Map.from(params) }); } // 跳转原生商品详情 static openGoodsDetail(Map params) async { NativeUtil._invokeChannel('openGoodsDetail', params: params); } static Future getToken() async { Map data = await NativeUtil._invokeChannel('getToken'); if (data.containsKey('token')) { return data['token']; } return ''; } static Future> getSetting() async { return NativeUtil._invokeChannel('getSetting'); } // 打开支付宝支付 static Future payByAlipay(String payCode) async { Map params = {"code": payCode}; Map result = await NativeUtil._invokeChannel('openAliPay', params: new Map.from(params)); if (result.containsKey('msg')) { Fluttertoast.showToast( msg: result['msg'], toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, ); } if (result.containsKey('success') && result['success'] == '1') { return true; } return false; } // 打开微信支付 static Future payByWechat(Map params) async { Map result = await NativeUtil._invokeChannel( 'openWechatPay', params: new Map.from(params)); if (result.containsKey('msg')) { Fluttertoast.showToast( msg: result['msg'], toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, ); } if (result.containsKey('success') && result['success'] == '1') { return true; } return false; } static Future> onPagePop(bool isTop) async { NativeUtil._invokeChannel('pageDidPop', params: new Map.from({'isTop': isTop})); } static Future> onPagePush(bool isTop) async { NativeUtil._invokeChannel('pageDidPush', params: new Map.from({'isTop': isTop})); } static Future> invokeMethod( String method, Map params) { Map request = {"method": method, "params": params}; return NativeUtil._invokeChannel('invokeMethod', params: request); } }