Flutter底部导航栏插件classic_bottom_navigation_bar的使用
Flutter底部导航栏插件classic_bottom_navigation_bar的使用
通过使用此插件,您可以在Flutter中轻松创建一个美观的底部导航栏,并在不同的操作系统上使用它。以下是关于如何使用它的文档。
示例动画
如何使用
在Scaffold中设置底部导航栏
Scaffold(
bottomNavigationBar: ClassicButtomNavBar(
items: const [
ClassicBottomNavyBarItem(),
ClassicBottomNavyBarItem(),
ClassicBottomNavyBarItem(),
...
],
),
);
ClassicButtomNavBar的参数
参数名称 | 类型 | 描述 |
---|---|---|
backgroundColor |
Color |
可选。容器的颜色 |
selectedIndex |
int |
可选。当前选中页面的索引 |
iconSize |
int |
可选。底部导航栏中图标的大小 |
height |
double |
可选。经典底部导航栏的高度 |
width |
double |
可选。经典底部导航栏的宽度 |
borderRadius |
double |
可选。底部导航栏角的圆角半径 |
items |
List<ClassicBottomNavyBarItem> |
必需。底部导航栏中的项目列表 |
onItemSelected |
callbackFunction |
必需。选择项目的回调函数 |
完整示例
import 'package:classic_bottom_navigation_bar/classic_bottom_navigation_bar.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const Home(),
);
}
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
int _currentIndex = 0;
List myRoutes = [
const Center(
child: Text(
"PAGE 1",
style: TextStyle(
color: Color.fromARGB(255, 255, 255, 255),
fontSize: 50,
fontWeight: FontWeight.bold,
),
),
),
const Center(
child: Text(
"PAGE 2",
style: TextStyle(
color: Colors.white,
fontSize: 50,
fontWeight: FontWeight.bold,
),
),
),
const Center(
child: Text(
"PAGE 3",
style: TextStyle(
color: Colors.white,
fontSize: 50,
fontWeight: FontWeight.bold,
),
),
),
const Center(
child: Text(
"PAGE 4",
style: TextStyle(
color: Colors.white,
fontSize: 50,
fontWeight: FontWeight.bold,
),
),
),
];
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color.fromARGB(255, 0, 0, 0),
body: myRoutes[_currentIndex],
bottomNavigationBar: ClassicButtomNavBar(
selectedIndex: _currentIndex,
onItemSelected: (value) => setState(() {
print("🛑$_currentIndex🛑");
_currentIndex = value;
print("🟢$_currentIndex🟢");
}),
items: const [
ClassicBottomNavyBarItem(
icon: Icon(Icons.home),
title: "H O M E",
),
ClassicBottomNavyBarItem(
icon: Icon(Icons.search),
title: "S E A R C H",
),
ClassicBottomNavyBarItem(
icon: Icon(Icons.person),
title: "P R O F I L E",
),
ClassicBottomNavyBarItem(
icon: Icon(Icons.settings),
title: "S E T T I N G S",
),
],
),
);
}
}
使用方法
首先,在Scaffold中定义所需的菜单项:
Scaffold(
bottomNavigationBar: ClassicButtomNavBar(
items: const [
ClassicBottomNavyBarItem(),
ClassicBottomNavyBarItem(),
ClassicBottomNavyBarItem(),
...
],
),
);
更多关于Flutter底部导航栏插件classic_bottom_navigation_bar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter底部导航栏插件classic_bottom_navigation_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中使用classic_bottom_navigation_bar
插件的示例代码。这个插件可以帮助你轻松地在应用中实现底部导航栏。
首先,确保你已经在pubspec.yaml
文件中添加了classic_bottom_navigation_bar
依赖:
dependencies:
flutter:
sdk: flutter
classic_bottom_navigation_bar: ^x.y.z # 请替换为最新版本号
然后运行flutter pub get
来安装依赖。
接下来,你可以在你的Flutter应用中实现底部导航栏。以下是一个完整的示例代码:
import 'package:flutter/material.dart';
import 'package:classic_bottom_navigation_bar/classic_bottom_navigation_bar.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _selectedIndex = 0;
final List<Widget> _widgetOptions = <Widget>[
Text('Home Screen'),
Text('Search Screen'),
Text('Profile Screen'),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Classic Bottom Navigation Bar Demo'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: ClassicBottomNavigationBar(
items: [
ClassicBottomNavigationBarItem(
icon: Icon(Icons.home),
title: 'Home',
),
ClassicBottomNavigationBarItem(
icon: Icon(Icons.search),
title: 'Search',
),
ClassicBottomNavigationBarItem(
icon: Icon(Icons.person),
title: 'Profile',
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
animationDuration: Duration(milliseconds: 300),
backgroundColor: Colors.white,
activeColor: Colors.blue,
inactiveColor: Colors.grey,
),
);
}
}
代码解释:
-
依赖导入:
- 导入
flutter/material.dart
和classic_bottom_navigation_bar
包。
- 导入
-
主应用:
MyApp
是一个StatelessWidget
,它定义了应用的主题和主页面。
-
主页面:
MyHomePage
是一个StatefulWidget
,它有一个状态类_MyHomePageState
。_selectedIndex
变量用于跟踪当前选中的导航项。_widgetOptions
列表包含了不同导航项对应的页面内容(这里简单使用Text
组件作为示例)。
-
底部导航栏:
- 使用
ClassicBottomNavigationBar
来创建底部导航栏。 items
属性定义了导航项的图标和标题。currentIndex
属性绑定到_selectedIndex
变量。onTap
回调用于处理导航项点击事件。- 其他属性如
animationDuration
、backgroundColor
、activeColor
和inactiveColor
用于自定义导航栏的样式。
- 使用
这个示例展示了如何使用classic_bottom_navigation_bar
插件来创建一个具有三个导航项的底部导航栏,并根据用户的选择显示不同的内容。你可以根据自己的需求进一步自定义和扩展这个示例。