Flutter - Navigation Bar

Flutter - Navigation Bar 首页 / Flutter入门教程 / Flutter - Navigation Bar

底部导航栏在最近几年中变得很流行,可以在不同的UI之间进行导航。在Flutter应用程序中,无涯教程通常将底部导航栏与脚手架小部件一起设置。脚手架小部件提供了一个Scaffold.bottomNavigationBar参数来设置底部导航栏。

BottomNavigationBar属性

以下是与底部导航栏小部件一起使用的属性:

链接:https://www.learnfk.comhttps://www.learnfk.com/flutter/flutter-bottom-navigation-bar.html

来源:LearnFk无涯教程网

items               - 它定义了要显示在底部导航栏中的列表。它使用参数BottomNavigationBarItem,该参数包含下面给出的sup-properties:

const BottomNavigationBarItem({
    @required this.icon,
    this.title,
    Widget activeIcon,
    this.backgroundColor,
  }) 

currentIndex   -  它确定屏幕上当前活动的底部导航栏项目。

onTap                 -  当无涯教程点击屏幕上时,将调用它。

iconSize            -  它用于指定所有底部导航项目图标的大小。

fixedColor        -  用于设置所选项目的颜色。如果无涯教程尚未为图标或标题设置颜色,则会显示该颜色。

type                     -  它确定底部导航栏的布局和行为。它以两种不同的方式运行:fixed和shifting。如果为null,则将使用fixed。否则,它将使用shifting

例子

让无涯教程了解如何借助示例在Flutter应用程序中创建底部导航栏。因此,打开android studio并创建Flutter应用程序。下一步。打开main.dart文件,并使用以下代码删除其代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// 这个小部件是主要的应用小部件。
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyNavigationBar (),
    );
  }
}

class MyNavigationBar extends StatefulWidget {
  MyNavigationBar ({Key key}) : super(key: key);

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

class _MyNavigationBarState extends State<MyNavigationBar > {
  int _selectedIndex = 0;
  static const List<Widget> _widgetOptions = <Widget>[
    Text('Home Page', style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold)),
    Text('Search Page', style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold)),
    Text('Profile Page', style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold)),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter BottomNavigationBar Example'),
          backgroundColor: Colors.green
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
            backgroundColor: Colors.green
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.search),
            title: Text('Search'),
            backgroundColor: Colors.yellow
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            title: Text('Profile'),
            backgroundColor: Colors.blue,
          ),
        ],
        type: BottomNavigationBarType.shifting,
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.black,
        iconSize: 40,
        onTap: _onItemTapped,
        elevation: 5
      ),
    );
  }
}

在上面的代码中,无涯教程在脚手架小部件中使用了BottomNavigationBar。该导航包含三个BottomNavigationBarItem小部件。在这里,无涯教程将currentIndex设置为0,以选择绿色的项目。 onTap()函数用于更改所选项目的索引,然后显示相应的消息。

当无涯教程运行应用程序时,无涯教程应该获得类似于下面的屏幕截图的UI:

Flutter Bottom Navigation Bar

当无涯教程点击底部导航栏中的搜索图标时,它将提供下面的屏幕。

Flutter Bottom Navigation Bar

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入浅出区块链 -〔陈浩〕

Java性能调优实战 -〔刘超〕

基于人因的用户体验设计课 -〔刘石〕

Spark性能调优实战 -〔吴磊〕

说透数字化转型 -〔付晓岩〕

遗留系统现代化实战 -〔姚琪琳〕

超级访谈:对话道哥 -〔吴翰清(道哥)〕

AI大模型企业应用实战 -〔蔡超〕

给程序员的写作课 -〔高磊〕

好记忆不如烂笔头。留下您的足迹吧 :)