Flutter底部导航栏插件fancy_bottom_navigation的使用

Flutter底部导航栏插件fancy_bottom_navigation的使用

FancyBottomNavigation

Fancy Gif

开始使用

pubspec.yaml 文件中添加插件依赖:

dependencies:
  ...
  fancy_bottom_navigation: ^0.3.2

限制条件

目前该插件仅支持2到4个标签页。

基本用法

FancyBottomNavigation 小部件添加到您的应用中:

bottomNavigationBar: FancyBottomNavigation(
    tabs: [
        TabData(iconData: Icons.home, title: "Home"),
        TabData(iconData: Icons.search, title: "Search"),
        TabData(iconData: Icons.shopping_cart, title: "Basket")
    ],
    onTabChangedListener: (position) {
        setState(() {
        currentPage = position;
        });
    },
)

TabData

  • iconData -> 用于标签的图标
  • title -> 用于标签的字符串
  • onclick -> 可选函数,在点击活动标签的圆圈时调用

属性

必需属性

  • tabs -> 包含 TabData 对象的列表
  • onTabChangedListener -> 处理标签点击的函数,接收 int position

可选属性

  • initialSelection -> 默认值为0
  • circleColor -> 默认为null,从 Theme 派生
  • activeIconColor -> 默认为null,从 Theme 派生
  • inactiveIconColor -> 默认为null,从 Theme 派生
  • textColor -> 默认为null,从 Theme 派生
  • barBackgroundColor -> 默认为null,从 Theme 派生
  • key -> 默认为null

主题定制

该栏将尝试自动使用当前主题。但是,您可能希望对其进行自定义。以下是相关属性:

Fancy Theming

程序化选择

要通过编程选择一个标签页,需要为小部件分配一个 GlobalKey。当您想要更改标签页时,需要使用此键访问状态,并调用 setPage(position) 方法。查看示例项目中的 main.dart 文件,第75行以了解示例。

示例代码

以下是一个完整的示例代码,展示了如何在应用中使用 FancyBottomNavigation 插件:

import 'package:example/second_page.dart';
import 'package:fancy_bottom_navigation/fancy_bottom_navigation.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.deepOrange,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int currentPage = 0;

  GlobalKey bottomNavigationKey = GlobalKey();

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Fancy Bottom Navigation"),
      ),
      body: Container(
        decoration: BoxDecoration(color: Colors.white),
        child: Center(
          child: _getPage(currentPage),
        ),
      ),
      bottomNavigationBar: FancyBottomNavigation(
        tabs: [
          TabData(
              iconData: Icons.home,
              title: "Home",
              onclick: () {
                final FancyBottomNavigationState fState = bottomNavigationKey
                    .currentState as FancyBottomNavigationState;
                fState.setPage(2);
              }),
          TabData(
              iconData: Icons.search,
              title: "Search",
              onclick: () => Navigator.of(context)
                  .push(MaterialPageRoute(builder: (context) => SecondPage()))),
          TabData(iconData: Icons.shopping_cart, title: "Basket")
        ],
        initialSelection: 1,
        key: bottomNavigationKey,
        onTabChangedListener: (position) {
          setState(() {
            currentPage = position;
          });
        },
      ),
      drawer: Drawer(
        child: ListView(
          children: <Widget>[Text("Hello"), Text("World")],
        ),
      ),
    );
  }

  _getPage(int page) {
    switch (page) {
      case 0:
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text("This is the home page"),
            RaisedButton(
              child: Text(
                "Start new page",
                style: TextStyle(color: Colors.white),
              ),
              color: Theme.of(context).primaryColor,
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => SecondPage()));
              },
            ),
            RaisedButton(
              child: Text(
                "Change to page 3",
                style: TextStyle(color: Colors.white),
              ),
              color: Theme.of(context).accentColor,
              onPressed: () {
                final FancyBottomNavigationState fState = bottomNavigationKey
                    .currentState as FancyBottomNavigationState;
                fState.setPage(2);
              },
            )
          ],
        );
      case 1:
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text("This is the search page"),
            RaisedButton(
              child: Text(
                "Start new page",
                style: TextStyle(color: Colors.white),
              ),
              color: Theme.of(context).primaryColor,
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => SecondPage()));
              },
            )
          ],
        );
      default:
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text("This is the basket page"),
            RaisedButton(
              child: Text(
                "Start new page",
                style: TextStyle(color: Colors.white),
              ),
              color: Theme.of(context).primaryColor,
              onPressed: () {},
            )
          ],
        );
    }
  }
}

更多关于Flutter底部导航栏插件fancy_bottom_navigation的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter底部导航栏插件fancy_bottom_navigation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter中使用fancy_bottom_navigation插件来创建底部导航栏的示例代码。这个插件提供了一个美观的底部导航栏,并且支持多种自定义选项。

首先,你需要在你的pubspec.yaml文件中添加fancy_bottom_navigation依赖:

dependencies:
  flutter:
    sdk: flutter
  fancy_bottom_navigation: ^x.y.z  # 请替换为最新版本号

然后,运行flutter pub get来安装依赖。

接下来是一个完整的示例代码,展示如何使用fancy_bottom_navigation插件:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fancy Bottom Navigation Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FancyBottomNavigationScaffold(
        body: HomeScreen(),
        tabs: [
          FancyTabData(
            iconData: Icons.home,
            title: 'Home',
            screen: HomeScreen(),
          ),
          FancyTabData(
            iconData: Icons.search,
            title: 'Search',
            screen: SearchScreen(),
          ),
          FancyTabData(
            iconData: Icons.add,
            title: 'Add',
            screen: AddScreen(),
          ),
          FancyTabData(
            iconData: Icons.profile,
            title: 'Profile',
            screen: ProfileScreen(),
          ),
        ],
        // 可选参数,用于自定义导航栏
        activeColor: Colors.blue,
        inactiveColor: Colors.grey,
        barBackgroundColor: Colors.white,
        hideNavigationBarWhenKeyboardShows: true,
        // 更多自定义选项...
      ),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('Home Screen'),
    );
  }
}

class SearchScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('Search Screen'),
    );
  }
}

class AddScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('Add Screen'),
    );
  }
}

class ProfileScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('Profile Screen'),
    );
  }
}

在这个示例中,我们做了以下几件事:

  1. pubspec.yaml中添加了fancy_bottom_navigation依赖。
  2. 使用FancyBottomNavigationScaffold作为主布局的脚手架。
  3. 使用FancyTabData定义了四个导航项,每个导航项都有一个图标、标题和对应的屏幕。
  4. 定义了四个简单的屏幕(HomeScreen, SearchScreen, AddScreen, ProfileScreen),每个屏幕只是显示一个文本。

你可以根据需要进一步自定义每个屏幕的内容,以及FancyBottomNavigationScaffold的其他属性,比如activeColor, inactiveColor, barBackgroundColor等。

希望这个示例代码对你有所帮助!

回到顶部