模块

sn-flutter

引入模块:

  1. var flutter = uni.requireNativePlugin('sn-flutter');

1. cachePages

cachePages是一个快速缓存Flutter Engine的方法

  1. flutter.cachePages({
  2. pages: ['main'] // flutter应用入口函数,支持多个
  3. });

2. cacheEntryPoints

cacheEntryPoints是指定缓存Flutter Engine的方法

  1. flutter.cacheEntryPoints({
  2. home: 'main', // main是flutter应用入口函数,home为别名
  3. my: 'profile'
  4. });

3. openPage

openPage是打开一个Activity或ViewController来加载Flutter

  1. flutter.openPage({
  2. instanceId: 'topMain', // instanceId 是用来与flutter通信的标识,必须唯一
  3. entryPoint: 'topMain', // id 等价 entryPoint
  4. destroyAfterBack: true,
  5. params: {
  6. a: 1
  7. }
  8. });

注意:iOS中,由于uniapp的限制,openPage打开的ViewController不能滑动返回,当然Flutter内部路由不影响。

nvue组件

sn-flutter-view

nvue文件中使用

  1. <template>
  2. <sn-flutter-view ref="flutter" :instanceId="instanceId" :entryPoint="entryPoint" :params="params" destroyAfterBack="true" @pop="onPop" @popChange="popChange" style="flex:1;" />
  3. </template>
  4. <script>
  5. import { MethodChannel } from '@/js/flutter/flutter.js';
  6. export default {
  7. data() {
  8. return {
  9. canPop: false,
  10. instanceId: 'topMain',
  11. entryPoint: 'main',
  12. params: {
  13. a: 1
  14. }
  15. };
  16. },
  17. onBackPress() {
  18. if (this.canPop) {
  19. this.$refs.flutter.pop();
  20. return true;
  21. }
  22. },
  23. onLoad() {
  24. this.methodChannel = new MethodChannel(this.instanceId);
  25. this.methodChannel.$on('test', e => {
  26. uni.showToast({
  27. title: JSON.stringify(e),
  28. icon: 'none'
  29. });
  30. if (e.callbackId) {
  31. this.methodChannel.callback(e.callbackId, { result: 3 });
  32. }
  33. });
  34. },
  35. onUnload() {
  36. this.methodChannel.$off('test');
  37. },
  38. methods: {
  39. onPop() {
  40. uni.navigateBack();
  41. },
  42. popChange({ detail }) {
  43. this.canPop = detail.pop;
  44. }
  45. }
  46. };
  47. </script>
  48. <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方法