Flutter学习记录——10.导航组件

分手后的思念是犯贱 2023-06-19 02:53 4阅读 0赞

文章目录

  • 1.TabBar和TabBarView Widget
  • 2.BottomNavigationBar Widget
  • 3.CupertinoTabBar和PageView Widget
  • 4.BottomAppBar Widget
  • 5.总结

1.TabBar和TabBarView Widget

TabBar 和 TabBarView 一般是搭配使用,TabBar 用来实现 Tab 导航部分,TabBarView 用来实现 body 内容区域部分。TabBar 继承自 StatefulWidget,是一个有状态组件。TabBarView 同样也是继承自 StatefulWidget。

来看下 TabBar 的构造方法:

  1. const TabBar({
  2. Key key,
  3. // tab页Widget集合,可以使用Tab组件或者其他组件
  4. @required this.tabs,
  5. // TabController对象,控制tab页
  6. this.controller,
  7. // 是否可滚动
  8. this.isScrollable = false,
  9. // 指示器颜色
  10. this.indicatorColor,
  11. // 指示器高度
  12. this.indicatorWeight = 2.0,
  13. // 底部指示器的Padding
  14. this.indicatorPadding = EdgeInsets.zero,
  15. // 指示器装饰器decoration,例如加边框
  16. this.indicator,
  17. // 指示器大小计算方式,TabBarIndicatorSize.label跟文字等宽,TabBarIndicatorSize.tab跟每个tab等宽
  18. this.indicatorSize,
  19. // 选中Tab文字颜色
  20. this.labelColor,
  21. // 选中Tab文字Style
  22. this.labelStyle,
  23. // 每个label的padding值
  24. this.labelPadding,
  25. // 未选中label颜色
  26. this.unselectedLabelColor,
  27. // 未选中label的Style
  28. this.unselectedLabelStyle,
  29. this.dragStartBehavior = DragStartBehavior.down,
  30. // 点击事件
  31. this.onTap,
  32. })

再看下 TabBarVeiw 构造方法:

  1. const TabBarView({
  2. Key key,
  3. // tab内容页列表,和TabBar的tab数量一样
  4. @required this.children,
  5. // TabController对象,控制tab页
  6. this.controller,
  7. this.physics,
  8. this.dragStartBehavior = DragStartBehavior.down,
  9. })

我们看一个 TabBar 和 TabBarView 结合的实例:

  1. // TabBar和TabBarView最简单的用法
  2. class TabBarSamplesState extends State<TabBarSamples>
  3. with SingleTickerProviderStateMixin {
  4. TabController _tabController;
  5. @override
  6. void initState() {
  7. super.initState();
  8. //initialIndex为初始选中第几个,length为数量
  9. _tabController = TabController(initialIndex: 0, length: 5, vsync: this);
  10. // 监听
  11. _tabController.addListener(() {
  12. switch (_tabController.index) {
  13. case 0:
  14. break;
  15. case 1:
  16. break;
  17. }
  18. });
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. return Scaffold(
  23. appBar: AppBar(
  24. title: Text('TabBar Demo'),
  25. primary: true,
  26. // 设置TabBar
  27. bottom: TabBar(
  28. controller: _tabController,
  29. tabs: <Widget>[
  30. Tab(
  31. text: "Tab1",
  32. ),
  33. Tab(
  34. text: "Tab2",
  35. ),
  36. Tab(
  37. text: "Tab3",
  38. ),
  39. Tab(
  40. text: "Tab4",
  41. ),
  42. Tab(
  43. text: "Tab5",
  44. ),
  45. ],
  46. ),
  47. ),
  48. // body用TabBarView
  49. body: TabBarView(
  50. controller: _tabController,
  51. children: <Widget>[
  52. Center(
  53. child: Text("TabBarView data1"),
  54. ),
  55. Center(
  56. child: Text("TabBarView data2"),
  57. ),
  58. Center(
  59. child: Text("TabBarView data3"),
  60. ),
  61. Center(
  62. child: Text("TabBarView data4"),
  63. ),
  64. Center(
  65. child: Text("TabBarView data5"),
  66. ),
  67. ],
  68. ),
  69. );
  70. }
  71. @override
  72. void dispose() {
  73. super.dispose();
  74. _tabController.dispose();
  75. }
  76. }

运行效果如图:

TabBar和TabBarView

当然把 TabBar 放在底部也可以,不过一般放在顶部,放在底部的导航效果用另外一种组件,我们接下来将会学到。

如果想放在底部可以这样设置:

  1. // 最外层是Scaffold布局
  2. bottomNavigationBar: Material(
  3. color: Colors.blue,
  4. child: TabBar(
  5. controller: _controller,
  6. tabs: <Tab>[
  7. Tab(text: "Home", icon: Icon(Icons.home)),
  8. Tab(text: "Apps", icon: Icon(Icons.list)),
  9. Tab(text: "Center", icon: Icon(Icons.message)),
  10. ],
  11. indicatorWeight: 0.1,
  12. ),
  13. ),

2.BottomNavigationBar Widget

BottomNavigationBar 一般用来实现底部导航效果,和 Android 原生效果基本一样。

BottomNavigationBar 继承自 StatefulWidget,一般搭配 BottomNavigationBarItem 进行使用。

我们看下 BottomNavigationBar 实现的效果:

BottomNavigationBar和BottomNavigationBarItem

BottomNavigationBar 实现的导航有一个特点就是选中项会稍微有一个放大的动画,这是和其他组件实现的导航效果的一个小差别。

BottomNavigationBar 的构造方法:

  1. BottomNavigationBar({
  2. Key key,
  3. // BottomNavigationBarItem集合
  4. @required this.items,
  5. this.onTap,
  6. // 当前选中位置
  7. this.currentIndex = 0,
  8. // 设置显示的模式
  9. BottomNavigationBarType type,
  10. // 主题色
  11. this.fixedColor,
  12. // 图标尺寸
  13. this.iconSize = 24.0,
  14. })

BottomNavigationBarItem 的构造方法:

  1. const BottomNavigationBarItem({
  2. // 图标
  3. @required this.icon,
  4. // 文字
  5. this.title,
  6. // 选中图标
  7. Widget activeIcon,
  8. // 背景色
  9. this.backgroundColor,
  10. })

接下来我们通过代码来实现上面的 BottomNavigationBar 的效果:

  1. class NavigationBarState extends State<NavigationBarSamples> {
  2. // 默认选中第一项
  3. int _selectedIndex = 0;
  4. final _widgetOptions = [
  5. Text('Index 0: Home'),
  6. Text('Index 1: Business'),
  7. Text('Index 2: School'),
  8. ];
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. appBar: AppBar(
  13. title: Text('BottomNavigationBar Demo'),
  14. ),
  15. // 主体内容
  16. body: Center(
  17. child: _widgetOptions.elementAt(_selectedIndex),
  18. ),
  19. // 底部BottomNavigationBar
  20. bottomNavigationBar: BottomNavigationBar(
  21. items: <BottomNavigationBarItem>[
  22. // 单个BottomNavigationBarItem
  23. BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
  24. BottomNavigationBarItem(
  25. icon: Icon(Icons.business), title: Text('Business')),
  26. BottomNavigationBarItem(
  27. icon: Icon(Icons.school), title: Text('School')),
  28. ],
  29. // 选中位置
  30. currentIndex: _selectedIndex,
  31. // 主题色
  32. fixedColor: Colors.deepPurple,
  33. // 点击
  34. onTap: _onItemTapped,
  35. ),
  36. );
  37. }
  38. void _onItemTapped(int index) {
  39. setState(() {
  40. _selectedIndex = index;
  41. });
  42. }
  43. }

可以得到上面效果图所示的运行效果。

3.CupertinoTabBar和PageView Widget

想实现 Android 上类似于 ViewPager 和 TabBar 的导航效果的,也可以使用 CupertinoTabBar 配合PageView 进行实现。这也是一种实现底部导航切换页面的一种方式。

我们看下 CupertinoTabBar 实现的效果:

CupertinoTabBar和PageView

CupertinoTabBar 继承自 StatelessWidget,PageView 继承自 StatefulWidget。

我们先看下 CupertinoTabBar 的构造方法:

  1. CupertinoTabBar({
  2. Key key,
  3. // 导航项
  4. @required this.items,
  5. this.onTap,
  6. this.currentIndex = 0,
  7. this.backgroundColor,
  8. // 选中色
  9. this.activeColor,
  10. // 未选中色
  11. this.inactiveColor = CupertinoColors.inactiveGray,
  12. this.iconSize = 30.0,
  13. // 边框
  14. this.border = const Border(
  15. top: BorderSide(
  16. color: _kDefaultTabBarBorderColor,
  17. width: 0.0, // One physical pixel.
  18. style: BorderStyle.solid,
  19. ),
  20. ),
  21. })

再看下 PageView 的构造方法:

  1. PageView({
  2. Key key,
  3. // 滚动方向
  4. this.scrollDirection = Axis.horizontal,
  5. this.reverse = false,
  6. // PageController页面控制
  7. PageController controller,
  8. // 滚动的动画效果
  9. this.physics,
  10. this.pageSnapping = true,
  11. // 页面改变监听
  12. this.onPageChanged,
  13. // 子元素
  14. List<Widget> children = const <Widget>[],
  15. this.dragStartBehavior = DragStartBehavior.down,
  16. })

接下来我们通过代码来实现上面的 CupertinoTabBar 的效果:

  1. class CupertinoTabBarState extends State<CupertinoTabBarSamples> {
  2. // 默认选中第一项
  3. int _selectedIndex = 0;
  4. var _pageController = new PageController(initialPage: 0);
  5. @override
  6. void initState() {
  7. super.initState();
  8. _pageController.addListener(() { });
  9. }
  10. @override
  11. Widget build(BuildContext context) {
  12. return Scaffold(
  13. appBar: AppBar(
  14. title: Text('CupertinoTabBar Demo'),
  15. ),
  16. // body主体内容用PageView
  17. body: PageView(
  18. // 监听控制类
  19. controller: _pageController,
  20. onPageChanged: _onItemTapped,
  21. children: <Widget>[
  22. Text('Index 0: Home'),
  23. Text('Index 1: Business'),
  24. Text('Index 2: School'),
  25. ],
  26. ),
  27. // 底部导航栏用CupertinoTabBar
  28. bottomNavigationBar: CupertinoTabBar(
  29. // 导航集合
  30. items: <BottomNavigationBarItem>[
  31. BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
  32. BottomNavigationBarItem(
  33. icon: Icon(Icons.business), title: Text('Business')),
  34. BottomNavigationBarItem(
  35. icon: Icon(Icons.school), title: Text('School')),
  36. ],
  37. currentIndex: _selectedIndex,
  38. onTap: setPageViewItemSelect,
  39. ),
  40. );
  41. }
  42. void _onItemTapped(int index) {
  43. setState(() {
  44. _selectedIndex = index;
  45. });
  46. }
  47. // 底部点击切换
  48. void setPageViewItemSelect(int indexSelect) {
  49. _pageController.animateToPage(indexSelect,
  50. duration: const Duration(milliseconds: 300), curve: Curves.ease);
  51. }
  52. }

4.BottomAppBar Widget

实现底部的导航效果,除了上面讲到的这些组件外,还可以用 BottomAppBar 来实现,这个组件自定义功能更加强大一些,也可以实现比较复杂的自定义效果。一般搭配 FloatingActionButton 自定义使用。

BottomAppBar 继承自 StatefulWidget。

我们看下 BottoAppBar 自定义实现的效果图:

BottoAppBar

我们看下 BottomAppBar 的构造方法:

  1. const BottomAppBar({
  2. Key key,
  3. // 颜色
  4. this.color,
  5. this.elevation,
  6. // 设置底栏的形状
  7. this.shape,
  8. this.clipBehavior = Clip.none,
  9. this.notchMargin = 4.0,
  10. // 可以放置各种类型的Widget,自定义性更强
  11. this.child,
  12. })

接下来我们通过代码来实现上面的 BottomAppBar 的效果:

  1. class BottomAppBarState extends State<BottomAppBarSamples> {
  2. @override
  3. void initState() {
  4. super.initState();
  5. }
  6. @override
  7. Widget build(BuildContext context) {
  8. return Scaffold(
  9. appBar: AppBar(
  10. title: Text('BottomAppBar Demo'),
  11. ),
  12. // body主体内容
  13. body: Text("Index 0:Home"),
  14. // 底部导航栏用BottomAppBar
  15. bottomNavigationBar: BottomAppBar(
  16. // 切口的距离
  17. notchMargin: 6,
  18. // 底部留出空缺
  19. shape: CircularNotchedRectangle(),
  20. child: Row(
  21. children: <Widget>[
  22. IconButton(
  23. icon: Icon(Icons.home),
  24. onPressed: null,
  25. ),
  26. IconButton(
  27. icon: Icon(Icons.business),
  28. onPressed: null,
  29. ),
  30. IconButton(
  31. icon: Icon(Icons.school),
  32. onPressed: null,
  33. ),
  34. ],
  35. ),
  36. ),
  37. floatingActionButton: FloatingActionButton(
  38. child: Icon(
  39. Icons.add,
  40. color: Colors.white,
  41. ),
  42. onPressed: null),
  43. floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
  44. );
  45. }
  46. }

运行效果如上面的效果图所示。

5.总结

本节博客主要是给大家讲解了 Flutter 的几种实现导航页效果的组件,我们可以根据实际情况需要选择合适的组件进行实现导航页效果,也要知道它们的用法和特点。主要注意点和建议如下:

  • 掌握这几种导航页实现的方法、组件特点。
  • 熟练掌握它们的用法,实践一下这几个 Widget 使用方法,尝试写一个可以有顶部和底部导航栏的页面。

发表评论

表情:
评论列表 (有 0 条评论,4人围观)

还没有评论,来说两句吧...

相关阅读