Flutter底部导航高亮插件bottom_navigation_spotlight的使用

发布于 1周前 作者 sinazl 来自 Flutter

Flutter底部导航高亮插件bottom_navigation_spotlight的使用

安装

在你的 pubspec.yaml 文件中添加依赖:

dependencies:
  bottom_navigation_spotlight: ^0.2.0

然后,获取包:

flutter pub get

使用

1. 默认自定义导航

你可以创建一个带有自定义图标、标签和屏幕的底部导航栏:

import 'package:bottom_navigation_spotlight/bottom_navigation_spotlight.dart';
import 'package:flutter/material.dart';
import 'package:iconsax/iconsax.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BottomNavigationSpotlight(
        menuItems: [
          MenuItem(
            icon: Iconsax.home,
            label: 'Home',
            screen: Center(child: Text('Home Screen')),
          ),
          MenuItem(
            icon: Iconsax.search_favorite,
            label: 'Search',
            screen: Center(child: Text('Search Screen')),
          ),
          MenuItem(
            icon: Iconsax.notification,
            label: 'Notifications',
            screen: Center(child: Text('Notifications Screen')),
          ),
          MenuItem(
            icon: Iconsax.profile_tick,
            label: 'Profile',
            screen: Center(child: Text('Profile Screen')),
          ),
        ],
        backgroundColor: Colors.white,
        selectedItemColor: Colors.blue,
        unselectedItemColor: Colors.grey,
        iconSize: 24,
        textSize: 12,
        padding: 12,
      ),
    );
  }
}
2. 预定义模板

我们提供预配置的模板以更快地集成到特定的应用程序类别中:

🏥 医疗模板
import 'package:bottom_navigation_spotlight/bottom_navigation_spotlight.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BottomNavigationSpotlight.medical(),
    );
  }
}

包含的页面:

  • Dashboard
  • Appointments
  • Notifications
  • Profile
🛒 电子商务模板
import 'package:bottom_navigation_spotlight/bottom_navigation_spotlight.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BottomNavigationSpotlight.ecommerce(),
    );
  }
}

包含的页面:

  • Home
  • Categories
  • Cart
  • Profile

自定义选项

你可以微调导航栏的外观和行为:

属性 描述 默认值
menuItems MenuItem对象列表 必须
backgroundColor 导航栏背景颜色 Colors.white
selectedItemColor 选中项的颜色 Colors.blue
unselectedItemColor 未选中项的颜色 Colors.grey
iconSize 图标大小 24.0
textSize 标签文本大小 12.0
padding 项目周围的间距 8.0

自定义示例:

BottomNavigationSpotlight(
  menuItems: [
    MenuItem(icon: Iconsax.home, label: 'Home', screen: Text('Home Screen')),
    MenuItem(icon: Iconsax.profile_tick, label: 'Profile', screen: Text('Profile Screen')),
  ],
  backgroundColor: Colors.black,
  selectedItemColor: Colors.green,
  unselectedItemColor: Colors.white,
  iconSize: 30,
  textSize: 14,
  padding: 16,
)

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

1 回复

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


当然,以下是如何在Flutter项目中使用bottom_navigation_spotlight插件来实现底部导航栏高亮效果的代码示例。

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

dependencies:
  flutter:
    sdk: flutter
  bottom_navigation_spotlight: ^x.y.z  # 请使用最新版本号替换 x.y.z

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

接下来是具体的代码实现:

  1. 导入必要的包
import 'package:flutter/material.dart';
import 'package:bottom_navigation_spotlight/bottom_navigation_spotlight.dart';
  1. 创建主应用程序
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BottomNavigationSpotlightDemo(),
    );
  }
}
  1. 实现底部导航高亮效果
class BottomNavigationSpotlightDemo extends StatefulWidget {
  @override
  _BottomNavigationSpotlightDemoState createState() => _BottomNavigationSpotlightDemoState();
}

class _BottomNavigationSpotlightDemoState extends State<BottomNavigationSpotlightDemo> {
  int _selectedIndex = 0;
  final List<Widget> _widgetOptions = [
    HomeScreen(),
    SearchScreen(),
    ProfileScreen(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.search),
            label: 'Search',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: 'Profile',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.blue,
        onTap: (index) {
          setState(() {
            _selectedIndex = index;
          });
          // 显示高亮效果
          _showSpotlight(index);
        },
      ),
    );
  }

  void _showSpotlight(int index) {
    BottomNavigationSpotlight.show(
      context: context,
      container: _findBottomNavigationBar(),
      targetIndex: index,
      color: Colors.blue.withOpacity(0.8),
      radius: 24.0,
      animationDuration: 300,
    );
  }

  Widget _findBottomNavigationBar() {
    // 这里通过全局键来找到BottomNavigationBar,具体实现可以根据布局情况调整
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
    return scaffoldKey.currentState?.bottomNavigationBar;
  }
}

// 示例页面
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 ProfileScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Profile Screen'));
  }
}

注意:

  • 在上面的代码中,_findBottomNavigationBar函数试图通过全局键(GlobalKey)来找到BottomNavigationBar,但在实际项目中,由于我们直接在Scaffold中定义了bottomNavigationBar,所以这个方法可能无法直接工作。这里只是为了展示一种思路,实际项目中需要根据具体的布局结构来调整。
  • 在真实项目中,你可以通过其他方式来定位BottomNavigationBar,比如将BottomNavigationBar封装到一个单独的组件中,并通过引用该组件来找到它。

希望这个示例能够帮助你理解如何在Flutter中使用bottom_navigation_spotlight插件来实现底部导航栏的高亮效果。

回到顶部