Flutter导航视图插件navigation_view的使用
Flutter导航视图插件navigation_view的使用
NavigationView (1.0.6)
NavigationView 是一个 Flutter 插件,提供了一个可自定义颜色和图标的导航组件。它允许你轻松创建带有页面切换动画的导航栏。
安装
要在项目中使用 NavigationView 插件,首先需要在 pubspec.yaml
文件中添加依赖:
dependencies:
navigation_view: ^1.0.6
然后运行 flutter pub get
来获取插件。
使用方法
-
导入包
在 Dart 文件中导入 NavigationView 包:
import 'package:navigation_view/navigation_view.dart';
-
创建 NavigationView 组件
创建一个
NavigationView
组件,并提供必要的参数:NavigationView( onChangePage: (index) { // 页面切换时的回调函数 print("Selected page: $index"); }, curve: Curves.easeInBack, // 动画曲线 durationAnimation: const Duration(milliseconds: 400), // 动画持续时间 items: [ ItemNavigationView( childAfter: const Icon(Icons.home_rounded, color: Colors.blue, size: 30), childBefore: Icon(Icons.home_outlined, color: Colors.grey.withAlpha(60), size: 30), ), ItemNavigationView( childAfter: const Icon(Icons.widgets_rounded, color: Colors.blue, size: 30), childBefore: Icon(Icons.now_widgets_outlined, color: Colors.grey.withAlpha(60), size: 30), ), ItemNavigationView( childAfter: const Icon(Icons.wifi, color: Colors.blue, size: 30), childBefore: Icon(Icons.wifi_lock, color: Colors.grey.withAlpha(60), size: 30), ), ItemNavigationView( childAfter: const Icon(Icons.accessible, color: Colors.blue, size: 30), childBefore: Icon(Icons.not_accessible, color: Colors.grey.withAlpha(60), size: 30), ), ], )
onChangePage
:页面切换时的回调函数,接收当前选中的索引。curve
:设置动画的曲线效果。durationAnimation
:设置动画的持续时间。items
:导航项列表,每个项是一个ItemNavigationView
,包含两个子组件childAfter
和childBefore
,分别表示选中和未选中状态下的显示内容。
完整示例
以下是一个完整的示例代码,展示了如何在应用中使用 NavigationView
:
import 'package:flutter/material.dart';
import 'package:navigation_view/item_navigation_view.dart';
import 'package:navigation_view/navigation_view.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Navigation',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'PuzzleTak NavigationView'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Color> colors = [Colors.blue, Colors.red, Colors.amber, Colors.green];
Color color = Colors.blue;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
backgroundColor: Colors.white.withAlpha(235),
body: Padding(
padding: const EdgeInsets.only(left: 10, right: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 第一个 NavigationView
NavigationView(
onChangePage: (index) {
setState(() {
color = colors[index];
});
},
durationAnimation: const Duration(milliseconds: 600),
color: color,
items: [
ItemNavigationView(
childAfter: const Icon(Icons.home_rounded, color: Colors.blue, size: 30),
childBefore: Icon(Icons.home_outlined, color: Colors.grey.withAlpha(60), size: 30)),
ItemNavigationView(
childAfter: const Icon(Icons.widgets_rounded, color: Colors.red, size: 30),
childBefore: Icon(Icons.now_widgets_outlined, color: Colors.grey.withAlpha(60), size: 30)),
ItemNavigationView(
childAfter: const Icon(Icons.wifi, color: Colors.amber, size: 30),
childBefore: Icon(Icons.wifi_lock, color: Colors.grey.withAlpha(60), size: 30)),
ItemNavigationView(
childAfter: const Icon(Icons.accessible, color: Colors.green, size: 30),
childBefore: Icon(Icons.not_accessible, color: Colors.grey.withAlpha(60), size: 30)),
],
),
const SizedBox(height: 15),
// 第二个 NavigationView
NavigationView(
onChangePage: (index) {},
curve: Curves.easeInBack,
durationAnimation: const Duration(milliseconds: 400),
items: [
ItemNavigationView(
childAfter: const Icon(Icons.home_rounded, color: Colors.blue, size: 30),
childBefore: Icon(Icons.home_outlined, color: Colors.grey.withAlpha(60), size: 30)),
ItemNavigationView(
childAfter: const Icon(Icons.widgets_rounded, color: Colors.blue, size: 30),
childBefore: Icon(Icons.now_widgets_outlined, color: Colors.grey.withAlpha(60), size: 30)),
ItemNavigationView(
childAfter: const Icon(Icons.wifi, color: Colors.blue, size: 30),
childBefore: Icon(Icons.wifi_lock, color: Colors.grey.withAlpha(60), size: 30)),
ItemNavigationView(
childAfter: const Icon(Icons.accessible, color: Colors.blue, size: 30),
childBefore: Icon(Icons.not_accessible, color: Colors.grey.withAlpha(60), size: 30)),
],
),
const SizedBox(height: 15),
// 第三个 NavigationView
NavigationView(
onChangePage: (index) {},
color: Colors.pink,
curve: Curves.easeInQuint,
durationAnimation: const Duration(milliseconds: 300),
borderRadius: BorderRadius.circular(50),
gradient: LinearGradient(
colors: [
Colors.white.withAlpha(0),
Colors.white.withOpacity(0.2)
],
begin: const FractionalOffset(0.0, 0.0),
end: const FractionalOffset(0.0, 1.0),
stops: const [0.0, 1.0],
tileMode: TileMode.clamp),
items: [
ItemNavigationView(
childAfter: const Text("widget", style: TextStyle(color: Colors.pink)),
childBefore: Container(width: 60, height: 20, color: Colors.grey.withAlpha(50))),
ItemNavigationView(
childAfter: const Icon(Icons.widgets_rounded, color: Colors.pink, size: 30),
childBefore: Icon(Icons.now_widgets_outlined, color: Colors.grey.withAlpha(60), size: 30)),
ItemNavigationView(
childAfter: const Icon(Icons.wifi, color: Colors.blue, size: 30),
childBefore: const Text("widget", style: TextStyle(color: Colors.grey))),
],
),
const SizedBox(height: 15),
// 第四个 NavigationView
NavigationView(
onChangePage: (index) {},
color: Colors.deepPurpleAccent,
curve: Curves.easeInCubic,
durationAnimation: const Duration(milliseconds: 350),
items: [
ItemNavigationView(
childAfter: const Icon(Icons.wifi, color: Colors.deepPurpleAccent, size: 25),
childBefore: Text("Wifi", style: TextStyle(color: Colors.grey.withAlpha(100)))),
ItemNavigationView(
childAfter: const Icon(Icons.widgets_rounded, color: Colors.deepPurpleAccent, size: 25),
childBefore: Text("Apps", style: TextStyle(color: Colors.grey.withAlpha(100)))),
ItemNavigationView(
childAfter: const Icon(Icons.alarm, color: Colors.deepPurpleAccent, size: 25),
childBefore: Text("Alarm", style: TextStyle(color: Colors.grey.withAlpha(100)))),
ItemNavigationView(
childAfter: const Icon(Icons.account_box, color: Colors.deepPurpleAccent, size: 25),
childBefore: Text("Profile", style: TextStyle(color: Colors.grey.withAlpha(100)))),
],
),
],
),
),
);
}
}
更多关于Flutter导航视图插件navigation_view的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复