为了账号安全,请及时绑定邮箱和手机立即绑定

Flutter 10: 图解底部状态栏小尝试

标签:
Android

      小菜今天来整理一下在学习测试 Flutter 时需用到的底部导航栏 BottomNavigationBar,使用方式很简单,小菜感觉效果比原生的 Android 要好一些。

何为 BottomNavigationBar ?

      BottomNavigationBar 为底部导航栏控件,可以包含文字标签和图标等基本信息,通常在三到五个之间;据了解,iOS 的规范底部导航栏最多可设置五个,所以大部分应用均在五个以内;现在很多应用都是以底部导航栏 + 中部主内容 Content 方式来展示。
      官网建议,BottomNavigationBar 底部导航栏通常与 Scaffold 一起使用,其中它作为Scaffold.bottomNavigationBar 参数提供。

如何应用 BottomNavigationBar ?

  1. 与 body 同级的位置添加 BottomNavigationBar,BottomNavigationBarItem 中可以添加文字标签或图标 (Icons/Image) 等,若图片不存在时会显示空白;这样就可以添加底部状态栏内容,文字和图标的样式也可以随意调整;如下:

bottomNavigationBar: new BottomNavigationBar(
  type: BottomNavigationBarType.fixed,
  items: [
    BottomNavigationBarItem(
        icon: new Icon(Icons.category), title: new Text('签到')),
    BottomNavigationBarItem(
        icon: new Icon(Icons.account_circle), title: new Text('我')),
  ],
),

webp

  1. 只有底部状态栏是不够的,还需要对应的中间展示内容块,可以跟 Android 的思路一样,添加几个 Page() 页作为 Fragment,小菜因为测试内容相对简单,尝试使用了 PageView,即对应 Android 中的 ViewPager,小菜会在今后的测试中详细说明,今天主要是使用基本方法展示主模块内容;如下:

body: new PageView.builder(
  itemBuilder: (BuildContext context, int index) {
    var str =
        (index == 0) ? "这里是【HomePage】->【签到】页面" : "这里是【HomePage】->【我】页面";    return new Center(
        child: new Container(
      width: 340.0,
      child: new Card(
          color: Colors.blue,
          elevation: 16.0,
          child: new FlatButton(
            child:                new Text(str, style: new TextStyle(color: Colors.white)),
          )),
    ));
  },
  itemCount: 2,
),

webp

  1. 此时主模块 PageView 可以滑动切换内容,但是对应的底部状态栏不会变化;因为目前没有绑定对应的点击事件等;此时需要添加 PageController 和 状态栏的 onTap 点击事件;如下:

int _currentIndex = 0;
var _pageController = new PageController(initialPage: 0);void _pageChange(int index) {    if (_currentIndex != index) {
      _currentIndex = index;
    }
}// 添加 PageView 的 PageControllerbody: new PageView.builder(
  onPageChanged: _pageChange,
  controller: _pageController,
  itemBuilder: (BuildContext context, int index) {
    ...
  }
),// 添加 BottomNavigationBar 的 onTapbottomNavigationBar: new BottomNavigationBar(
  type: BottomNavigationBarType.fixed,
  items: [
    ...
  ],  //设置当前的索引
  currentIndex: _currentIndex,  //tabBottom的点击监听
  onTap: (int index) {
    _pageController.animateToPage(index,
        duration: new Duration(seconds: 2),
        curve: new ElasticOutCurve(0.8));
    _pageChange(index);
    _currentIndex = index;
  },
),
  1. 此时,点击底部状态栏对应的 PageView 会切换内容,但是底部状态栏并没有改变样式,因为目前用的时固定的图标和文字,此时需要处理图标和文字切换时的样式,如下:

var _bottomText = ['签到', '我'];
var _bottomIcons = [
  [    new Icon(Icons.category, color: Colors.grey),    new Icon(Icons.category, color: Colors.blue),
  ],
  [    new Icon(Icons.account_circle, color: Colors.grey),    new Icon(Icons.account_circle, color: Colors.blue),
  ]
];Icon changeIconStyle(int curIndex) {  if (curIndex == _currentIndex) {    return _bottomIcons[curIndex][1];
  }  return _bottomIcons[curIndex][0];
}Text changeTextStyle(int curIndex) {  if (curIndex == _currentIndex) {    return new Text(_bottomText[curIndex],
        style: new TextStyle(color: Colors.blue));
  } else {    return new Text(_bottomText[curIndex],
        style: new TextStyle(color: Colors.grey));
  }
}

bottomNavigationBar: new BottomNavigationBar(
  type: BottomNavigationBarType.fixed,
  items: [    new BottomNavigationBarItem(
        icon: changeIconStyle(0), title: changeTextStyle(0)),    new BottomNavigationBarItem(
        icon: changeIconStyle(1), title: changeTextStyle(1)),
  ],
  ...
),

webp

  1. 然而小菜添加了更改状态时的样式,点击底部状态栏时依旧不会变色;小菜查了很久突然发现,小菜的 HomePage() 继承的是 StatelessWidget 无状态样式,此时更换为 StatefulWidget 有状态样式,并实现对应方法;如下:

class HomePage extends StatefulWidget {
  String result;

  HomePage(this.result);  @override
  State<StatefulWidget> createState() {    // TODO: implement createState
    return new HomePageState();
  }
}class HomePageState extends State<HomePage> {  int _currentIndex = 0;
  ...
}

webp

      至此,底部状态栏 BottomNavigationBar 配合滑动 PageView 的基本功能已经完成。

实用小贴士

  1. 通过点击 BottomNavigationBar 对 PageView 切换过程中,可以设置动画过程,也可以直接跳转到对应页面,需要设置 animateToPage 或 jumpToPage;如下:

onTap: (int index) {    // 切换时没有动画效果//    _pageController.jumpToPage(index);
    // 切换时添加动画效果
    _pageController.animateToPage(index,
        duration: new Duration(seconds: 2),
        curve: new ElasticOutCurve(0.8));
    _pageChange(index);
    _currentIndex = index;
},
  1. BottomNavigationBar 有两种样式分别为 shiftingfixed;直接效果图,shifting 样式时会突出显示选中的 item,其他的 item 文字隐藏;fixed 样式均分,没有突出效果;如下:

type: BottomNavigationBarType.shifting,

webp

shifting


webp

shifting

type: BottomNavigationBarType.fixed,

webp

fixed

webp

fixed



作者:阿策神奇


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
165
获赞与收藏
165

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消