Android开发经验谈程序员Flutter圈子

新贵 Flutter(5) 底部导航栏

2019-06-14  本文已影响11人  zidea
flutter

底部导航是现在移动应用的标配,Flutter 也提供了底部导航栏 Widget

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: ZiApp()));

class ZiApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("BottomNavigationBar"),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.home,
                color: Colors.black87,
              ),
              title: Text("首页")),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.message,
                color: Colors.black87,
              ),
              title: Text("消息")),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.image,
                color: Colors.black87,
              ),
              title: Text("图片")),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.build,
                color: Colors.black87,
              ),
              title: Text("设置")),
        ],
      ),
    );
  }
}


现在只有选中的菜单才会显示文字,我们写可以通过修改 type 为 BottomNavigationBarType.fixed 来显示底部菜单的文字。

type: BottomNavigationBarType.fixed,
显示文字

添加事件

class ZiApp extends StatelessWidget {
  int index = 0;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("BottomNavigationBar"),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: index,
        onTap: (int idx) {
          index = idx;
        },
import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: ZiApp()));

class ZiApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ZiAppState();
  }
}

class _ZiAppState extends State<ZiApp> {
  int index = 0;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("BottomNavigationBar"),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: index,
        onTap: (int idx) {
          setState(() {
            index = idx;
          });
        },
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.home,
                color: Colors.black87,
              ),
              title: Text(
                "首页",
                style: TextStyle(color: Colors.black87),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.message,
                color: Colors.black87,
              ),
              title: Text(
                "消息",
                style: TextStyle(color: Colors.black87),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.image,
                color: Colors.black87,
              ),
              title: Text(
                "图片",
                style: TextStyle(color: Colors.black87),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.build,
                color: Colors.black87,
              ),
              title: Text(
                "设置",
                style: TextStyle(color: Colors.black87),
              )),
        ],
      ),
    );
  }
}

切换图片

class _ZiAppState extends State<ZiApp> {
  int index = 0;
  List<Widget> pages = [
    Container(
      color: Colors.amber,
    ),
    Container(
      color: Colors.orange,
    ),
    Container(
      color: Colors.green,
    ),
    Container(
      color: Colors.blue,
    ),
  ];
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("BottomNavigationBar"),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: index,
        onTap: (int idx) {
          setState(() {
            index = idx;
          });
        },
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.home,
                color: Colors.black87,
              ),
              title: Text(
                "首页",
                style: TextStyle(color: Colors.black87),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.message,
                color: Colors.black87,
              ),
              title: Text(
                "消息",
                style: TextStyle(color: Colors.black87),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.image,
                color: Colors.black87,
              ),
              title: Text(
                "图片",
                style: TextStyle(color: Colors.black87),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.build,
                color: Colors.black87,
              ),
              title: Text(
                "设置",
                style: TextStyle(color: Colors.black87),
              )),
        ],
      ),
      body: pages[index],
    );
  }
}

上一篇 下一篇

猜你喜欢

热点阅读