ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

flutter实现底部导航栏

2022-01-06 17:01:04  阅读:189  来源: 互联网

标签:BottomNavigationBar title dart Colors 选中 底部 key 导航 flutter


一.flutter底部导航栏常用组件BottomNavigationBar 属性介绍

 BottomNavigationBar({
    Key? key,
    required this.items, //必填项,设置各个按钮
    this.onTap, //点击事件
    this.currentIndex = 0, //当前选中item下标
    this.elevation, //控制阴影高度
    this.type, //BottomNavigationBarType,默认 fixed,设置为 shifting 时,需要设置选中样式,和未选中样式,提供一个特殊动画
    Color? fixedColor, //选中 item 填充色
    this.backgroundColor, //整个BottomNavigationBar 背景色
    this.iconSize = 24.0, //图标大小
    Color? selectedItemColor, //选中title填充色
    this.unselectedItemColor, //未选中title填充色
    this.selectedIconTheme, //选中item图标主题
    this.unselectedIconTheme, //未选中item图标主题
    this.selectedFontSize = 14.0, //选中title字体大小
    this.unselectedFontSize = 12.0, //未选中title字体大小
    this.selectedLabelStyle, //选中title样式 TextStyle
    this.unselectedLabelStyle, //未选中title样式 TextStyle
    this.showSelectedLabels, //是否展示选中title,默认为true
    this.showUnselectedLabels, //是否展示未选中title,默认为true
    this.mouseCursor, //鼠标悬停
    this.enableFeedback,
    this.landscapeLayout,
  }) 

二.BottomNavigationBar的具体实现

1.创建四个页面,分别为,首页,通讯录,发现和我的,这里以homepage.dart为例,其他页面只需要修改对应内容显示即可,eg:

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget{

  const HomePage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState()=>_HomePageState();

}

class _HomePageState extends State<HomePage>{

  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Text(
        "主页",
        style:TextStyle(
          color: Colors.black,
          fontSize: 20
        ),
      ),
    );
  }

}

2.添加BottomNavigationBar,需要在主页中实现BottomNavigationBar,eg:

import 'package:flutter/material.dart';
import 'findpage.dart';
import 'mypage.dart';
import 'contactpage.dart';
import 'homepage.dart';

class MainPage extends StatefulWidget{
  const MainPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState()=>_MainPageState();
}

class _MainPageState extends State<MainPage>{

  var allPages=[HomePage(),ContactPage(),FindPage(),MyPage()];
  var currentIndex=0;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "导航栏",
          style: TextStyle(
            color: Colors.black,
            fontSize: 30,
          ),
          textAlign: TextAlign.center,
        ),
      ),
      body: allPages[currentIndex],
      backgroundColor: Colors.green,
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.blue,
        /*unselectedLabelStyle:TextStyle(
          color: Colors.black
        ),*/
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: "首页",
              //backgroundColor:Colors.blue
          ),

          BottomNavigationBarItem(
              icon: Icon(Icons.person),
              label: "通讯录",
              //backgroundColor:Colors.blue
          ),

          BottomNavigationBarItem(
              icon: Icon(Icons.find_in_page),
              label: "发现",
              //backgroundColor:Colors.blue
          ),

          BottomNavigationBarItem(
              icon: Icon(Icons.flip_outlined),
              label: "我的",
              //backgroundColor:Colors.blue
          ),
        ],

        onTap: (index){
          setState(() {
            print("the index is :$index");
            currentIndex=index;
          });
        },
      ),
    );
  }
}

三.实际效果展示,eg:

标签:BottomNavigationBar,title,dart,Colors,选中,底部,key,导航,flutter
来源: https://blog.csdn.net/j086924/article/details/122347292

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有