flutter BottomAppBar实现不规则底部导航栏

所属分类: 软件编程 / Android 阅读数: 92
收藏 0 赞 0 分享

本文实例为大家分享了flutter实现不规则底部导航栏的具体代码,供大家参考,具体内容如下

实现底部导航栏并点击切换页面可简述为有三种方式

  • TabBar + TabBarView
  • BottomNavigationBar + BottomNavigationBarItem
  • 自定义 BottomAppBar

在这里 使用 BottomAppBar 来实现

/**
 * 有状态StatefulWidget
 * 继承于 StatefulWidget,通过 State 的 build 方法去构建控件
 */
class BotomeMenumBarPage extends StatefulWidget {
 ////通过构造方法传值
 BotomeMenumBarPage();

 //主要是负责创建state
 @override
 BotomeMenumBarPageState createState() => BotomeMenumBarPageState();
}

/**
 * 在 State 中,可以动态改变数据
 * 在 setState 之后,改变的数据会触发 Widget 重新构建刷新
 */
class BotomeMenumBarPageState extends State<BotomeMenumBarPage> {
 BotomeMenumBarPageState();

 @override
 void initState() {
  ///初始化,这个函数在生命周期中只调用一次
  super.initState();
 }

 @override
 Widget build(BuildContext context) {
  //构建页面
  return buildBottomTabScaffold();
 }

 //当前显示页面的
 int currentIndex = 0;
 //点击导航项是要显示的页面
 final pages = [
  ChildItemView("首页"),
  ChildItemView("发现"),
  ChildItemView("动态"),
  ChildItemView("我的")
 ];

 Widget buildBottomTabScaffold() {
  return SizedBox(
    height: 100,
    child: Scaffold(
     //对应的页面
     body: pages[currentIndex],
     //appBar: AppBar(title: const Text('Bottom App Bar')),
     //悬浮按钮的位置
     floatingActionButtonLocation:
       FloatingActionButtonLocation.centerDocked,
     //悬浮按钮
     floatingActionButton: FloatingActionButton(
      child: const Icon(Icons.add),
      onPressed: () {
       print("add press ");
      },
     ),
     //其他菜单栏
     bottomNavigationBar: BottomAppBar(
      //悬浮按钮 与其他菜单栏的结合方式
      shape: CircularNotchedRectangle(),
      // FloatingActionButton和BottomAppBar 之间的差距
      notchMargin: 6.0,
      color: Colors.white,
      child: Row(
       mainAxisSize: MainAxisSize.max,
       mainAxisAlignment: MainAxisAlignment.spaceAround,
       children: <Widget>[
        buildBotomItem(currentIndex, 0, Icons.home, "首页"),
        buildBotomItem(currentIndex, 1, Icons.library_music, "发现"),
        buildBotomItem(currentIndex, -1, null, "发现"),
        buildBotomItem(currentIndex, 2, Icons.email, "消息"),
        buildBotomItem(currentIndex, 3, Icons.person, "我的"),
       ],
      ),
     ),
    ));
 }
 

// ignore: slash_for_doc_comments
 /**
  * @param selectIndex 当前选中的页面
  * @param index 每个条目对应的角标
  * @param iconData 每个条目对就的图标
  * @param title 每个条目对应的标题
  */
 buildBotomItem(int selectIndex, int index, IconData iconData, String title) {
  //未选中状态的样式
  TextStyle textStyle = TextStyle(fontSize: 12.0,color: Colors.grey);
  MaterialColor iconColor = Colors.grey;
  double iconSize=20;
  EdgeInsetsGeometry padding = EdgeInsets.only(top: 8.0);

  if(selectIndex==index){
   //选中状态的文字样式
   textStyle = TextStyle(fontSize: 13.0,color: Colors.blue);
   //选中状态的按钮样式
   iconColor = Colors.blue;
   iconSize=25;
   padding = EdgeInsets.only(top: 6.0);
  }
  Widget padItem = SizedBox();
  if (iconData != null) {
   padItem = Padding(
    padding: padding,
    child: Container(
     color: Colors.white,
     child: Center(
      child: Column(
       children: <Widget>[
        Icon(
         iconData,
         color: iconColor,
         size: iconSize,
        ),
        Text(
         title,
         style: textStyle,
        )
       ],
      ),
     ),
    ),
   );
  }
  Widget item = Expanded(
   flex: 1,
   child: new GestureDetector(
    onTap: () {
     if (index != currentIndex) {
      setState(() {
       currentIndex = index;
      });
     }
    },
    child: SizedBox(
     height: 52,
     child: padItem,
    ),
   ),
  );
  return item;
 }
}
//子页面
class ChildItemView extends StatefulWidget {
 String _title;

 ChildItemView(this._title);

 @override
 _ChildItemViewState createState() => _ChildItemViewState();
}

class _ChildItemViewState extends State<ChildItemView> {
 @override
 Widget build(BuildContext context) {
  return Container(
   child: Center(child: Text(widget._title)),
  );
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多