Flutter底部导航栏适配插件top_notch_bottom_bar的使用

Flutter底部导航栏适配插件top_notch_bottom_bar的使用

TopNotchBottomBar

一个美观且带有动画效果的底部导航栏。你可以根据自己的需求自由定制它。

预览

演示图1 演示图2 演示图3

定制化

TopNotchBottomBar

必须参数

  • items - 导航项,必须包含多于一个且少于六个项。
  • onTap - 监听项被点击时的事件,提供选中的项的索引。

可选参数

  • backgroundColor - 底部导航栏的背景颜色。
  • height - 改变底部栏的高度(默认为50)。
  • iconSize - 项图标的大小。
  • showLabel - 项的文字标签。
  • activeColor - 选中项的颜色。
  • inActiveColor - 未选中项的颜色。
  • showElevation - 如果为false,则移除底部导航栏的阴影。
  • animationDuration - 不缺口和文本动画的时间持续时间。
  • initialIndex - 设置初始选中的项。
  • showCurvedBar - 如果为false,则移除底部导航栏的曲线。
  • showCurvedBarAnimation - 如果为false,则移除底部导航栏的曲线动画。

TopNotchItem

必须参数

  • icon - 该项的图标。
  • name - 图标下的文字。

可选参数

  • color - 项的颜色。

开始使用

pubspec.yaml文件中添加依赖:

dependencies:
  top_notch_bottom_bar: ^0.1.0

基本用法

在你的Flutter应用中添加底部导航栏:

class _HomePageState extends State<HomePage> {
  static const TextStyle txtstyle = TextStyle(fontSize: 30);

  List<Widget> screens = [
    const Center(child: Text('Home', style: txtstyle)),
    const Center(child: Text('Favourite', style: txtstyle)),
    const Center(child: Text('Messages', style: txtstyle)),
    const Center(child: Text('Settings', style: txtstyle)),
  ];

  int index = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Top Notch Bottom Bar')),
      body: screens[index],
      bottomNavigationBar: TopNotchBottomBar(
          height: 56, // 改变底部栏的高度 -> 默认 = 50
          onTap: ((value) => setState(() {
                index = value;
              })),
          items: [
            TopNotchItem(icon: const Icon(Icons.home), name: 'Home'),
            TopNotchItem(icon: const Icon(Icons.favorite), name: 'Favourite'),
            TopNotchItem(icon: const Icon(Icons.message), name: 'Messages'),
            TopNotchItem(icon: const Icon(Icons.settings), name: 'Settings'),
          ]),
    );
  }
}

示例代码

以下是完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      debugShowCheckedModeBanner: false,
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  static const TextStyle txtstyle = TextStyle(fontSize: 30);

  List<Widget> screens = [
    const Center(child: Text('Home', style: txtstyle)),
    const Center(child: Text('Favourite', style: txtstyle)),
    const Center(child: Text('Messages', style: txtstyle)),
    const Center(child: Text('Settings', style: txtstyle)),
  ];

  int index = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Top Notch Bottom Bar'),
        backgroundColor: Colors.cyan,
      ),
      body: screens[index],
      bottomNavigationBar: TopNotchBottomBar(
          backgroundColor: Colors.cyan,
          inActiveColor: Colors.black38,
          activeColor: Colors.white,
          height: 56, // 改变底部栏的高度 -> 默认 = 50
          onTap: ((value) => setState(() {
                index = value;
              })),
          items: [
            TopNotchItem(icon: const Icon(Icons.home), name: 'Home'),
            TopNotchItem(icon: const Icon(Icons.favorite), name: 'Favourite'),
            TopNotchItem(icon: const Icon(Icons.message), name: 'Messages'),
            TopNotchItem(icon: const Icon(Icons.settings), name: 'Settings'),
          ]),
    );
  }
}

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

1 回复

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


top_notch_bottom_bar 是一个用于 Flutter 的底部导航栏插件,它支持刘海屏和凹槽屏的适配,确保底部导航栏在各种设备上都能正确显示。以下是使用 top_notch_bottom_bar 的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 top_notch_bottom_bar 依赖:

dependencies:
  flutter:
    sdk: flutter
  top_notch_bottom_bar: ^1.0.0  # 请使用最新版本

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

2. 导入包

在需要使用底部导航栏的 Dart 文件中导入 top_notch_bottom_bar 包:

import 'package:top_notch_bottom_bar/top_notch_bottom_bar.dart';

3. 使用 TopNotchBottomBar

ScaffoldbottomNavigationBar 中使用 TopNotchBottomBar

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

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

  final List<Widget> _pages = [
    Page1(),
    Page2(),
    Page3(),
  ];

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_selectedIndex],
      bottomNavigationBar: TopNotchBottomBar(
        items: const <TopNotchBottomBarItem>[
          TopNotchBottomBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          TopNotchBottomBarItem(
            icon: Icon(Icons.business),
            label: 'Business',
          ),
          TopNotchBottomBarItem(
            icon: Icon(Icons.school),
            label: 'School',
          ),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
      ),
    );
  }
}

4. 自定义样式

你可以通过 TopNotchBottomBar 的构造函数来自定义底部导航栏的样式:

bottomNavigationBar: TopNotchBottomBar(
  items: const <TopNotchBottomBarItem>[
    TopNotchBottomBarItem(
      icon: Icon(Icons.home),
      label: 'Home',
    ),
    TopNotchBottomBarItem(
      icon: Icon(Icons.business),
      label: 'Business',
    ),
    TopNotchBottomBarItem(
      icon: Icon(Icons.school),
      label: 'School',
    ),
  ],
  currentIndex: _selectedIndex,
  onTap: _onItemTapped,
  backgroundColor: Colors.blue, // 背景颜色
  selectedItemColor: Colors.white, // 选中项颜色
  unselectedItemColor: Colors.grey, // 未选中项颜色
  elevation: 10.0, // 阴影
  notchMargin: 10.0, // 凹槽边距
  notchColor: Colors.blue, // 凹槽颜色
  showNotch: true, // 是否显示凹槽
),
回到顶部