当前位置:   article > 正文

Flutter开发(十六):Flutter顶部导航、底部导航与侧拉菜单_flutter fixed

flutter fixed

1. TabBar + TabBarView 来实现顶部导航

2. PageView + BottomNavigationBar 底部导航

3.利用 Drawer 实现侧拉菜单

4. PageView

实现导航需要用到 Scaffold 和 BottomNavigationBar ,PageView 和 PageController。

Scaffold widget 是实现了基本的 material design 的布局结构。里面包括了常用的标题 appBar ,内容 body,侧拉 drawer。

1.TabBar + TabBarView 来实现顶部导航

首先实现像 Android 中 ViewPager + Fragment 的效果,效果图如下:

 利用 TabBar + TabBarView 来实现,示例代码:

  1. void main() {
  2. runApp(MyApp());
  3. }
  4. class MyApp extends StatefulWidget {
  5. @override
  6. _MyHomePageState createState() => _MyHomePageState();
  7. }
  8. class _MyHomePageState extends State<MyApp> {
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. home: DefaultTabController(
  13. length: choices.length,
  14. child: Scaffold(
  15. appBar: AppBar(
  16. title: const Text('Tabbed AppBar'),
  17. bottom: TabBar(
  18. isScrollable: true,
  19. tabs: choices.map((Choice choice) {
  20. return Tab(
  21. text: choice.title,
  22. icon: Icon(choice.icon),
  23. );
  24. }).toList(),
  25. ),
  26. ),
  27. body: TabBarView(
  28. children: choices.map((Choice choice) {
  29. return Padding(
  30. padding: const EdgeInsets.all(16.0),
  31. child: ChoiceCard(choice: choice),
  32. );
  33. }).toList(),
  34. ),
  35. ),
  36. ),
  37. );
  38. }
  39. }
  40. class Choice{
  41. const Choice({required this.title,required this.icon});
  42. final String title;
  43. final IconData icon;
  44. }
  45. const List<Choice> choices = const <Choice>[
  46. const Choice(title: '1', icon: Icons.animation),
  47. const Choice(title: '1', icon: Icons.phone),
  48. const Choice(title: '2', icon: Icons.directions_bike),
  49. const Choice(title: '3', icon: Icons.directions_boat),
  50. const Choice(title: '4', icon: Icons.directions_bus),
  51. const Choice(title: '5', icon: Icons.directions_car),
  52. ];
  53. class ChoiceCard extends StatelessWidget{
  54. const ChoiceCard({Key ? key ,required this.choice}):super(key: key);
  55. final Choice choice;
  56. @override
  57. Widget build(BuildContext context) {
  58. return Card(
  59. color: Colors.white,
  60. child: Center(
  61. child: Column(
  62. mainAxisSize: MainAxisSize.min,
  63. crossAxisAlignment: CrossAxisAlignment.center,
  64. children: [
  65. Icon(choice.icon,size:128.0,color: Colors.blue,),
  66. Text(choice.title)
  67. ],
  68. ),
  69. ),
  70. );
  71. }
  72. }

2. PageView + BottomNavigationBar 底部导航

在 Android 中,实现常用底部导航栏,用 Fragment 即可,来控制显示隐藏。

Flutter 中,要用 PageView + BottomNavigationBar 来实现。

效果图如下:

 示例代码,代码中有注释,并不难理解:

  1. void main() {
  2. runApp(MyApp());
  3. }
  4. class MyApp extends StatefulWidget {
  5. @override
  6. _MyHomePageState createState() => _MyHomePageState();
  7. }
  8. class _MyHomePageState extends State<MyApp>
  9. with SingleTickerProviderStateMixin {
  10. final _defaultColor = Colors.grey; //未选中
  11. final _activeColor = Colors.blue; //选中
  12. int _currentIndex = 0; //默认选中第一个
  13. var _controller = PageController(
  14. initialPage: 0,
  15. );
  16. @override
  17. Widget build(BuildContext context) {
  18. return new MaterialApp(
  19. home: Scaffold(
  20. body: PageView(
  21. controller: _controller,
  22. children: [
  23. Center(child: Text("首页")),
  24. Center(child: Text("搜索")),
  25. Center(child: Text("发现")),
  26. Center(child: Text("我的"))
  27. ],
  28. physics: NeverScrollableScrollPhysics(),
  29. ),
  30. //底部导航菜单
  31. bottomNavigationBar: BottomNavigationBar(
  32. //默认显示第几个页面
  33. currentIndex: _currentIndex,
  34. onTap: (index) {
  35. //点击跳转指定页面
  36. _controller.jumpToPage(index);
  37. //改变当前页面
  38. setState(() {
  39. _currentIndex = index;
  40. });
  41. },
  42. //fixed永远显示底部lab(文字),shifting只有被选中才会显示底部文字
  43. type: BottomNavigationBarType.fixed,
  44. items: [
  45. BottomNavigationBarItem(
  46. //设置默认图标和颜色
  47. icon: Icon(
  48. Icons.home,
  49. color: _defaultColor,
  50. ),
  51. //设置选中图标和颜色
  52. activeIcon: Icon(
  53. Icons.home,
  54. color: _activeColor,
  55. ),
  56. title: Text(
  57. '首页',
  58. style: TextStyle(
  59. color: _defaultColor,
  60. ),
  61. )),
  62. BottomNavigationBarItem(
  63. icon: Icon(
  64. Icons.search,
  65. color: _defaultColor,
  66. ),
  67. activeIcon: Icon(
  68. Icons.search,
  69. color: _activeColor,
  70. ),
  71. title: Text('搜索',
  72. style: TextStyle(
  73. color: _currentIndex != 1 ? _defaultColor : _activeColor,
  74. )),
  75. ),
  76. BottomNavigationBarItem(
  77. icon: Icon(
  78. Icons.camera_alt,
  79. color: _defaultColor,
  80. ),
  81. activeIcon: Icon(
  82. Icons.camera_alt,
  83. color: _activeColor,
  84. ),
  85. title: Text('发现',
  86. style: TextStyle(
  87. color: _currentIndex != 2 ? _defaultColor : _activeColor,
  88. )),
  89. ),
  90. BottomNavigationBarItem(
  91. icon: Icon(
  92. Icons.my_library_add,
  93. color: _defaultColor,
  94. ),
  95. activeIcon: Icon(
  96. Icons.my_library_add,
  97. color: _activeColor,
  98. ),
  99. title: Text('我的',
  100. style: TextStyle(
  101. color: _currentIndex != 3 ? _defaultColor : _activeColor,
  102. )),
  103. )
  104. ],
  105. ),
  106. ));
  107. }
  108. }

 

3.利用 Drawer 实现侧拉菜单

如 QQ,网易云的侧拉菜单效果。在 Flutter 中,用 Drawer 就可以实现;

效果图:

 示例代码,代码中有注释,并不难理解:

  1. void main() {
  2. runApp(MyApp());
  3. }
  4. /**
  5. * 侧拉菜单
  6. */
  7. class MyApp extends StatelessWidget {
  8. const MyApp({Key? key}) : super(key: key);
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: 'Flutter Demo',
  13. theme: ThemeData(
  14. primarySwatch: Colors.blue,
  15. ),
  16. home: HomePage(),
  17. );
  18. }
  19. }
  20. class HomePage extends StatefulWidget {
  21. @override
  22. _MyHomePageState createState() => _MyHomePageState();
  23. }
  24. class _MyHomePageState extends State<HomePage> {
  25. @override
  26. Widget build(BuildContext context) {
  27. return Scaffold(
  28. appBar: AppBar(
  29. title: Text('标题'),
  30. ),
  31. body: Center(
  32. child: Text('侧拉菜单'),
  33. ),
  34. drawer: Drawer(
  35. child: ListView(
  36. padding: EdgeInsets.zero,
  37. children: [
  38. //侧拉菜单头部
  39. DrawerHeader(
  40. child: Text('侧拉菜单头部标题'),
  41. decoration: BoxDecoration(color: Colors.blue),
  42. ),
  43. ListTile(
  44. title: Text('内容1'),
  45. onTap: () {
  46. Navigator.pop(context);
  47. },
  48. ),
  49. ListTile(
  50. title: Text('内容2'),
  51. onTap: () {
  52. Navigator.pop(context);
  53. },
  54. ),
  55. ],
  56. ),
  57. ),
  58. );
  59. }
  60. }

 

4. PageView

PageView 在 Fultter 中最常用的 widget 之一。它可以完成水平和垂直方向的滚动。

它里面有许多参数:

  1. PageView({
  2. Key? key,
  3. this.scrollDirection = Axis.horizontal, //滚动方向,垂直或水平
  4. this.reverse = false, //是否反向滚动
  5. PageController? controller, //PageView控制器
  6. this.physics, //手势滚动逻辑
  7. this.pageSnapping = true, //false禁止页面捕捉,实现自定义滚动
  8. this.onPageChanged, //页面切换被调用
  9. List<Widget> children = const <Widget>[],
  10. this.dragStartBehavior = DragStartBehavior.start,
  11. this.allowImplicitScrolling = false, //是否显示滚动条
  12. this.restorationId, //回到顶部
  13. this.clipBehavior = Clip.hardEdge,
  14. this.scrollBehavior,
  15. this.padEnds = true,
  16. }) : assert(allowImplicitScrolling != null),
  17. assert(clipBehavior != null),
  18. controller = controller ?? _defaultPageController,
  19. childrenDelegate = SliverChildListDelegate(children),
  20. super(key: key);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/257455?site
推荐阅读
相关标签
  

闽ICP备14008679号