main.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/MyInfoPage.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. Text getTabTitle(int curIndex) {
  69. return Text(appBarTitles[curIndex], style: getTabTextStyle(curIndex));
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. initData();
  74. return MaterialApp(
  75. theme: ThemeData(
  76. primaryColor: const Color(0xFF63CA6C)
  77. ),
  78. home: Scaffold(
  79. appBar: AppBar(
  80. title: Text(appBarTitles[_tabIndex], style: TextStyle(color: Colors.white)),
  81. iconTheme: IconThemeData(color: Colors.white)
  82. ),
  83. body: _body,
  84. bottomNavigationBar: CupertinoTabBar(
  85. items: <BottomNavigationBarItem>[
  86. BottomNavigationBarItem(
  87. icon: getTabIcon(0),
  88. title: getTabTitle(0)),
  89. BottomNavigationBarItem(
  90. icon: getTabIcon(1),
  91. title: getTabTitle(1)),
  92. BottomNavigationBarItem(
  93. icon: getTabIcon(2),
  94. title: getTabTitle(2)),
  95. BottomNavigationBarItem(
  96. icon: getTabIcon(3),
  97. title: getTabTitle(3)),
  98. ],
  99. currentIndex: _tabIndex,
  100. onTap: (index) {
  101. setState((){
  102. _tabIndex = index;
  103. });
  104. },
  105. ),
  106. drawer: MyDrawer(),
  107. ),
  108. );
  109. }
  110. }