实现点击以及滑动都可以切换导航的效果

核心代码



完整代码

// 输入 imrm 快速生成下面
import 'package:flutter/material.dart';
import 'Home.dart';
import 'HomeNew.dart';
import 'Category.dart';
import 'User.dart';
import 'Cart.dart'; class Tabs extends StatefulWidget {
const Tabs({Key? key, arguments}) : super(key: key); @override
_TabsState createState() => _TabsState();
} class _TabsState extends State<Tabs> {
int _currentIndex = 0;
late PageController _pageController; // 缓存页面重要代码
@override
void initState() {
super.initState();
this._pageController =
new PageController(initialPage: this._currentIndex); // 缓存页面重要代码
} Widget _initPng() {
return Image.asset(
'assets/images/tdd2.png',
height: 25.0,
);
} static const Color myPink = Color(0xFFdf769e);
var _title;
List<Widget> _pageList = [
HomeNewPage(),
HomePage(),
CategoryPage(),
CartPage(),
UserPage()
];
void _pageChange(int index) {
setState(() {
_currentIndex = index;
});
} @override
Widget build(BuildContext context) {
// _title = _initPng();
return Scaffold(
//这里设置总体背景色
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 0, //隐藏底部阴影分割线
centerTitle: true, //标题是否居中 安卓上有效ios默认居中
backgroundColor: Colors.white, //设置导航背景颜色
title: _title,
),
// title: Text('我是title')),
// body: Text('页码下标' + this._currentIndex.toString()),
// body: this._pageList[this._currentIndex],
body: PageView(
onPageChanged: _pageChange,
controller: this._pageController, // 缓存页面重要代码
children: this._pageList,
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
BottomNavigationBarItem(icon: Icon(Icons.home), label: '测试'),
BottomNavigationBarItem(icon: Icon(Icons.category), label: '分类'),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_cart), label: '购物车'),
BottomNavigationBarItem(icon: Icon(Icons.people), label: '个人中心'),
],
currentIndex: _currentIndex,
onTap: (index) {
setState(() {
this._currentIndex = index;
this._pageController.jumpToPage(index); // 缓存页面重要代码
switch (index) {
case 0:
{
print('case01');
_title = Image.asset(
'assets/images/tdd2.png',
height: 25.0,
);
}
break;
case 1:
{
_title = Text(
"测试页面",
style: TextStyle(
color: myPink,
fontSize: 17,
letterSpacing: 1,
wordSpacing: 2,
height: 1.2,
fontWeight: FontWeight.w600),
);
}
break;
case 2:
{
_title = Text(
"分类",
style: TextStyle(
color: myPink,
fontSize: 17,
letterSpacing: 1,
wordSpacing: 2,
height: 1.2,
fontWeight: FontWeight.w600),
);
}
break;
case 3:
{
_title = Text(
"购物车",
style: TextStyle(
color: myPink,
fontSize: 17,
letterSpacing: 1,
wordSpacing: 2,
height: 1.2,
fontWeight: FontWeight.w600),
);
}
break;
case 4:
{
_title = Text(
"个人中心",
style: TextStyle(
color: myPink,
fontSize: 17,
letterSpacing: 1,
wordSpacing: 2,
height: 1.2,
fontWeight: FontWeight.w600),
);
}
break;
}
});
},
type: BottomNavigationBarType.fixed,
fixedColor: myPink,
),
);
}
}

最新文章

  1. Writing Text Files On The Client in Oracle Forms 10g
  2. java 反射: 当Timestamp类型的属性值为null时,设置默认值
  3. makefile_2
  4. HTML在IE中的条件注释
  5. set集合的用法总结(转)
  6. scala学习笔记2
  7. 【转】有向图强连通分量的Tarjan算法
  8. R语言学习笔记:数据的可视化
  9. SGU 122.The book (哈密顿回路)
  10. UML学习-状态图
  11. 使用传入的总记录数实现一条sql语句完成分页查询
  12. java字符串,包,数组及空心正方形,菱形的实例
  13. JAVA课程设计 学生成绩管理
  14. python-邮件提醒功能
  15. IDEA 常用插件
  16. pycharm断点应用
  17. 【Spring源码解读】bean标签中的属性(二)你可能还不够了解的 abstract 属性和 parent 属性
  18. 【float】与【position】汇总
  19. 【php 之获得当前日期以及比较日期大小】
  20. 用十条命令在一分钟内检查Linux服务器性能[转]

热门文章

  1. 八、Django的组件
  2. RHCE习题
  3. Sprint产品待办列表的优先级要怎么排?
  4. Python图像处理丨5种图像处理特效
  5. Karmada跨集群优雅故障迁移特性解析
  6. UEFI引导linux启动过程的顺序,及修改办法
  7. Oracle12c异常关闭后启动PDBORCL(ORA-01033)
  8. xml中出现&lt; &gt;&amp;等特殊字符如何存储
  9. vscode+springboot+gradle
  10. 【Java SE】课程目录