Flutter底部导航栏插件awesome_floating_bottom_navigation的使用
Flutter底部导航栏插件awesome_floating_bottom_navigation的使用
安装
在pubspec.yaml
文件中添加以下依赖:
dependencies:
awesome_floating_bottom_navigation: ^0.0.1
使用
首先,确保已经安装了awesome_floating_bottom_navigation
插件。接下来,我们可以通过以下方式使用它:
import 'package:awesome_floating_bottom_navigation/awesome_floating_bottom_navigation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'AwesomeFloatingBottomNavigation Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'AwesomeFloatingBottomNavigation Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
final iconList = [
Icons.dashboard,
Icons.search,
Icons.shopping_bag,
Icons.qr_code,
Icons.account_circle
];
PageController pageController = PageController();
[@override](/user/override)
void initState() {
super.initState();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
extendBody: true,
appBar: AppBar(
backgroundColor: Colors.black87,
title: Text(
widget.title,
style: const TextStyle(color: Colors.white),
),
),
body: PageView(
controller: pageController,
onPageChanged: (index) {
setState(() {
pageController.jumpToPage(index);
});
},
children: const [
NavigationScreen(Icons.dashboard),
NavigationScreen(Icons.search),
NavigationScreen(Icons.shopping_bag),
NavigationScreen(Icons.qr_code),
NavigationScreen(Icons.account_circle),
],
),
bottomNavigationBar: AwesomeFloatingBottomNavigation.builder(
itemCount: iconList.length,
tabBuilder: (int index, bool isActive) {
final color = isActive ? Colors.green : Colors.grey;
return Center(
child: Icon(
iconList[index],
size: 24,
color: color,
),
);
},
backgroundColor: Colors.black87,
activeIndex: pageController.hasClients ? pageController.page?.round() ?? 0 : 0,
splashColor: Colors.green.shade400,
splashSpeedInMilliseconds: 300,
cornerRadius: 32,
onTap: (index) => setState(() {
pageController.jumpToPage(index);
}),
padding: const EdgeInsets.all(16),
leftAndRightBonusPadding: 48,
shadow: const BoxShadow(
offset: Offset(0, 1),
blurRadius: 12,
spreadRadius: 0.5,
color: Colors.grey,
),
navigationBarType: NavigationBarType.center,
),
);
}
}
class NavigationScreen extends StatefulWidget {
final IconData iconData;
const NavigationScreen(this.iconData, {super.key});
[@override](/user/override)
State<NavigationScreen> createState() => _NavigationScreenState();
}
class _NavigationScreenState extends State<NavigationScreen> with TickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> animation;
[@override](/user/override)
void didUpdateWidget(NavigationScreen oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.iconData != widget.iconData) {
_startAnimation();
}
}
[@override](/user/override)
void initState() {
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1000),
);
animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeIn,
);
_controller.forward();
super.initState();
}
_startAnimation() {
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1000),
);
animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeIn,
);
_controller.forward();
}
[@override](/user/override)
void dispose() {
_controller.dispose();
super.dispose();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Container(
color: Theme.of(context).colorScheme.background,
child: ListView(
children: [
const SizedBox(height: 64),
Center(
child: Icon(
widget.iconData,
color: Colors.green,
size: 160,
),
),
],
),
);
}
}
更多关于Flutter底部导航栏插件awesome_floating_bottom_navigation的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter底部导航栏插件awesome_floating_bottom_navigation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter项目中使用awesome_floating_bottom_navigation
插件的示例代码。这个插件允许你创建一个具有浮动效果的底部导航栏。
首先,确保你已经在pubspec.yaml
文件中添加了awesome_floating_bottom_navigation
依赖:
dependencies:
flutter:
sdk: flutter
awesome_floating_bottom_navigation: ^latest_version # 请替换为最新版本号
然后运行flutter pub get
来安装依赖。
接下来,你可以在你的Flutter项目中按照以下步骤使用这个插件:
- 导入必要的包:
import 'package:flutter/material.dart';
import 'package:awesome_floating_bottom_navigation/awesome_floating_bottom_navigation.dart';
- 定义页面:
创建一些简单的页面来展示导航效果。例如:
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home')),
body: Center(child: Text('Home Page')),
);
}
}
class SearchPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Search')),
body: Center(child: Text('Search Page')),
);
}
}
// 可以根据需要添加更多页面
- 设置底部导航栏:
在你的主应用文件中(通常是main.dart
),配置AwesomeFloatingBottomNavigation
:
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _currentIndex = 0;
final List<FloatingActionData> _actions = [
FloatingActionData(
iconData: Icons.home,
title: 'Home',
),
FloatingActionData(
iconData: Icons.search,
title: 'Search',
),
// 可以根据需要添加更多导航项
];
final List<Widget> _screens = [
HomePage(),
SearchPage(),
// 可以根据需要添加更多页面
];
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: _screens[_currentIndex],
floatingActionButton: AwesomeFloatingActionButton(
items: _actions,
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
),
),
);
}
}
在这个例子中,我们定义了一个包含两个导航项的底部导航栏(Home和Search)。当用户点击某个导航项时,会切换到相应的页面。
AwesomeFloatingActionButton
组件接收一个items
列表,其中每个FloatingActionData
对象包含一个图标和一个标题。当用户点击某个浮动动作按钮时,onTap
回调会被触发,你可以在这里更新当前选中的索引,从而切换页面。
这样,你就成功地在Flutter项目中集成了awesome_floating_bottom_navigation
插件,并创建了一个具有浮动效果的底部导航栏。