main.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'pages/NewsListPage.dart';
  4. import 'pages/TweetsListPage.dart';
  5. import 'pages/DiscoveryPage.dart';
  6. import 'pages/my_info_page.dart';
  7. import './widgets/MyDrawer.dart';
  8. void main() {
  9. runApp(MyOSCClient());
  10. }
  11. class MyOSCClient extends StatefulWidget {
  12. @override
  13. State<StatefulWidget> createState() => MyOSCClientState();
  14. }
  15. class MyOSCClientState extends State<MyOSCClient> {
  16. int _tabIndex = 0;
  17. final tabTextStyleNormal = TextStyle(color: const Color(0xff969696));
  18. final tabTextStyleSelected = TextStyle(color: const Color(0xff63ca6c));
  19. var tabImages;
  20. var _body;
  21. var appBarTitles = ['资讯', '动弹', '发现', '我的'];
  22. Image getTabImage(path) {
  23. return Image.asset(path, width: 20.0, height: 20.0);
  24. }
  25. void initData() {
  26. if (tabImages == null) {
  27. tabImages = [
  28. [
  29. getTabImage('images/ic_nav_news_normal.png'),
  30. getTabImage('images/ic_nav_news_actived.png')
  31. ],
  32. [
  33. getTabImage('images/ic_nav_tweet_normal.png'),
  34. getTabImage('images/ic_nav_tweet_actived.png')
  35. ],
  36. [
  37. getTabImage('images/ic_nav_discover_normal.png'),
  38. getTabImage('images/ic_nav_discover_actived.png')
  39. ],
  40. [
  41. getTabImage('images/ic_nav_my_normal.png'),
  42. getTabImage('images/ic_nav_my_pressed.png')
  43. ]
  44. ];
  45. }
  46. _body = IndexedStack(
  47. children: [
  48. NewsListPage(),
  49. TweetsListPage(),
  50. DiscoveryPage(),
  51. MyInfoPage()
  52. ],
  53. index: _tabIndex,
  54. );
  55. }
  56. TextStyle getTabTextStyle(int curIndex) {
  57. if (curIndex == _tabIndex) {
  58. return tabTextStyleSelected;
  59. }
  60. return tabTextStyleNormal;
  61. }
  62. Image? getTabIcon(int curIndex) {
  63. if (curIndex == _tabIndex) {
  64. return tabImages[curIndex][1];
  65. }
  66. return tabImages[curIndex][0];
  67. }
  68. String getTabTitle(int curIndex) {
  69. return appBarTitles[
  70. curIndex]; //Text(appBarTitles[curIndex], style: getTabTextStyle(curIndex));
  71. }
  72. @override
  73. Widget build(BuildContext context) {
  74. initData();
  75. return MaterialApp(
  76. theme: ThemeData(primaryColor: const Color(0xFF63CA6C)),
  77. home: Scaffold(
  78. appBar: AppBar(
  79. title: Text(appBarTitles[_tabIndex],
  80. style: TextStyle(color: Colors.white)),
  81. iconTheme: IconThemeData(color: Colors.white)),
  82. body: _body,
  83. bottomNavigationBar: CupertinoTabBar(
  84. items: <BottomNavigationBarItem>[
  85. BottomNavigationBarItem(icon: getTabIcon(0)!, label: getTabTitle(0)),
  86. BottomNavigationBarItem(icon: getTabIcon(1)!, label: getTabTitle(1)),
  87. BottomNavigationBarItem(icon: getTabIcon(2)!, label: getTabTitle(2)),
  88. BottomNavigationBarItem(icon: getTabIcon(3)!, label: getTabTitle(3)),
  89. ],
  90. currentIndex: _tabIndex,
  91. onTap: (index) {
  92. setState(() {
  93. _tabIndex = index;
  94. });
  95. },
  96. ),
  97. drawer: MyDrawer(),
  98. ),
  99. );
  100. }
  101. }