组件导航 (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随便写

效果如下

相关推荐
老高学长1 分钟前
企业如何对局域网电脑进行监控?五个局域网电脑实时监控的方法分享,全方位监控电脑
网络·安全·电脑
狼哥16861 分钟前
学习卡片案例新特性接入
ui·华为·harmonyos
Davina_yu2 分钟前
JSON数据处理:字符串序列化与反序列化实战(20)
harmonyos·鸿蒙·鸿蒙系统
小鹏linux10 分钟前
鸿蒙PC迁移:Tesseract OCR C++ 三方库鸿蒙适配全记录
c++·ocr·harmonyos
JOJO数据科学11 分钟前
DbGate Electron 鸿蒙 PC 适配全记录:从桌面数据库工具到 OpenHarmony HAP
数据库·electron·harmonyos
JOJO数据科学16 分钟前
鸿蒙PC迁移:KTouch Qt/QML 打字训练器适配全记录
qt·华为·harmonyos
herinspace16 分钟前
管家婆云辉煌开单优化
服务器·数据库·电脑·管家婆软件·财务软件
User_芊芊君子18 分钟前
鸿蒙PC适配:Pinta GTK 图像编辑器鸿蒙 PC ArkWeb 适配全记录:从 .NET_GTK4 桌面到 HarmonyOS PC HAP
编辑器·.net·harmonyos
轻口味19 分钟前
轻规划鸿蒙开发实战10:分布式数据同步深度博弈,UserId 隔离与并发数据冲突消解机
分布式·华为·harmonyos·鸿蒙
金启攻25 分钟前
鸿蒙原生应用开发实战(五):地图可视化与性能优化——钓点地图与构建发布全攻略
harmonyos