YSTabbar.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:ysairplane/code/YSCourse.dart';
  5. import 'package:ysairplane/code/YSHome.dart';
  6. import 'package:ysairplane/code/YSLogin.dart';
  7. import 'package:ysairplane/code/YSMine.dart';
  8. import 'package:ysairplane/code/YSSquare.dart';
  9. import 'package:ysairplane/tools/YSTools.dart';
  10. import 'package:jpush_flutter/jpush_flutter.dart';
  11. // class YSTabbar extends StatefulWidget {
  12. // @override
  13. // _YSPageState createState() => _YSPageState();
  14. // }
  15. //
  16. // class _YSPageState extends State<YSTabbar> {
  17. // final JPush jpush = new JPush();
  18. // @override
  19. // void initState() {
  20. // jpush.setup(
  21. // appKey: "5cdf5bc06073b6ba4b3eaf86", //你自己应用的 AppKey
  22. // channel: "theChannel",
  23. // production: false,
  24. // debug: true,
  25. // );
  26. // jpush.applyPushAuthority(
  27. // new NotificationSettingsIOS(sound: true, alert: true, badge: true)
  28. // );
  29. // jpush.getRegistrationID().then((rid) {
  30. // print("flutter get registration id : $rid");
  31. // });
  32. // try {
  33. // jpush.addEventHandler(
  34. // onReceiveNotification: (Map<String, dynamic> message) async {
  35. // print("flutter onReceiveNotification: $message");
  36. // },
  37. // onOpenNotification: (Map<String, dynamic> message) async {
  38. // print("flutter onOpenNotification: $message");
  39. // // Navigator.of(context).push(
  40. // // CupertinoPageRoute(
  41. // // builder: (context){
  42. // // return null;
  43. // // }
  44. // // )
  45. // // );
  46. // },
  47. // onReceiveMessage: (Map<String, dynamic> message) async {
  48. // print("flutter onReceiveMessage: $message");
  49. // },
  50. // onReceiveNotificationAuthorization:(Map<String, dynamic> message) async {
  51. // print("flutter onReceiveNotificationAuthorization: $message");
  52. // });
  53. // } on PlatformException {
  54. //
  55. // }
  56. //
  57. // super.initState();
  58. // }
  59. // int _selected = 0;
  60. // Widget _selectedWidget = YSHome();
  61. // _onTap(int index) async{
  62. // if(index==3||index==2){
  63. // isLogin(context,login: (){
  64. // switch (index){
  65. // case 0:
  66. // _selectedWidget = YSHome();
  67. // break;
  68. // case 1:
  69. // _selectedWidget = YSCourse();
  70. // break;
  71. // case 2:
  72. // _selectedWidget = YSSquare();
  73. // break;
  74. // case 3:
  75. // _selectedWidget = YSMine();
  76. // break;
  77. // }
  78. // setState(() {
  79. // _selected = index;
  80. // });
  81. // });
  82. // }else{
  83. // switch (index){
  84. // case 0:
  85. // _selectedWidget = YSHome();
  86. // break;
  87. // case 1:
  88. // _selectedWidget = YSCourse();
  89. // break;
  90. // case 2:
  91. // _selectedWidget = YSSquare();
  92. // break;
  93. // case 3:
  94. // _selectedWidget = YSMine();
  95. // break;
  96. // }
  97. // setState(() {
  98. // _selected = index;
  99. // });
  100. // }
  101. //
  102. // }
  103. // @override
  104. // Widget build(BuildContext context) {
  105. // return Scaffold(
  106. // bottomNavigationBar: CupertinoTabBar(
  107. // items: [
  108. // BottomNavigationBarItem(
  109. // icon: ImageIcon(AssetImage('lib/images/home.png'),size: 20,),
  110. // title: Text('首页',style: TextStyle(fontWeight: FontWeight.bold),)
  111. // ),
  112. // BottomNavigationBarItem(
  113. // icon: ImageIcon(AssetImage('lib/images/course.png'),size: 20,),
  114. // title: Text('行程',style: TextStyle(fontWeight: FontWeight.bold),)
  115. // ),
  116. // BottomNavigationBarItem(
  117. // icon: Stack(
  118. // children: [
  119. // ImageIcon(AssetImage('lib/images/square.png'),size: 20,),
  120. // Container(
  121. // height: 10,
  122. // width: 10,
  123. // decoration: BoxDecoration(
  124. // color: Colors.red,
  125. // borderRadius: BorderRadius.all(Radius.circular(5))
  126. // ),
  127. // margin: EdgeInsets.only(left: 15),
  128. // )
  129. // ],
  130. // ),//
  131. // title: Text('广场',style: TextStyle(fontWeight: FontWeight.bold),)
  132. // ),
  133. // BottomNavigationBarItem(
  134. // icon: ImageIcon(AssetImage('lib/images/mine.png'),size: 20,),
  135. // title: Text('我的',style: TextStyle(fontWeight: FontWeight.bold),)
  136. // ),
  137. // ],
  138. // activeColor: Color(0xFF007AFF),
  139. // inactiveColor: Color(0xFF999999),
  140. // iconSize: 25,
  141. // onTap: _onTap,
  142. // currentIndex: _selected,
  143. // ),
  144. // body: _selectedWidget,
  145. // );
  146. // }
  147. // }
  148. class YSTabbar extends StatefulWidget {
  149. @override
  150. _MyHomePageState createState() => _MyHomePageState();
  151. }
  152. class _MyHomePageState extends State<YSTabbar> {
  153. final JPush jpush = new JPush();
  154. @override
  155. void initState() {
  156. jpush.setup(
  157. appKey: "5cdf5bc06073b6ba4b3eaf86", //你自己应用的 AppKey
  158. channel: "theChannel",
  159. production: false,
  160. debug: true,
  161. );
  162. jpush.applyPushAuthority(
  163. new NotificationSettingsIOS(sound: true, alert: true, badge: true)
  164. );
  165. jpush.getRegistrationID().then((rid) {
  166. print("flutter get registration id : $rid");
  167. });
  168. try {
  169. jpush.addEventHandler(
  170. onReceiveNotification: (Map<String, dynamic> message) async {
  171. print("flutter onReceiveNotification: $message");
  172. },
  173. onOpenNotification: (Map<String, dynamic> message) async {
  174. print("flutter onOpenNotification: $message");
  175. // Navigator.of(context).push(
  176. // CupertinoPageRoute(
  177. // builder: (context){
  178. // return null;
  179. // }
  180. // )
  181. // );
  182. },
  183. onReceiveMessage: (Map<String, dynamic> message) async {
  184. print("flutter onReceiveMessage: $message");
  185. },
  186. onReceiveNotificationAuthorization:(Map<String, dynamic> message) async {
  187. print("flutter onReceiveNotificationAuthorization: $message");
  188. });
  189. } on PlatformException {
  190. }
  191. super.initState();
  192. }
  193. final items = [
  194. BottomNavigationBarItem(
  195. icon: ImageIcon(AssetImage('lib/images/home.png'),size: 20,),
  196. title: Text('首页',style: TextStyle(fontWeight: FontWeight.bold),),
  197. ),
  198. BottomNavigationBarItem(
  199. icon: ImageIcon(AssetImage('lib/images/course.png'),size: 20,),
  200. title: Text('行程',style: TextStyle(fontWeight: FontWeight.bold),)
  201. ),
  202. BottomNavigationBarItem(
  203. icon: Stack(
  204. children: [
  205. ImageIcon(AssetImage('lib/images/square.png'),size: 20,),
  206. Container(
  207. height: 10,
  208. width: 10,
  209. decoration: BoxDecoration(
  210. color: Colors.red,
  211. borderRadius: BorderRadius.all(Radius.circular(5))
  212. ),
  213. margin: EdgeInsets.only(left: 15),
  214. )
  215. ],
  216. ),//
  217. title: Text('广场',style: TextStyle(fontWeight: FontWeight.bold),)
  218. ),
  219. BottomNavigationBarItem(
  220. icon: ImageIcon(AssetImage('lib/images/mine.png'),size: 20,),
  221. title: Text('我的',style: TextStyle(fontWeight: FontWeight.bold),)
  222. ),
  223. ];
  224. final bodyList = [YSHome(), YSCourse(), YSSquare(), YSMine()];
  225. final pageController = PageController();
  226. int currentIndex = 0;
  227. void onTap(int index) {
  228. if(index==3||index==2){
  229. isLogin(context,login: (){
  230. pageController.jumpToPage(index);
  231. });
  232. }else{
  233. pageController.jumpToPage(index);
  234. }
  235. }
  236. void onPageChanged(int index) {
  237. if(index==3||index==2){
  238. isLogin(context,login: (){
  239. currentIndex = index;
  240. });
  241. }else{
  242. currentIndex = index;
  243. }
  244. setState(() {});
  245. }
  246. @override
  247. Widget build(BuildContext context) {
  248. return Scaffold(
  249. bottomNavigationBar: CupertinoTabBar(
  250. items: items,
  251. currentIndex: currentIndex,
  252. onTap: onTap,
  253. activeColor: Color(0xFF007AFF),
  254. inactiveColor: Color(0xFF999999),
  255. ),
  256. body: PageView(
  257. controller: pageController,
  258. onPageChanged: onPageChanged,
  259. children: bodyList,
  260. physics: NeverScrollableScrollPhysics(), // 禁止滑动
  261. )
  262. );
  263. }
  264. }