Flutter自定义导航插件custom_navigator的使用
Flutter自定义导航插件custom_navigator的使用
Custom navigator
一个flutter包,它使得在小部件树中的任何地方创建自己的导航器变得容易。
一个常见的用途是在需要实现始终呈现底部导航栏时。
Getting Started
首先,你需要将该包添加到你的pubspec.yaml
文件中。
Custom scaffold
CustomScaffold
是一个使用CustomNavigator
来处理带有嵌套导航的BottomNavigationBar
项过渡的状态小部件,并保持BottomNavigationBar
可见!
Usage
// 这里是自定义scaffold小部件
// 它接受一个普通的scaffold,其必要的底部导航栏和子页面
CustomScaffold(
scaffold: Scaffold(
bottomNavigationBar: BottomNavigationBar(
items: _items,
),
),
// 子页面是每次点击时将显示的页面
// 它们应该按顺序放置,例如
// 当点击`BottomNavigationBar`中的`item 0`时,将显示`page 0`
children: <Widget>[
Page('0'),
Page('1'),
Page('2'),
],
// 当点击其中一个`items`时调用
onItemTap: (index) {},
);
查看自定义scaffold示例以获取更多详细信息。
Custom navigator
CustomNavigator
非常易于使用。
Usage
CustomNavigator(
home: YourChildWidget(),
// 指定您的页面路由 [PageRoutes.materialPageRoute] 或 [PageRoutes.cupertinoPageRoute]
pageRoute: PageRoutes.materialPageRoute,
);
然后你可以像往常一样使用Navigator.of(context)
来调用它。
Options
-
您可以指定命名路由,就像在
MaterialApp
中一样。如果你想使用默认的
Navigator
,你需要为MaterialApp
指定一个GlobalKey
,并在外部使用navigatorKey.currentState
。
查看示例以获取更多详细信息。
示例代码
以下是一个完整的示例代码,展示了如何使用CustomNavigator
:
import 'package:flutter/material.dart';
import 'package:custom_navigator/custom_navigator.dart';
void main() => runApp(MyApp());
// 给 Material App 提供一个 navigator key,如果你想在应用的任何地方访问默认导航器
GlobalKey<NavigatorState> mainNavigatorKey = GlobalKey<NavigatorState>();
class MyApp extends StatelessWidget {
// 这个小部件是你的应用的根
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: mainNavigatorKey,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Page _page = Page('Page 0');
int _currentIndex = 0;
// 自定义导航器需要一个全局 key,如果你想要从它的子树外部访问导航器
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
items: _items,
onTap: (index) {
// 这里我们使用导航器 key 来弹出堆栈,回到主页面
navigatorKey.currentState.maybePop();
setState(() => _page = Page('Page $index'));
_currentIndex = index;
},
currentIndex: _currentIndex,
),
body: CustomNavigator(
navigatorKey: navigatorKey,
home: _page,
// 指定页面路由 [PageRoutes.materialPageRoute] 或 [PageRoutes.cupertinoPageRoute]
pageRoute: PageRoutes.materialPageRoute,
),
);
}
final _items = [
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('home')),
BottomNavigationBarItem(icon: Icon(Icons.event), title: Text('events')),
BottomNavigationBarItem(
icon: Icon(Icons.save_alt), title: Text('downloads')),
];
}
class Page extends StatelessWidget {
final String title;
const Page(this.title) : assert(title != null);
[@override](/user/override)
Widget build(BuildContext context) {
final text = Text(title);
return Container(
child: Center(
child: FlatButton(
onPressed: () => _openDetailsPage(context), child: text)),
);
}
// 使用 .of(context) 方法像平常一样使用导航器
_openDetailsPage(BuildContext context) => Navigator.of(context).push(
MaterialPageRoute(builder: (context) => DetailsPage(title)),
);
}
class DetailsPage extends StatelessWidget {
final String title;
const DetailsPage(this.title) : assert(title != null);
[@override](/user/override)
Widget build(BuildContext context) {
final text = Text('Details of $title');
return Container(
child: Center(child: text),
);
}
}
更多关于Flutter自定义导航插件custom_navigator的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义导航插件custom_navigator的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
custom_navigator
是一个 Flutter 插件,用于在 Flutter 应用中实现自定义导航逻辑。通过这个插件,你可以更灵活地控制页面的导航行为,比如自定义页面切换动画、管理导航栈等。
下面是一个简单的示例,展示如何使用 custom_navigator
插件来实现自定义导航。
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 custom_navigator
依赖:
dependencies:
flutter:
sdk: flutter
custom_navigator: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 基本使用
import 'package:flutter/material.dart';
import 'package:custom_navigator/custom_navigator.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: CustomNavigator(
initialPage: HomePage(),
pageBuilder: (context, route) {
return route;
},
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Page'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
CustomNavigator.push(
context,
SecondPage(),
);
},
child: Text('Go to Second Page'),
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
CustomNavigator.pop(context);
},
child: Text('Go Back'),
),
),
);
}
}
3. 解释
-
CustomNavigator: 这是
custom_navigator
提供的核心组件,用于包裹整个导航系统。它接受initialPage
参数来指定初始页面,并通过pageBuilder
来构建页面。 -
CustomNavigator.push: 用于导航到新页面。它类似于
Navigator.push
,但提供了更多的自定义选项。 -
CustomNavigator.pop: 用于返回到上一个页面。它类似于
Navigator.pop
。
4. 自定义导航动画
你可以通过 CustomNavigator
的 transitionBuilder
参数来自定义页面切换动画。例如:
CustomNavigator(
initialPage: HomePage(),
pageBuilder: (context, route) {
return route;
},
transitionBuilder: (context, animation, secondaryAnimation, child) {
return SlideTransition(
position: Tween<Offset>(
begin: const Offset(1.0, 0.0),
end: Offset.zero,
).animate(CurvedAnimation(
parent: animation,
curve: Curves.easeInOut,
)),
child: child,
);
},
);