美文网首页
flutter02: 底部导航

flutter02: 底部导航

作者: 蜗牛的学习方法 | 来源:发表于2019-06-12 09:42 被阅读0次

1、下面是我创建的一个flutter的项目,目录结构如图


WX20190612-092254.png

2、main.dart是入口文件,里面引入的home.dart就是底部导航,然后把home.dart里面的对象 在入口文件的body里面实例化出来

//main.dart 文件
import 'package:flutter/material.dart';
import './home.dart';
import './firstPage/live.dart';

void main(){
  runApp(
    new MaterialApp(
      title:'flutter App',
      theme: new ThemeData(
        primarySwatch:Colors.blue,
      ),
      routes:<String, WidgetBuilder>{
        "new_page":(context)=>LivePage(),
      } ,
      home:new MyHomePage(),
      
    )
  );
}
//home.dart
import 'package:flutter/material.dart';
//下面的五个引入就是底部导航需要跳转的页面
import './first.dart';
import './second.dart';
import './third.dart';
import './zhuanshi.dart';
import './user.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final items = [
    BottomNavigationBarItem(icon: Icon(IconData(0xe684, fontFamily: 'iconfont',),size: 24.0), title: Text('首页')),
    BottomNavigationBarItem(icon: Icon(IconData(0xe635, fontFamily: 'iconfont'),size: 24.0), title: Text('关注')),
    BottomNavigationBarItem(icon: Icon(IconData(0xe636, fontFamily: 'iconfont'),size: 24.0), title: Text('简书钻')),
    BottomNavigationBarItem(icon: Icon(IconData(0xe616, fontFamily: 'iconfont'),size: 24.0), title: Text('消息')),
    BottomNavigationBarItem(icon: Icon(IconData(0xe60c, fontFamily: 'iconfont'),size: 24.0), title: Text('我的')),
  ];

  final bodyList = [FirstPage(), SecondPage(), ThirdPage(),ZhuanShiPage(),UserPage()];

  int currentIndex = 0;

  void onTap(int index) {
    setState(() {
      currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('demo'),
        ),
        bottomNavigationBar: BottomNavigationBar( 
          items: items,
          currentIndex: currentIndex, 
          onTap: onTap,
          type: BottomNavigationBarType.fixed,
        ),
        body: IndexedStack(  //保持之前的页面点击的状态
          index: currentIndex,
          children: bodyList,
        )
    );
  }
}

其他的first.dart、second.dart、third.dart、user.dart、zhuangshi.dart都是和second.dart一样的格式,只是里面的类名要变成不一样的,避免冲突

//second.dart
import 'package:flutter/material.dart';

class SecondPage extends StatefulWidget {
  @override
  _SecondPageState createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  int count = 0;

  void add() {
    setState(() {
      count++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
            child: Text('Second: $count', style: TextStyle(fontSize: 30))
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: add,
          child: Icon(Icons.add),
        )
    );
  }
}

最终效果:


image.png

代码:
(底部常见导航)

import 'package:flutter/material.dart';
import 'package:flutter_demo/tabs/home.dart';
import 'package:flutter_demo/tabs/square.dart';
import 'package:flutter_demo/tabs/quote.dart';
import 'package:flutter_demo/tabs/trade.dart';

class BottomNavigationWidget extends StatefulWidget {
  _BottomNavigationWidgetState createState() => _BottomNavigationWidgetState();
}

class _BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  int _currentIndex = 0;
  List<Widget> list = [Home(),Square(),Quote(),Trade()];

  @override
  Widget build(BuildContext context) {
     return Scaffold(
       body: list[_currentIndex],
       bottomNavigationBar: BottomNavigationBar(
         items: [
          BottomNavigationBarItem(
            icon:Icon(Icons.home,color:Colors.grey),
            activeIcon: Icon(Icons.home,color:Colors.lightBlue),
            title:Text('小仙',style: TextStyle(color: _currentIndex!=0?Colors.grey:Colors.lightBlue),)
          ),
          BottomNavigationBarItem(
            icon:Icon(Icons.home,color:Colors.grey),
            activeIcon: Icon(Icons.home,color:Colors.lightBlue),
            title:Text('广场',style: TextStyle(color:_currentIndex!=1?Colors.grey:Colors.lightBlue),)
          ),
          BottomNavigationBarItem(
            icon:Icon(Icons.home,color:Colors.grey),
            activeIcon: Icon(Icons.home,color:Colors.lightBlue),
            title:Text('行情',style: TextStyle(color: _currentIndex!=2?Colors.grey:Colors.lightBlue),)
          ),
          BottomNavigationBarItem(
            icon:Icon(Icons.home,color:Colors.grey),
            activeIcon: Icon(Icons.home,color:Colors.lightBlue),
            title:Text('交易',style: TextStyle(color: _currentIndex!=3?Colors.grey:Colors.lightBlue),)
          ),
        ],
         currentIndex:_currentIndex,
         onTap:(int index){
           setState((){
             _currentIndex= index;
           });
         },
         type:BottomNavigationBarType.fixed,//底部导航文字显示在底部
       ),
     );
  }
}

底部自定义导航

import 'package:flutter/material.dart';
import './pages/each_view.dart';


class BottomOptionalWidget extends StatefulWidget{
  _BottomOptionalState createState() => _BottomOptionalState();
}

class _BottomOptionalState extends State<BottomOptionalWidget>{
  List<Widget> _eachView;  //创建视图数组
  int _index = 0;          //数组索引,通过改变索引值改变视图

  @override
  void initState() {  //视图初始化赋值
    super.initState();
    _eachView = List();
    _eachView..add(EachView('Home'))..add(EachView('Me'));
  }


  @override
  Widget build(BuildContext context){
    return Scaffold(
      body:_eachView[_index],
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          Navigator.of(context).push(MaterialPageRoute(builder:(BuildContext context){
            return EachView('New Page');
          }));
        },
        tooltip: 'Increment',
        child: Icon(Icons.add,color:Colors.white),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,//中心停靠,与底栏重合
      bottomNavigationBar:BottomAppBar(
        color: Colors.blue,
        shape: CircularNotchedRectangle(),//设置底栏的形状,有缺口的圆形矩形
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.home),
              color: Colors.white,
              onPressed: (){
                 setState(() {
                    _index=0;             
                  });
              },
            ),
            IconButton(
              icon: Icon(Icons.airport_shuttle),
              color: Colors.white,
              onPressed: (){
                setState(() {
                    _index=1;             
                  });
              },
            ),
          ],
        ),
      ),
    );
    
  }
}

相关文章

  • flutter02: 底部导航

    1、下面是我创建的一个flutter的项目,目录结构如图 2、main.dart是入口文件,里面引入的home.d...

  • BottomNavigationView的属性设置

    底部导航栏 底部导航栏的使用比较常见,目前常用的APP几乎都是使用底部导航栏将内容分类。底部导航栏的实现也比较简单...

  • Android使用底部导航2018-08-16

    Android使用底部导航 Android底部导航停留在屏幕底部,提供应用中顶级视图之间的导航。这是在具有向后兼容...

  • 有一种看似多余的导航你知道吗?

    很多网站在页面底部也有导航,这种底部导航看似有些多余。但是他们的作用不比主导航差。 底部导航的作用和做法有如下几种...

  • 为什么底部导航被人忽视

    很多网站在页面底部也有导航,这种底部导航看似有些多余。但是他们的作用不比主导航差。 底部导航的作用和做法有如下几种...

  • 底部导航思路

    底部导航是每个 app 必备的,有的 app 底部导航做的很精美,很有诱人,于是我去特意看看底部导航都有那些玩法 ...

  • 移动端交互之导航

    讨论两种导航架构在应用中的区分:底部横向导航和侧边纵向导航 1、底部横向导航 特点:横向排列与界面底部,可直接方便...

  • 使用 TabLayout 制作底部导航栏

    国内大部分应用使用底部导航栏, 底部导航栏 是国内 APP 常见的导航方式, 历经: TabActivity -...

  • 安卓底部导航

    Android底部导航栏实现(一)之BottomNavigationBarAndroid底部导航栏实现(二)之Ra...

  • flutter底部导航

    常规App都有一个底部导航,flutter为我们封装好了底部导航组件

网友评论

      本文标题:flutter02: 底部导航

      本文链接:https://www.haomeiwen.com/subject/irfkfctx.html