Flutter模糊导航栏插件blured_navigation_bar_x的使用
Flutter模糊导航栏插件blured_navigation_bar_x的使用
blured_navigation_bar_x
是一个适用于 Flutter 的底部导航视图库。它支持多个平台,包括 Android、iOS、Windows、Linux、macOS 和 Web。
支持的平台
- Android
- iOS
- Windows
- Linux
- MacOS
- Web
自适应布局
带边框的导航栏
默认样式
显示选中的标签
如何使用
首先,在 pubspec.yaml
文件中添加依赖项:
dependencies:
blured_navigation_bar_x: ^1.0.3
然后在 Dart 文件中导入库:
import 'package:blured_navigation_bar_x/blured_navigation_bar_x.dart';
接下来,您可以创建一个简单的示例应用来展示如何使用 blured_navigation_bar_x
插件。
// ignore_for_file: library_private_types_in_public_api
import 'package:blured_navigation_bar_x/blured_nav_bar_x_item.dart';
import 'package:blured_navigation_bar_x/blured_navigation_bar_x.dart';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _index = 0;
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: "Blured Navigation Bar X",
home: Scaffold(
appBar: AppBar(
title: const Text('Blured Navigation Bar X'),
centerTitle: true,
),
// 如果希望导航栏后面的主体部分显示背景,应设置为 true
extendBody: true,
body: Container(
height: double.infinity,
width: double.infinity,
child: Center(child: Screen(index: _index)),
),
bottomNavigationBar: BluredNavigationBarX(
currentIndex: _index,
labelStatus: LabelStatus.showSelected,
backgroundColor: Colors.black87.withOpacity(.2),
browColor: Colors.red,
border: Border.all(width: 3, color: Colors.red),
unselectedItemColor: Colors.grey,
selectedItemColor: Colors.red,
items: [
BluredNavBarXItem(icon: Icons.home, title: 'Home'),
BluredNavBarXItem(icon: Icons.favorite, title: 'Favorite'),
BluredNavBarXItem(icon: Icons.message, title: 'Message'),
],
onPressed: (int v) {
setState(() {
_index = v;
});
},
),
),
);
}
}
class Screen extends StatefulWidget {
final int index;
const Screen({Key? key, required this.index}) : super(key: key);
[@override](/user/override)
State<Screen> createState() => _ScreenState();
}
class _ScreenState extends State<Screen> {
[@override](/user/override)
Widget build(BuildContext context) {
return SizedBox(
width: MediaQuery.maybeOf(context)?.size.width,
height: MediaQuery.maybeOf(context)?.size.height,
child: Image.asset(
"assets/images/fon${widget.index + 1}.png",
fit: BoxFit.fill,
),
);
}
}
更多关于Flutter模糊导航栏插件blured_navigation_bar_x的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter模糊导航栏插件blured_navigation_bar_x的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用blured_navigation_bar_x
插件来实现模糊导航栏效果的代码示例。首先,你需要确保已经在pubspec.yaml
文件中添加了该插件的依赖:
dependencies:
flutter:
sdk: flutter
blured_navigation_bar_x: ^最新版本号 # 请替换为实际最新版本号
然后运行flutter pub get
来安装依赖。
以下是一个完整的示例代码,展示了如何使用blured_navigation_bar_x
插件:
import 'package:flutter/material.dart';
import 'package:blured_navigation_bar_x/blured_navigation_bar_x.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> {
final _controller = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
controller: _controller,
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.parallax,
background: Image.network(
'https://via.placeholder.com/1500x500',
fit: BoxFit.cover,
),
),
),
];
},
body: Builder(
builder: (BuildContext context) {
return Column(
children: <Widget>[
Expanded(
child: ListView.builder(
controller: _controller,
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('Item $index'),
);
},
),
),
BlurNavigationBarX(
color: Colors.transparent,
height: 70,
items: [
BlurNavigationBarItemX(
icon: Icon(Icons.home),
title: Text('Home'),
),
BlurNavigationBarItemX(
icon: Icon(Icons.search),
title: Text('Search'),
),
BlurNavigationBarItemX(
icon: Icon(Icons.account_circle),
title: Text('Profile'),
),
],
),
],
);
},
),
),
);
}
}
代码解释
-
依赖添加:确保在
pubspec.yaml
文件中添加了blured_navigation_bar_x
依赖。 -
Scaffold和NestedScrollView:使用
NestedScrollView
来嵌套一个可滚动的列表视图和一个SliverAppBar
。SliverAppBar
用于显示一个可扩展的导航栏,背景是一张图片。 -
SliverAppBar:
SliverAppBar
的flexibleSpace
属性中设置了一个FlexibleSpaceBar
,它包含一个背景图片,并设置了collapseMode
为CollapseMode.parallax
以实现视差效果。 -
BlurNavigationBarX:在
Column
的底部添加了一个BlurNavigationBarX
,并配置了三个导航项。 -
ScrollController:使用
ScrollController
来同步SliverAppBar
和ListView
的滚动状态。
注意事项
- 确保在实际使用中替换图片URL为有效的图片链接。
- 插件的具体使用方法可能会根据版本有所不同,请参考插件的官方文档和示例代码进行适当调整。
希望这个示例代码能帮助你快速上手blured_navigation_bar_x
插件的使用。如果有其他问题,欢迎继续提问!