YSTabBar.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import 'package:file_preview/file_preview.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_tzh/gather/YSGather.dart';
  5. import 'package:flutter_tzh/mine/YSMine.dart';
  6. import 'package:flutter_tzh/tool/YSTools.dart';
  7. import '../home/YSHome.dart';
  8. class YSTabBar extends StatefulWidget {
  9. const YSTabBar({Key? key}) : super(key: key);
  10. @override
  11. YSTabBarState createState() => YSTabBarState();
  12. }
  13. class YSTabBarState extends State<YSTabBar> {
  14. int _selectedIndex = 0;
  15. final PageController _pageController = PageController();
  16. static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  17. static const List<Widget> _widgetOptions = <Widget>[
  18. YSHome(),
  19. YSGather(),
  20. YSMine()
  21. ];
  22. @override
  23. void dispose() {
  24. _pageController.dispose();
  25. super.dispose();
  26. }
  27. @override
  28. void initState() {
  29. _initTbs();
  30. super.initState();
  31. }
  32. void _onItemTapped(int index) {
  33. }
  34. _initTbs() async{
  35. bool hasInit = await FilePreview.tbsHasInit();
  36. if(hasInit)return;
  37. bool isInit = await FilePreview.initTBS(license: '123456');
  38. // FilePreview.tbsDebug();
  39. LogUtil.d('isInit============$isInit');
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. return Scaffold(
  44. backgroundColor: const Color(0xFF23262B),
  45. body:PageView.builder(
  46. controller: _pageController,
  47. itemBuilder: (context,index){
  48. return _widgetOptions[index];
  49. },
  50. itemCount: _widgetOptions.length,
  51. physics: const NeverScrollableScrollPhysics(),
  52. ),
  53. //_widgetOptions.elementAt(_selectedIndex),
  54. bottomNavigationBar: YSTabBarView(postIndex: (value){
  55. if(value==1){
  56. Navigator.of(context).push(
  57. CupertinoPageRoute(builder: (context){
  58. return const YSGather();
  59. })
  60. );
  61. }else{
  62. _pageController.jumpToPage(value);
  63. _selectedIndex = value;
  64. setState(() {});
  65. }
  66. },)
  67. );
  68. }
  69. }
  70. class YSTabBarView extends StatefulWidget {
  71. final ValueSetter<int> postIndex;
  72. const YSTabBarView({Key? key, required this.postIndex}) : super(key: key);
  73. @override
  74. YSTabBarViewState createState() => YSTabBarViewState();
  75. }
  76. class YSTabBarViewState extends State<YSTabBarView> {
  77. final List<YSBarItem> _itemArray = [
  78. YSBarItem('images/home.png', 'images/home1.png', '首页',),
  79. YSBarItem('images/add.png', 'images/add.png', '',),
  80. YSBarItem('images/mine.png', 'images/mine1.png', '我的',),
  81. ];
  82. int _index = 0;
  83. @override
  84. Widget build(BuildContext context) {
  85. return ClipRRect(
  86. borderRadius: const BorderRadius.all(Radius.circular(24)),
  87. child: Container(
  88. height: 114,
  89. color: const Color(0xFF2E3138),
  90. width: ysWidth(context),
  91. child: ListView.builder(itemBuilder: (context,index){
  92. YSBarItem item = _itemArray[index];
  93. return GestureDetector(
  94. onTap: (){
  95. if(item.label.isNotEmpty){
  96. _index = index;
  97. setState(() {});
  98. }
  99. widget.postIndex(index);
  100. },
  101. behavior: HitTestBehavior.opaque,
  102. child: Container(
  103. width: ysWidth(context)/_itemArray.length,
  104. alignment: Alignment.center,
  105. child: item.label.isEmpty?Image.asset(item.image,height: 60,width: 60,):Column(
  106. mainAxisSize: MainAxisSize.min,
  107. children: [
  108. Image.asset(_index==index?item.imageSelected:item.image,height: 22,width: 22,),
  109. Padding(
  110. padding: const EdgeInsets.only(top: 5),
  111. child: Text(item.label,style: TextStyle(fontSize: 11,color: _index==index?const Color(0xFF31D19E):Colors.white),),
  112. )
  113. ],
  114. ),
  115. ),
  116. );
  117. },itemCount: _itemArray.length,scrollDirection: Axis.horizontal,),
  118. ),
  119. );
  120. }
  121. }
  122. class YSBarItem{
  123. final String image;
  124. final String imageSelected;
  125. String label = '';
  126. YSBarItem(this.image, this.imageSelected, this.label);
  127. }