flutter 底部导航栏组件BottomNavigationBar

淡淡的烟草味﹌ 2021-07-24 23:58 653阅读 0赞
  1. 底部导航栏组件
  2. (1)在Scaffold组件中与body同级设置
  3. bottomNavigationBar: BottomNavigationBar(...)
  4. (2)实现点击改变效果需要在有状态组件中根据索引实现,不同的页面根据不同的组件内容来设置,放进数值里,根据索引选择
  5. 参数:
  6. BottomNavigationBar(
  7. items:[ 配置导航栏每个bar内容,数值类型必须是List<BottomNavigationBarItem>
  8. BottomNavigationBarItem( 单个bar内容
  9. icon:Icon(Icons.home), 设置图标
  10. title:Text("首页"), bar的文本标签内容
  11. backgroundColor: Colors.red, 点击切换时,整个导航栏的颜色
  12. activeIcon:Icon(Icons.hd) 选中图标
  13. ),
  14. currentIndex: n, 通过索引选中bar
  15. fixedColor: Colors.green, 选中的icon和文字颜色
  16. selectedItemColor:Colors.green, 选中的icon和文字颜色,不能和fixedColor同时出现
  17. unselectedItemColor: Colors.blue, 未选中的icon和文字颜色,不能和fixedColor同时出现
  18. iconSize: 35,
  19. selectedFontSize: 20,
  20. unselectedFontSize: 10,
  21. backgroundColor: Colors.red, 整个导航栏的颜色fixed类型下有效
  22. onTap:(index){setState(() { 点击bar回调,必须传入index参数,bar的索引
  23. this._index=index;
  24. });},
  25. type: BottomNavigationBarType.shifting 点击会有切换变大的特效,.fixed类型可以放置任意多的bar,无特效
  26. ),
  27. );

代码示例:

  1. import "package:flutter/material.dart";
  2. import 'page/1.dart';
  3. import 'page/2.dart';
  4. void main()
  5. {
  6. runApp(App());
  7. }
  8. class App extends StatelessWidget{
  9. @override
  10. Widget build(BuildContext context) {
  11. // TODO: implement build
  12. return MaterialApp(
  13. home:Tabs()
  14. ,);
  15. }
  16. }
  17. class Tabs extends StatefulWidget {
  18. Tabs({ Key key}) : super(key: key);
  19. @override
  20. _TabsState createState() => _TabsState();
  21. }
  22. class _TabsState extends State<Tabs> {
  23. int _index=0;
  24. List _page=[Home2(),Home3()];
  25. @override
  26. Widget build(BuildContext context) {
  27. return Scaffold(appBar: AppBar(title: Text("hh")),
  28. body:_page[this._index],
  29. bottomNavigationBar: BottomNavigationBar(
  30. items:[ //配置内容,数值类型必须是List<BottomNavigationBarItem>
  31. BottomNavigationBarItem(//单个bar内容
  32. icon:Icon(Icons.home),
  33. title:Text("首页"),
  34. // backgroundColor: Colors.red, //点击切换时,整个导航栏的颜色
  35. // activeIcon:Icon(Icons.hd)
  36. ),
  37. BottomNavigationBarItem(
  38. icon:Icon(Icons.category),
  39. title:Text("第二页"),
  40. // backgroundColor: Colors.blue
  41. )
  42. ],
  43. currentIndex: this._index, //选中的底部bar索引
  44. fixedColor: Colors.green, //选中的颜色
  45. // selectedItemColor:Colors.green, //不能和fixedColor同时出现
  46. // unselectedItemColor: Colors.blue,
  47. iconSize: 35,
  48. selectedFontSize: 20,
  49. unselectedFontSize: 10,
  50. // backgroundColor: Colors.red, //整个导航栏的颜色fixed类型下有效
  51. onTap:(index){ setState(() {
  52. this._index=index;
  53. });}, //点击bar回调,必须传入index参数,bar的索引
  54. type: BottomNavigationBarType.fixed //点击会有切换变大的特效,.fixed类型可以放置任意多的bar
  55. ),
  56. );
  57. }
  58. }
  59. class Home extends StatelessWidget {
  60. const Home({ Key key}) : super(key: key);
  61. @override
  62. Widget build(BuildContext context) {
  63. return Container(
  64. child: Column(
  65. children: <Widget>[
  66. ],
  67. ),
  68. );
  69. }
  70. }

发表评论

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

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

相关阅读