Flutter底部导航栏插件circular_bottom_navigation的使用
Flutter底部导航栏插件circular_bottom_navigation的使用
Circular Bottom Navigation (或可能是一个标签栏).
这个插件是根据 Uplabs 上的一个艺术作品实现的。
开始使用
1 - 安装和导入
在你的Dart代码中,你可以使用以下语句:
import 'package:circular_bottom_navigation/circular_bottom_navigation.dart';
import 'package:circular_bottom_navigation/tab_item.dart';
2 - 快速参考
3 - 简单上手
创建TabItems
List<TabItem> tabItems = List.of([
TabItem(Icons.home, "Home", Colors.blue, labelStyle: TextStyle(fontWeight: FontWeight.normal)),
TabItem(Icons.search, "Search", Colors.orange, labelStyle: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
TabItem(Icons.layers, "Reports", Colors.red, circleStrokeColor: Colors.black),
TabItem(Icons.notifications, "Notifications", Colors.cyan),
]);
使用此组件
CircularBottomNavigation(
tabItems,
selectedCallback: (int selectedPos) {
print("clicked on $selectedPos");
},
)
CircularBottomNavigation
支持RTL设计。如果你将此组件包裹在一个 Directionality
组件中并设置 textDirection
属性,你就可以自定义方向:
MaterialApp(
title: 'Circular Bottom Navigation Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Directionality(
// 使用此属性更改整个应用程序的方向
// CircularBottomNavigation 将相应地工作
textDirection: TextDirection.rtl,
child: MyHomePage(title: 'circular_bottom_navigation'),
),
);
如何使用CircularBottomNavigationController
通过此控制器,您可以读取当前选项卡位置,并且可以在不重建树的情况下设置选项卡位置,并带有组件内置的动画效果。
创建一个新的控制器实例:
CircularBottomNavigationController _navigationController =
new CircularBottomNavigationController(selectedPos);
然后将其传递给你的组件:
CircularBottomNavigation(
tabItems,
controller: _navigationController,
);
现在,您可以在任何地方写入(设置新位置带动画)和读取值:
// 写入一个新值
_navigationController.value = 0;
// 读取最新值
int latest = _navigationController.value;
示例代码
以下是完整的示例代码:
import 'package:circular_bottom_navigation/tab_item.dart';
import 'package:flutter/material.dart';
import 'package:circular_bottom_navigation/circular_bottom_navigation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Circular Bottom Navigation Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Directionality(
// 使用此属性更改整个应用程序的方向
// CircularBottomNavigation 将相应地工作
textDirection: TextDirection.ltr,
child: MyHomePage(title: 'circular_bottom_navigation'),
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({
Key? key,
this.title,
}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int selectedPos = 0;
double bottomNavBarHeight = 60;
List<TabItem> tabItems = List.of([
TabItem(
Icons.home,
"Home",
Colors.blue,
labelStyle: TextStyle(
fontWeight: FontWeight.normal,
),
),
TabItem(
Icons.search,
"Search",
Colors.orange,
labelStyle: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
TabItem(
Icons.layers,
"Reports",
Colors.red,
circleStrokeColor: Colors.black,
),
TabItem(
Icons.notifications,
"Notifications",
Colors.cyan,
),
]);
late CircularBottomNavigationController _navigationController;
@override
void initState() {
super.initState();
_navigationController = CircularBottomNavigationController(selectedPos);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Padding(
child: bodyContainer(),
padding: EdgeInsets.only(bottom: bottomNavBarHeight),
),
Align(alignment: Alignment.bottomCenter, child: bottomNav())
],
),
);
}
Widget bodyContainer() {
Color? selectedColor = tabItems[selectedPos].circleColor;
String slogan;
switch (selectedPos) {
case 0:
slogan = "Family, Happiness, Food";
break;
case 1:
slogan = "Find, Check, Use";
break;
case 2:
slogan = "Receive, Review, Rip";
break;
case 3:
slogan = "Noise, Panic, Ignore";
break;
default:
slogan = "";
break;
}
return GestureDetector(
child: Container(
width: double.infinity,
height: double.infinity,
color: selectedColor,
child: Center(
child: Text(
slogan,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
),
onTap: () {
if (_navigationController.value == tabItems.length - 1) {
_navigationController.value = 0;
} else {
_navigationController.value = _navigationController.value! + 1;
}
},
);
}
Widget bottomNav() {
return CircularBottomNavigation(
tabItems,
controller: _navigationController,
selectedPos: selectedPos,
barHeight: bottomNavBarHeight,
// 使用barBackgroundColor或barBackgroundGradient来为bar背景添加渐变
barBackgroundColor: Colors.white,
// barBackgroundGradient: LinearGradient(
// begin: Alignment.bottomCenter,
// end: Alignment.topCenter,
// colors: [
// Colors.blue,
// Colors.red,
// ],
// ),
backgroundBoxShadow: <BoxShadow>[
BoxShadow(color: Colors.black45, blurRadius: 10.0),
],
animationDuration: Duration(milliseconds: 300),
selectedCallback: (int? selectedPos) {
setState(() {
this.selectedPos = selectedPos ?? 0;
print(_navigationController.value);
});
},
);
}
@override
void dispose() {
super.dispose();
_navigationController.dispose();
}
}
希望这段内容能帮助你在Flutter项目中成功使用 circular_bottom_navigation
插件。如果有任何问题或需要进一步的帮助,请随时提问!
更多关于Flutter底部导航栏插件circular_bottom_navigation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter底部导航栏插件circular_bottom_navigation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter中使用circular_bottom_navigation
插件实现底部导航栏的示例代码。这个插件提供了一个圆形的底部导航栏,可以让你的应用界面看起来更加现代和美观。
首先,确保你已经在pubspec.yaml
文件中添加了circular_bottom_navigation
依赖:
dependencies:
flutter:
sdk: flutter
circular_bottom_navigation: ^x.y.z # 请替换为最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Dart文件中,你可以按照以下方式使用circular_bottom_navigation
:
import 'package:flutter/material.dart';
import 'package:circular_bottom_navigation/circular_bottom_navigation.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Circular Bottom Navigation Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CircularBottomNavigationDemo(),
);
}
}
class CircularBottomNavigationDemo extends StatefulWidget {
@override
_CircularBottomNavigationDemoState createState() => _CircularBottomNavigationDemoState();
}
class _CircularBottomNavigationDemoState extends State<CircularBottomNavigationDemo> {
int _currentIndex = 0;
final List<CircularBottomNavigationItem> _items = [
CircularBottomNavigationItem(
icon: Icons.home,
title: "Home",
),
CircularBottomNavigationItem(
icon: Icons.search,
title: "Search",
),
CircularBottomNavigationItem(
icon: Icons.library_books,
title: "Library",
),
CircularBottomNavigationItem(
icon: Icons.person,
title: "Profile",
),
];
final List<Widget> _pages = [
HomeScreen(),
SearchScreen(),
LibraryScreen(),
ProfileScreen(),
];
void _onItemSelected(int index) {
setState(() {
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _pages[_currentIndex],
bottomNavigationBar: CircularBottomNavigation(
backgroundColor: Colors.white,
fixedColor: Colors.blue,
activeColor: Colors.blueAccent,
inactiveColor: Colors.grey,
items: _items,
currentIndex: _currentIndex,
onSelected: _onItemSelected,
),
);
}
}
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 LibraryScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Text('Library Screen'),
);
}
}
class ProfileScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Text('Profile Screen'),
);
}
}
在这个示例中,我们创建了一个包含四个导航项的圆形底部导航栏。每个导航项都关联一个不同的屏幕(HomeScreen
, SearchScreen
, LibraryScreen
, ProfileScreen
)。当用户点击不同的导航项时,会相应地切换到对应的屏幕。
CircularBottomNavigation
组件的主要属性包括:
backgroundColor
:底部导航栏的背景颜色。fixedColor
:未选中项图标的颜色。activeColor
:选中项图标的颜色。inactiveColor
:未选中项标题的颜色(如果有的话)。items
:导航项的列表。currentIndex
:当前选中的导航项的索引。onSelected
:选中项改变时的回调函数。
这个示例展示了如何使用circular_bottom_navigation
插件来创建一个简洁而美观的底部导航栏。你可以根据需要进一步自定义和扩展这个示例。