flutter之桌面侧边导航栏 NavigationRail

本文详细介绍了Flutter中的NavigationRail组件,包括其构造函数参数、onDestinationSelected回调、如何切换页面、首尾组件与折叠功能、以及阴影、标签类型和样式设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

资料来源

Flutter 组件集录 | 桌面导航 NavigationRail - 掘金 (juejin.cn) 

NavigationRail 构造函数

 const NavigationRail({
    Key? key,
    this.backgroundColor,
    this.extended = false,
    this.leading,
    this.trailing,
    required this.destinations,
    required this.selectedIndex,
    this.onDestinationSelected,
    this.elevation,
    this.groupAlignment,
    this.labelType,
    this.unselectedLabelTextStyle,
    this.selectedLabelTextStyle,
    this.unselectedIconTheme,
    this.selectedIconTheme,
    this.minWidth,
    this.minExtendedWidth,
    this.useIndicator,
    this.indicatorColor,
  }) :  assert(destinations != null && destinations.length >= 2),
        assert(selectedIndex == null || (0 <= selectedIndex && selectedIndex < destinations.length)),
        assert(elevation == null || elevation > 0),
        assert(minWidth == null || minWidth > 0),
        assert(minExtendedWidth == null || minExtendedWidth > 0),
        assert((minWidth == null || minExtendedWidth == null) || minExtendedWidth >= minWidth),
        assert(extended != null),
        assert(!extended || (labelType == null || labelType == NavigationRailLabelType.none)),
        super(key: key);

NavigationRail 必须传入两个参数

  • destinations : 表示导航栏的信息,是 NavigationRailDestination 列表。
  • selectedIndex: 表示激活索引,int 类型。

onDestinationSelected 回调方法

NavigationRail 通过 onDestinationSelected 回调方法,来监听用户和导航栏的交互事件,传递用点击的索引位置。

切换页面

使用PageView,通过 PageController 切换界面。在调用onDestinationSelected 回调方法更新索引时,通过 PageController 的 jumpToPage 方法进行界面跳转。

void switchTap(int value){
    print("click $value 个侧边栏");
    state.selectedIndex.value = value;
    state.pageController.jumpToPage(value);
    update();
  }

首尾组件与折叠

leading 和 trailing 属性相当于两个插槽,如下所示,表示导航菜单外的首尾组件。

final Widget? leading;

final Widget? trailing;

折叠

extended 的 bool 参数,用于控制是否展开侧边栏,当该属性变化时,会进行动画展开和收起。

影深 与 标签类型

elevation 表示阴影的深度,设置 elevation 之后右侧会有阴影,该值越大,阴影越明显。

labelType 参数表示标签类型,对应的属性是 NavigationRailLabelType 枚举。用于表示什么时候显示文字标签,默认是 none ,也就是只显示图标,没有文字。

enum NavigationRailLabelType {
  none,
  selected,
  all,
}
  • 设置为 all 时,效果如下:导航菜单会同时显示 图标 和 文字标签
  • 设置为 selected 时,效果如下:只有激活的导航菜单会同时显示 图标 和 文字标签 。
  • 有一点需要注意: 当 extended 属性为 true 时, labelType 必须为 none 不然会报错。

背景、文字、图标样式

  • unselectedLabelTextStyle : 未选中签文字样式
  • selectedLabelTextStyle : 选中标签文字样式
  • unselectedIconTheme : 未选中图标样式
  • selectedIconTheme : 选中图标样式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值