目录

元服务中Navigation替换AtomicServiceNavigation

问题现象

元服务AtomicServiceNavigation中titleOptions参数,当titleBarType字段设置为TitleBarType.ROUND_ICON或者TitleBarType.SQUARED_ICON,且设置了titleIcon时,title标题内容将不会显示,如何实现同时显示标题栏的图标和标题?

背景知识

组件导航(Navigation)主要用于实现Navigation页面间的跳转,支持在不同Navigation页面间传递参数,提供灵活的跳转栈操作,从而更便捷地实现对不同页面的访问和复用。相比AtomicServiceNavigation,Navigation功能更强大,在元服务中可以使用Navigation替换AtomicServiceNavigation。

解决方案

参考如下实现方式:

@Entry
@Component
struct NavigationPage {
  @State message: string = '标题';
  @State currentIndex: number = 0;
  childNavStack: NavPathStack = new NavPathStack();

  @Builder
  tabContent1() {
    Text('first page')
      .onClick(() => {
        this.childNavStack.pushPath({ name: 'page one' });
      });
  }

  @Builder
  tabContent2() {
    Text('second page');
  }

  @Builder
  tabContent3() {
    Text('third page');
  }

  @Builder
  pageMap(name: string) {
    if (name === 'page one') {
      PageOne();
    } else if (name === 'page two') {
      PageTwo();
    }
  }

  @Builder
  tabBuilder(title: string, targetIndex: number, normalImg: Resource) {
    Column() {
      Image(normalImg)
        .size({ width: 25, height: 25 })
        .fillColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B');
      Text(title)
        .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B');
    }
    .width('100%')
    .height(50)
    .justifyContent(FlexAlign.Center);
  }

  @Builder
  CustomTitle() {
    Row() {
      Image($r('app.media.startIcon')).width(30).height(30).margin({ left: 5, right: 5 });
      Text(this.message).fontWeight(FontWeight.Bold).fontSize(30);
    }
    .width('100%')
    .margin({ top: 10 });
  }

  build() {
    Column() {
      Navigation(this.childNavStack) {
        Tabs({ barPosition: BarPosition.End }) {
          TabContent() {
            this.tabContent1();
          }
          .tabBar(this.tabBuilder('功能1', 0, $r('sys.media.ohos_ic_public_phone')));

          TabContent() {
            this.tabContent2();
          }
          .tabBar(this.tabBuilder('功能2', 1, $r('sys.media.ohos_ic_public_location')));

          TabContent() {
            this.tabContent3();
          }
          .tabBar(this.tabBuilder('功能3', 2, $r('sys.media.ohos_ic_public_more')));

        }
        .onTabBarClick((index) => {
          if (index === 0) {
            this.message = '功能1';
            this.currentIndex = 0;
          } else if (index === 1) {
            this.message = '功能2';
            this.currentIndex = 1;
          } else {
            this.message = '功能3';
            this.currentIndex = 2;
          }
        })
        .width('100%');
      }
      .title(this.CustomTitle(),
        {
          backgroundColor: '#3d9db4',
          enableHoverMode: false
        })
      .titleMode(NavigationTitleMode.Mini)
      .mode(NavigationMode.Stack)
      .navDestination(this.pageMap)
      .hideBackButton(true);
    }
    .height('100%')
    .width('100%')
    .backgroundColor('#F1F3F5')
    .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]);
  }
}

@Component
export struct PageOne {
  pageInfo: NavPathStack = new NavPathStack();

  build() {
    NavDestination() {
      Button('Next')
        .onClick(() => {
          this.pageInfo.pushPath({ name: 'page two' });
        });
    }
    .title('PageOne')
    .onReady((context: NavDestinationContext) => {
      this.pageInfo = context.pathStack;
    });
  }
}

@Component
export struct PageTwo {
  pageInfo: NavPathStack = new NavPathStack();

  build() {
    NavDestination() {
      Button('End');
    }
    .title('PageTwo')
    .onReady((context: NavDestinationContext) => {
      this.pageInfo = context.pathStack;
    });
  }
}