组件导航 (Navigation)+flutter项目搭建-混合开发+分栏

组件导航 (Navigation)+flutter项目搭建

接上一章flutter项目的环境变量配置并运行flutter

上一章面熟了搭建flutter并用编辑器运行了ohos项目,这章主要是对项目的工程化改造

先创建flutter项目,再配置Navigation

1.在开发视图的resources/base/profile下面定义配置文件,文件名可以自定义,例如:router_map.json。

复制代码
{
  "routerMap": [
    {
      "name": "PageFlutter",
      "pageSourceFile": "src/main/ets/pages/home/PageFlutter.ets",
      "buildFunction": "PageFlutterBuilder",
      "data": {
        "description": "this is PageOne"
      }
    },
    {
      "name": "PageTwo",
      "pageSourceFile": "src/main/ets/pages/home/PageTwo.ets",
      "buildFunction": "PageTwoBuilder",
      "data": {
        "description": "this is PageTwo"
      }
    },
    {
      "name": "PageThree",
      "pageSourceFile": "src/main/ets/pages/home/PageThree.ets",
      "buildFunction": "PageThreeBuilder",
      "data": {
        "description": "this is PageThree"
      }
    }
  ]
}

2.在module.json5配置的module下面配置

复制代码
{
  "module": {
	  ...
	  "routerMap": "$profile:router_map",
	  }
}

项目文件如下

在入口 "pages/Index"文件配置

需要注意的是flutter只会一次,把栈移到顶部,不能一直push

Index.ets

复制代码
import { PageFlutterBuilder } from "./home/PageFlutter";
import { PageThreeBuilder } from "./home/PageThree";
import { PageTwoBuilder } from "./home/PageTwo";

let storage = LocalStorage.getShared()

@Entry(storage)
@Component
struct Index {
  @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()
  private arr: string[] = ['PageFlutter', 'PageTwo', 'PageThree'];

  build() {
    Column() {
      Navigation(this.pageInfos) {
        List({ space: 12 }) {
          ForEach(this.arr, (item: string) => {
            ListItem() {
              Text("Page" + item)
                .width("100%")
                .height(72)
                .backgroundColor('#FFFFFF')
                .borderRadius(24)
                .fontSize(16)
                .fontWeight(500)
                .textAlign(TextAlign.Center)
                .onClick(() => {
                  if(item==='PageFlutter'&&this.pageInfos.getAllPathName().includes('PageFlutter')){
                    // flutter只会一次,把栈移到顶部
                    this.pageInfos.moveToTop(item)
                  }else {
                    this.pageInfos.pushPath({ name: item});
                  }

                })
            }
          }, (item: number) => item.toString())
        }
        .width("90%")
        .margin({ top: 12 })
      }
      .mode(NavigationMode.Auto)
      .hideTitleBar(true)
    }
    .height('100%')
    .width('100%')
    .backgroundColor('#F1F3F5')
  }
}

PageFlutter.ets

复制代码
import { FlutterPage } from "@ohos/flutter_ohos";
import { common } from "@kit.AbilityKit";

// 跳转页面入口函数
@Builder
export function PageFlutterBuilder() {
  PageFlutter();
}
const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS'


@Component
struct PageFlutter {
  pathStack: NavPathStack = new NavPathStack();
  @LocalStorageLink('viewId') viewId: string = "";
  private context = getContext(this) as common.UIAbilityContext

  build() {
    NavDestination() {
      Text('hellppp')
      if(this.viewId){
        FlutterPage({ viewId: this.viewId })
      }
    }
    .onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack;
    })
    .onBackPressed(() => {
      this.context.eventHub.emit(EVENT_BACK_PRESS)
      return true;
    })
    .hideTitleBar(true)
  }
}

其他PageThree.ets,PageTwo.ets随便写

效果如下

相关推荐
白鹿第一帅13 小时前
【参赛心得】鸿蒙三方库适配实战:从 Hadoop 生态到鸿蒙生态,企业级项目集成的 6 个最佳实践
harmonyos·白鹿第一帅·鸿蒙三方库·csdn成都站·鸿蒙开放能力·鸿蒙学习之路·harmonyos创新赛
ITHAOGE1515 小时前
下载| Windows 11 ARM版10月官方ISO系统映像 (适合部分笔记本、苹果M系列芯片电脑、树莓派和部分安卓手机平板)
windows·科技·microsoft·电脑
纯爱掌门人15 小时前
鸿蒙端云一体化云存储实战:手把手教你玩转文件上传下载
前端·harmonyos
火柴就是我16 小时前
android:enableJetifier=true 再学习
android·flutter
用户4988881743716 小时前
ArkTS 语言基础 第五节:流程控制
harmonyos
小草cys16 小时前
华为910B服务器(搭载昇腾Ascend 910B AI 芯片的AI服务器查看服务器终端信息
服务器·人工智能·华为·昇腾·910b
gaosushexiangji16 小时前
产品推荐|千眼狼全新第二代背照式制冷sCMOS相机上市,再次刷新灵敏度和噪声极限
数码相机
中达瑞和-高光谱·多光谱16 小时前
关于汽油管道泄漏高光谱图像检测的研究
数码相机
mit6.82416 小时前
[无人机sdk] CameraModule | GimbalModule
数码相机·无人机
星释17 小时前
鸿蒙Flutter三方库适配指南-04.使用MacOS搭建开发环境
flutter·macos·harmonyos