zoukankan      html  css  js  c++  java
  • BottomNavigationBar 自定义 底部导航条

    在flutter中,BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold 组件的参数。 

    BottomNavigationBar 常见的属性 

    • items :List底部导航条按钮集合 
    • iconSize :icon 
    • currentIndex :默认选中第几个 
    • onTap:选中变化回调函数 
    • fixedColor :选中的颜色 
    • type :BottomNavigationBarType.fixed  &  BottomNavigationBarType.shifting

    基本实现

    import 'package:flutter/material.dart';
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {  
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home:Scaffold(
            appBar: AppBar(
              title: Text("Flutter Demo"),
            ),
            body: Text("tabBar"),
            bottomNavigationBar: BottomNavigationBar(
              items: [
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  title: Text("首页")
                ),
                 BottomNavigationBarItem(
                  icon: Icon(Icons.category),
                  title: Text("分类")
                ),
                
                 BottomNavigationBarItem(
                  icon: Icon(Icons.settings),
                  title: Text("设置")
                )
              ],
            ),
          )
        );
      }
    }

    切换选中

    当我们点击按钮,想要切换选中的导航块时,需要监听onTap,然后改变currentIndex。

    import 'package:flutter/material.dart';
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {  
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home:Tabs()
        );
      }
    }
    
    class Tabs extends StatefulWidget {
      Tabs({Key key}) : super(key: key);
    
      _TabsState createState() => _TabsState();
    }
    
    class _TabsState extends State<Tabs> {
    
      int _currentIndex=0;
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text("Flutter Demo"),
            ),
            body: Text("tabBar"),
            bottomNavigationBar: BottomNavigationBar(
    
              currentIndex: this._currentIndex,
              onTap: (int index){
                  setState(() {
                      this._currentIndex=index;
                  });
              },
              items: [
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  title: Text("首页")
                ),
                 BottomNavigationBarItem(
                  icon: Icon(Icons.category),
                  title: Text("分类")
                ),
                
                 BottomNavigationBarItem(
                  icon: Icon(Icons.settings),
                  title: Text("设置")
                )
              ],
            ),
          );
      }
    }

      

    需要特别说明的是,默认可以显示两个或者三个BottomNavigationBarItem,如果有更多的BottomNavigationBarItem需要显示,则需要配置type的为BottomNavigationBarType.fixed,否则样式会出现问题。

    页面切换

     要实现点击后页面切换的效果,首先需要有三个页面,在flutter中,一切皆组件,页面也是组件。

    首先,在lib目录下新建pages文件夹,然后在pages下新建文件夹tabs,并新建上面导航对应的三个页面。

    Category.dart

    import 'package:flutter/material.dart';
    
    class CategoryPage extends StatefulWidget {
      CategoryPage({Key key}) : super(key: key);
      _CategoryPageState createState() => _CategoryPageState();
    }
    
    class _CategoryPageState extends State<CategoryPage> {
      @override
      Widget build(BuildContext context) {
        return Text("分类");
      }
    }

    Home.dart

    import 'package:flutter/material.dart';
    
    class HomePage extends StatefulWidget {
      HomePage({Key key}) : super(key: key);
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      @override
      Widget build(BuildContext context) {
        return Text("我是首页组件");
      }
    }

    Setting.dart

    import 'package:flutter/material.dart';
    
    class SettingPage extends StatefulWidget {
      SettingPage({Key key}) : super(key: key);
      _SettingPageState createState() => _SettingPageState();
    }
    
    class _SettingPageState extends State<SettingPage> {
      @override
      Widget build(BuildContext context) {
        return ListView(
          children: <Widget>[
            ListTile(
              title: Text("我是一个文本"),
            ),
             ListTile(
              title: Text("我是一个文本"),
            ),
             ListTile(
              title: Text("我是一个文本"),
            ),
             ListTile(
              title: Text("我是一个文本"),
            )
          ],
        );
      }
    }

    然后,在pages文件夹下新建Tabs.dart文件,并将上面例子中的tabs组件剪切到这个文件中,

    import 'package:flutter/material.dart';
    class Tabs extends StatefulWidget {
      Tabs({Key key}) : super(key: key);
    
      _TabsState createState() => _TabsState();
    }
    
    class _TabsState extends State<Tabs> {
    
      int _currentIndex=0;
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text("Flutter Demo"),
            ),
            body: Text("tabBar"),
            bottomNavigationBar: BottomNavigationBar(
    
              currentIndex: this._currentIndex,
              onTap: (int index){
                  setState(() {
                      this._currentIndex=index;
                  });
              },
              items: [
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  title: Text("首页")
                ),
                 BottomNavigationBarItem(
                  icon: Icon(Icons.category),
                  title: Text("分类")
                ),
                
                 BottomNavigationBarItem(
                  icon: Icon(Icons.settings),
                  title: Text("设置")
                )
              ],
            ),
          );
      }
    }

    最后,在main.dart中引入Tabs.dart

    import 'package:flutter/material.dart';
    
    import 'pages/Tabs.dart';
    
    void main() => runApp(MyApp());
    class MyApp extends StatelessWidget {  
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home:Tabs()
        );
      }
    }

    此时,仅仅只是实现上面例子中的效果,还不能实现页面的切换,还需要继续修改Tabs.dart文件。

    import 'package:flutter/material.dart';
    import 'tabs/Home.dart';
    import 'tabs/Category.dart';
    import 'tabs/Setting.dart';
    
    class Tabs extends StatefulWidget {
      Tabs({Key key}) : super(key: key);
      _TabsState createState() => _TabsState();
    }
    
    class _TabsState extends State<Tabs> {
    
      int _currentIndex=0;
      List _pageList=[
        HomePage(),
        CategoryPage(),
        SettingPage(),
      ];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text("Flutter Demo"),
            ),
            body: this._pageList[this._currentIndex],
            bottomNavigationBar: BottomNavigationBar(
              currentIndex: this._currentIndex,   //配置对应的索引值选中
              onTap: (int index){
                  setState(() {  //改变状态
                      this._currentIndex=index;
                  });
              },
              iconSize:36.0,      //icon的大小
              fixedColor:Colors.red,  //选中的颜色  
              type:BottomNavigationBarType.fixed,   //配置底部tabs可以有多个按钮
              items: [
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  title: Text("首页")
                ),
                 BottomNavigationBarItem(
                  icon: Icon(Icons.category),
                  title: Text("分类")
                ),
                
                 BottomNavigationBarItem(
                  icon: Icon(Icons.settings),
                  title: Text("设置")
                )
              ],
            ),
          );
      }
    }

     代码下载:点这里  提取码(xsju)

  • 相关阅读:
    亲和数
    改革春风吹满地
    处理选中图片
    项目架构(结构)搭建:主流结构(UITabBarController + 导航控制器)
    iOS应用启动main函数
    LaunchScreen&LaunchImage
    颜色常识
    折半查找法(二分法)
    冒泡排序
    多用户登录
  • 原文地址:https://www.cnblogs.com/yuyujuan/p/11000934.html
Copyright © 2011-2022 走看看