Flutter自定义底部导航栏样式插件easy_bar_style的使用
Flutter自定义底部导航栏样式插件easy_bar_style的使用
easy_bar_style简介
什么是Easy bar Style?
easy_bar_style 是一个简单的 Flutter 小部件,用于轻松设置状态栏样式和系统栏导航样式。
为什么使用Easy bar Style?
通过使用 easy_bar_style,您无需手动调用 SystemChrome 或为每个页面单独更新系统 UI 覆盖。所有这些繁琐的逻辑都被隐藏在 StatusBarStyle 和 SystemNavigationBarStyle 小部件背后。
安装
在 pubspec.yaml 文件中添加依赖:
dependencies:
easy_bar_style: [latest-version]
然后运行以下命令安装依赖:
flutter pub get
StatusBarStyle
StatusBarStyle 用于设置状态栏的颜色和图标亮度。
参数说明:
| 参数名称 | 类型 | 描述 | Android | iOS |
|---|---|---|---|---|
| color | Color? | 状态栏颜色 | ✅ | ❌ |
| brightness | Brightness? | 状态栏图标的亮度,当为 null 时会尝试从给定的颜色推断合适的值 |
✅ | ✅ |
| maintainState | bool | 是否在返回到页面时恢复样式,默认为 true |
✅ | ✅ |
示例代码:
[@override](/user/override)
Widget build(BuildContext context) {
return StatusBarStyle(
brightness: Brightness.dark,
color: Colors.white60,
maintainState: true,
child: Container(color: Colors.red),
);
}
SystemNavigationBarStyle
SystemNavigationBarStyle 用于设置系统导航栏的颜色和图标亮度。
参数说明:
| 参数名称 | 类型 | 描述 | Android | iOS |
|---|---|---|---|---|
| color | Color? | 导航栏颜色 | ✅ | ❌ |
| dividerColor | Color? | 导航栏与应用内容之间的分隔线颜色 | ✅ | ❌ |
| iconBrightness | Brightness? | 导航栏图标的亮度,当为 null 时会尝试从给定的颜色推断合适的值 |
✅ | ❌ |
| maintainState | bool | 是否在返回到页面时恢复样式,默认为 true |
✅ | ❌ |
示例代码:
[@override](/user/override)
Widget build(BuildContext context) {
return SystemNavigationBarStyle(
brightness: Brightness.dark,
color: Colors.deepOrangeAccent,
maintainState: true,
child: Container(color: Colors.red),
);
}
EasyStyle
EasyStyle 可以同时使用 StatusBarStyle 和 SystemNavigationBarStyle,简化代码结构。
示例代码:
[@override](/user/override)
Widget build(BuildContext context) {
return EasyStyle(
styles: const [
SystemNavigationBarStyle(
color: Colors.brown,
iconBrightness: Brightness.light,
),
StatusBarStyle(
color: Colors.red,
brightness: Brightness.light,
),
],
child: Container(color: Colors.green),
);
}
完整示例
以下是一个完整的示例代码,展示了如何使用 easy_bar_style 创建一个带有不同页面样式的 Flutter 应用程序。
示例代码:
import 'package:easy_bar_style/easy_bar_style.dart';
import 'package:flutter/material.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> {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
navigatorObservers: [styleRouteObserver], // 自动管理页面样式
routes: {
'/': (context) => const HomeScreen(),
'/cart': (context) => const CartScreen(),
'/payment': (context) => const PaymentScreen(),
'/orders': (context) => const OrdersScreen(),
'/ratings': (context) => const RatingsScreen(),
'/settings': (context) => const SettingsScreen(),
},
initialRoute: '/',
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return SystemNavigationBarStyle(
color: Colors.blue,
child: StatusBarStyle(
color: Colors.deepOrangeAccent,
brightness: Brightness.light,
maintainState: true,
child: SizedBox(
child: Container(
color: Colors.amber,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
MaterialButton(
height: 120,
color: Colors.black,
onPressed: () {
Navigator.of(context).pushNamed('/cart');
},
textColor: Colors.white,
child: const Text('Cart'),
),
],
),
),
),
),
);
}
}
class CartScreen extends StatelessWidget {
const CartScreen({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return StatusBarStyle(
brightness: Brightness.dark,
color: Colors.white60,
child: Container(
color: Colors.red,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
MaterialButton(
height: 120,
color: Colors.black,
onPressed: () {
Navigator.of(context).pushNamed('/payment');
},
textColor: Colors.white,
child: const Text('Payment'),
),
],
),
),
);
}
}
class PaymentScreen extends StatelessWidget {
const PaymentScreen({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return StatusBarStyle(
color: Colors.lightGreenAccent,
child: Container(
color: Colors.lightGreenAccent,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
MaterialButton(
height: 120,
color: Colors.black,
onPressed: () {
Navigator.of(context).pushNamed('/orders');
},
textColor: Colors.white,
child: const Text('Orders'),
),
],
),
),
);
}
}
class OrdersScreen extends StatelessWidget {
const OrdersScreen({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Orders'),
backgroundColor: Colors.black,
),
backgroundColor: Colors.red,
body: Column(
children: [
MaterialButton(
height: 120,
color: Colors.black,
onPressed: () {
Navigator.of(context).pushNamed('/settings');
},
textColor: Colors.white,
child: const Text('Orders'),
),
],
),
);
}
}
class SettingsScreen extends StatelessWidget {
const SettingsScreen({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return EasyStyle(
styles: const [
SystemNavigationBarStyle(
color: Colors.brown,
iconBrightness: Brightness.light,
),
StatusBarStyle(
color: Colors.red,
brightness: Brightness.light,
),
],
child: Container(
color: Colors.green,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
MaterialButton(
height: 120,
color: Colors.black,
onPressed: () {
Navigator.of(context).pushNamed('/ratings');
},
textColor: Colors.white,
child: const Text('Settings'),
),
],
),
),
);
}
}
class RatingsScreen extends StatelessWidget {
const RatingsScreen({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return EasyStyle(
styles: const [
StatusBarStyle(
color: Colors.blue,
brightness: Brightness.light,
),
SystemNavigationBarStyle(
color: Colors.brown,
iconBrightness: Brightness.dark,
),
],
child: Scaffold(
backgroundColor: Colors.red,
body: SafeArea(
child: Column(
children: [
MaterialButton(
height: 120,
color: Colors.black,
onPressed: () {
Navigator.of(context).popUntil((route) => route.settings.name == '/');
},
textColor: Colors.white,
child: const Text('Ratings'),
),
],
),
),
),
);
}
}
更多关于Flutter自定义底部导航栏样式插件easy_bar_style的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义底部导航栏样式插件easy_bar_style的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
easy_bar_style 是一个用于 Flutter 的插件,可以帮助你快速自定义底部导航栏的样式。使用这个插件,你可以轻松地更改底部导航栏的颜色、图标、文字样式等。
安装插件
首先,你需要在 pubspec.yaml 文件中添加 easy_bar_style 插件的依赖:
dependencies:
flutter:
sdk: flutter
easy_bar_style: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get 来安装插件。
使用 easy_bar_style
下面是一个简单的示例,展示如何使用 easy_bar_style 来自定义底部导航栏。
import 'package:flutter/material.dart';
import 'package:easy_bar_style/easy_bar_style.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Easy Bar Style Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _selectedIndex = 0;
final List<Widget> _widgetOptions = [
Text('Home Page'),
Text('Search Page'),
Text('Profile Page'),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Easy Bar Style Demo'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: EasyBottomNavigationBar(
backgroundColor: Colors.white,
selectedItemColor: Colors.blue,
unselectedItemColor: Colors.grey,
selectedFontSize: 14.0,
unselectedFontSize: 12.0,
selectedIconSize: 30.0,
unselectedIconSize: 24.0,
items: const <EasyBottomNavigationBarItem>[
EasyBottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
EasyBottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Search',
),
EasyBottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
);
}
}

