模块
sn-flutter
引入模块:
var flutter = uni.requireNativePlugin('sn-flutter');
1. cachePages
cachePages是一个快速缓存Flutter Engine的方法
flutter.cachePages({pages: ['main'] // flutter应用入口函数,支持多个});
2. cacheEntryPoints
cacheEntryPoints是指定缓存Flutter Engine的方法
flutter.cacheEntryPoints({home: 'main', // main是flutter应用入口函数,home为别名my: 'profile'});
3. openPage
openPage是打开一个Activity或ViewController来加载Flutter
flutter.openPage({instanceId: 'topMain', // instanceId 是用来与flutter通信的标识,必须唯一entryPoint: 'topMain', // id 等价 entryPointdestroyAfterBack: true,params: {a: 1}});
注意:iOS中,由于uniapp的限制,openPage打开的ViewController不能滑动返回,当然Flutter内部路由不影响。
nvue组件
sn-flutter-view
nvue文件中使用
<template><sn-flutter-view ref="flutter" :instanceId="instanceId" :entryPoint="entryPoint" :params="params" destroyAfterBack="true" @pop="onPop" @popChange="popChange" style="flex:1;" /></template><script>import { MethodChannel } from '@/js/flutter/flutter.js';export default {data() {return {canPop: false,instanceId: 'topMain',entryPoint: 'main',params: {a: 1}};},onBackPress() {if (this.canPop) {this.$refs.flutter.pop();return true;}},onLoad() {this.methodChannel = new MethodChannel(this.instanceId);this.methodChannel.$on('test', e => {uni.showToast({title: JSON.stringify(e),icon: 'none'});if (e.callbackId) {this.methodChannel.callback(e.callbackId, { result: 3 });}});},onUnload() {this.methodChannel.$off('test');},methods: {onPop() {uni.navigateBack();},popChange({ detail }) {this.canPop = detail.pop;}}};</script><style></style>
属性
| 属性 | 类型 | 说明 |
|---|---|---|
| entryPoint | string | flutter入口函数名(别名) |
| instanceId | string | 用来与flutter通信的标识,必须唯一 |
| cacheId | string | 对应cacheEntryPoints的健值 |
| destroyAfterBack | boolean | 返回后是否销毁Flutter Engine |
| params | object | 参数 |
事件
| 事件 | 参数 | 说明 |
|---|---|---|
| popChange | { detail: { pop: true } } | Flutter Engine 路由变化,比如pop表示路由是否可以返回 |
| pop | 无 | Flutter通过methodchannel发送的pop事件,把pop操作交给uniapp来实现 |
方法
| 方法 | 参数 | 说明 |
|---|---|---|
| pop | 无 | 调用Flutter Engine 路由pop方法 |
