Flutter底部导航栏插件convex_bottom_app_bar的使用
Flutter底部导航栏插件convex_bottom_app_bar的使用
convex_bottom_app_bar
是一个可自定义的底部导航栏插件。以下是如何在你的Flutter项目中使用它的详细指南。
安装插件
首先,在你的 pubspec.yaml
文件中添加依赖:
dependencies:
convex_bottom_app_bar: ^1.0.1
然后运行 flutter pub get
来获取新安装的包。
导入库
在你的 Dart 文件中导入 convex_bottom_app_bar
库:
import 'package:convex_bottom_app_bar/convex_bottom_app_bar.dart';
使用示例
以下是一个完整的示例代码,展示了如何使用 convex_bottom_app_bar
插件来创建一个底部导航栏。
import 'package:flutter/material.dart';
import 'package:convex_bottom_app_bar/convex_bottom_app_bar.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int currentPage = 0;
late ConvexTabController tabController = ConvexTabController(initialIndex: 0);
void onBottomIconPressed(int index) {
setState(() {
currentPage = index;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Convex Bottom App Bar Example'),
),
extendBody: true,
resizeToAvoidBottomInset: false,
extendBodyBehindAppBar: false,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
shape: const CircleBorder(),
onPressed: () {
tabController.jumpToTab(3);
},
),
body: ConvexBody(
controller: tabController,
children: const [
TestPage('Home', Colors.red),
TestPage('Search', Colors.blue),
TestPage('Scan QR', Colors.blue),
TestPage('Cart', Colors.green),
TestPage('Favorite', Colors.deepOrange),
],
),
bottomNavigationBar: ConvexBottomAppBarV2(
controller: tabController,
backgroundColor: Colors.white,
selectedColor: CupertinoColors.activeBlue,
unSelectedColor: CupertinoColors.inactiveGray,
indicatorColor: CupertinoColors.activeBlue,
items: _items(),
),
),
);
}
List<ConvexBottomAppBarItem> _items() {
return [
ConvexBottomAppBarItem(
icon: const Icon(Icons.home),
title: 'Home',
),
ConvexBottomAppBarItem(
icon: const Icon(Icons.search),
title: 'Search',
),
ConvexBottomAppBarItem(
title: 'Scan QR',
textStyle: const TextStyle(
fontWeight: FontWeight.w500,
fontSize: 11,
color: Color(0xFFA6A6A6))),
ConvexBottomAppBarItem(
icon: const Icon(Icons.card_travel),
title: 'Travel',
),
ConvexBottomAppBarItem(
icon: const Icon(Icons.favorite_border),
title: 'Fav',
),
];
}
}
// 假设有一个TestPage组件用于显示不同的页面
class TestPage extends StatelessWidget {
final String title;
final Color color;
TestPage(this.title, this.color);
[@override](/user/override)
Widget build(BuildContext context) {
return Center(
child: Text(title, style: TextStyle(color: color)),
);
}
}
更多关于Flutter底部导航栏插件convex_bottom_app_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter底部导航栏插件convex_bottom_app_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个使用 convex_bottom_app_bar
插件在 Flutter 中实现底部导航栏的示例代码。这个插件提供了一个美观且灵活的底部导航栏,可以很好地集成到你的应用中。
首先,确保你的 pubspec.yaml
文件中已经添加了 convex_bottom_app_bar
依赖:
dependencies:
flutter:
sdk: flutter
convex_bottom_app_bar: ^3.0.0 # 请根据需要调整版本号
然后,运行 flutter pub get
来获取依赖。
接下来是完整的 Flutter 应用代码示例,展示了如何使用 convex_bottom_app_bar
:
import 'package:flutter/material.dart';
import 'package:convex_bottom_app_bar/convex_bottom_app_bar.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Convex Bottom App Bar Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
int _selectedIndex = 0;
final List<Widget> _widgetOptions = <Widget>[
Text('Home Page'),
Text('Search Page'),
Text('Profile Page'),
Text('Settings Page'),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Convex Bottom App Bar Demo'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Add',
child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: ConvexAppBar(
items: [
{
"icon": Icons.home,
"title": "Home",
},
{
"icon": Icons.search,
"title": "Search",
},
{
"icon": Icons.person,
"title": "Profile",
},
{
"icon": Icons.settings,
"title": "Settings",
},
].map<ConvexAppBarItem>((item) {
return ConvexAppBarItem(
icon: item["icon"],
title: item["title"],
activeColor: Colors.blue,
inactiveColor: Colors.grey,
onPressed: () {
_onItemTapped(
_widgetOptions.indexOf(
_widgetOptions.firstWhere((widget) => widget.toString().contains(item["title"]!),),
),
);
},
);
}).toList(),
activeColor: Colors.blue,
inactiveColor: Colors.grey,
height: 60.0,
backgroundColor: Colors.white,
),
);
}
}
代码解释:
- 依赖引入:在
pubspec.yaml
中引入convex_bottom_app_bar
依赖。 - MaterialApp:创建一个基本的 Flutter 应用。
- StatefulWidget:使用
StatefulWidget
来管理底部导航栏的选中状态。 - _widgetOptions:一个包含不同页面的 Widget 列表。
- _onItemTapped:更新当前选中项的索引。
- Scaffold:包含应用的主要结构,包括
AppBar
、body
、floatingActionButton
和bottomNavigationBar
。 - ConvexAppBar:使用
ConvexAppBar
创建底部导航栏,并配置每个导航项的图标、标题、颜色以及点击事件。
这样,你就可以在你的 Flutter 应用中使用 convex_bottom_app_bar
来实现一个美观的底部导航栏了。