offline_activity_page.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import '../util/NetUtils.dart';
  4. import '../model/api.dart';
  5. import '../pages/CommonWebPage.dart';
  6. // 线下活动
  7. class OfflineActivityPage extends StatefulWidget {
  8. @override
  9. State<StatefulWidget> createState() {
  10. return OfflineActivityPageState();
  11. }
  12. }
  13. class OfflineActivityPageState extends State<OfflineActivityPage> {
  14. String eventTypeLatest = "latest";
  15. String eventTypeYch = "ych";
  16. String eventTypeRec = "recommend";
  17. int curPage = 1;
  18. TextStyle titleTextStyle = TextStyle(color: Colors.black, fontSize: 18.0);
  19. List? recData, latestData, ychData;
  20. @override
  21. void initState() {
  22. super.initState();
  23. getData(eventTypeRec);
  24. getData(eventTypeLatest);
  25. getData(eventTypeYch);
  26. }
  27. void getData(String type) {
  28. String url = Api.EVENT_LIST;
  29. url += "$type?pageIndex=$curPage&pageSize=5";
  30. NetUtils.get(url).then((data) {
  31. if (data != null) {
  32. var obj = json.decode(data);
  33. if (obj != null && obj['code'] == 0) {
  34. print(obj);
  35. setState(() {
  36. if (type == eventTypeRec) {
  37. recData = obj['msg'];
  38. } else if (type == eventTypeLatest) {
  39. latestData = obj['msg'];
  40. } else {
  41. ychData = obj['msg'];
  42. }
  43. });
  44. }
  45. }
  46. });
  47. }
  48. Widget getRecBody() {
  49. if (recData == null) {
  50. return Center(child: CircularProgressIndicator());
  51. } else if (recData!.length == 0) {
  52. return Center(child: Text("暂无数据"));
  53. } else {
  54. return ListView.builder(itemBuilder: _renderRecRow, itemCount: recData!.length);
  55. }
  56. }
  57. Widget getLatestBody() {
  58. if (latestData == null) {
  59. return Center(child: CircularProgressIndicator());
  60. } else if (latestData!.length == 0) {
  61. return Center(child: Text("暂无数据"));
  62. } else {
  63. return ListView.builder(itemBuilder: _renderLatestRow, itemCount: latestData!.length);
  64. }
  65. }
  66. Widget getYchBody() {
  67. if (ychData == null) {
  68. return Center(child: CircularProgressIndicator());
  69. } else if (ychData!.length == 0) {
  70. return Center(child: Text("暂无数据"));
  71. } else {
  72. return ListView.builder(itemBuilder: _renderYchRow, itemCount: ychData!.length);
  73. }
  74. }
  75. Widget getCard(itemData) {
  76. return Card(
  77. child: Column(
  78. children: [
  79. Image.network(itemData['cover'], fit: BoxFit.cover,),
  80. Container(
  81. margin: const EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
  82. alignment: Alignment.centerLeft,
  83. child: Text(itemData['title'], style: titleTextStyle,),
  84. ),
  85. Container(
  86. margin: const EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 5.0),
  87. child: Row(
  88. children: [
  89. Expanded(child: Text(itemData['authorName']), flex: 1,),
  90. Text(itemData['timeStr'])
  91. ],
  92. )
  93. )
  94. ],
  95. ),
  96. );
  97. }
  98. Widget _renderRecRow(BuildContext ctx, int i) {
  99. Map itemData = recData![i];
  100. return InkWell(
  101. child: getCard(itemData),
  102. onTap: () {
  103. _showDetail(itemData['detailUrl']);
  104. },
  105. );
  106. }
  107. Widget _renderLatestRow(BuildContext ctx, int i) {
  108. Map itemData = latestData![i];
  109. return InkWell(
  110. child: getCard(itemData),
  111. onTap: () {
  112. _showDetail(itemData['detailUrl']);
  113. },
  114. );
  115. }
  116. Widget _renderYchRow(BuildContext ctx, int i) {
  117. Map itemData = ychData![i];
  118. return InkWell(
  119. child: getCard(itemData),
  120. onTap: () {
  121. _showDetail(itemData['detailUrl']);
  122. },
  123. );
  124. }
  125. _showDetail(detailUrl) {
  126. Navigator.of(context).push(MaterialPageRoute(
  127. builder: (ctx) {
  128. return CommonWebPage(title: '活动详情', url: detailUrl);
  129. }
  130. ));
  131. }
  132. @override
  133. Widget build(BuildContext context) {
  134. return Scaffold(
  135. appBar: AppBar(
  136. title: Text("线下活动", style: TextStyle(color: Colors.white)),
  137. iconTheme: IconThemeData(color: Colors.white),
  138. ),
  139. body: DefaultTabController(
  140. length: 3,
  141. child: Scaffold(
  142. appBar: TabBar(
  143. tabs: <Widget>[
  144. Tab(
  145. text: "强力推荐",
  146. ),
  147. Tab(
  148. text: "最新活动",
  149. ),
  150. Tab(
  151. text: "源创会",
  152. )
  153. ],
  154. ),
  155. body: TabBarView(
  156. children: [
  157. getRecBody(),
  158. getLatestBody(),
  159. getYchBody()
  160. ],
  161. )),
  162. ),
  163. );
  164. }
  165. }