DiscoveryPage.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'OfflineActivityPage.dart';
  4. import 'CommonWebPage.dart';
  5. import 'package:barcode_scan/barcode_scan.dart';
  6. class DiscoveryPage extends StatelessWidget {
  7. static const String TAG_START = "startDivider";
  8. static const String TAG_END = "endDivider";
  9. static const String TAG_CENTER = "centerDivider";
  10. static const String TAG_BLANK = "blankDivider";
  11. static const double IMAGE_ICON_WIDTH = 30.0;
  12. static const double ARROW_ICON_WIDTH = 16.0;
  13. final imagePaths = [
  14. "images/ic_discover_softwares.png",
  15. "images/ic_discover_git.png",
  16. "images/ic_discover_gist.png",
  17. "images/ic_discover_scan.png",
  18. "images/ic_discover_shake.png",
  19. "images/ic_discover_nearby.png",
  20. "images/ic_discover_pos.png",
  21. ];
  22. final titles = [
  23. "开源软件", "码云推荐", "代码片段", "扫一扫", "摇一摇", "码云封面人物", "线下活动"
  24. ];
  25. final rightArrowIcon = Image.asset('images/ic_arrow_right.png', width: ARROW_ICON_WIDTH, height: ARROW_ICON_WIDTH,);
  26. final titleTextStyle = TextStyle(fontSize: 16.0);
  27. List listData = [];
  28. DiscoveryPage() {
  29. initData();
  30. }
  31. initData() {
  32. listData.add(TAG_START);
  33. for (int i = 0; i < 3; i++) {
  34. listData.add(ListItem(title: titles[i], icon: imagePaths[i]));
  35. if (i == 2) {
  36. listData.add(TAG_END);
  37. } else {
  38. listData.add(TAG_CENTER);
  39. }
  40. }
  41. listData.add(TAG_BLANK);
  42. listData.add(TAG_START);
  43. for (int i = 3; i < 5; i++) {
  44. listData.add(ListItem(title: titles[i], icon: imagePaths[i]));
  45. if (i == 4) {
  46. listData.add(TAG_END);
  47. } else {
  48. listData.add(TAG_CENTER);
  49. }
  50. }
  51. listData.add(TAG_BLANK);
  52. listData.add(TAG_START);
  53. for (int i = 5; i < 7; i++) {
  54. listData.add(ListItem(title: titles[i], icon: imagePaths[i]));
  55. if (i == 6) {
  56. listData.add(TAG_END);
  57. } else {
  58. listData.add(TAG_CENTER);
  59. }
  60. }
  61. }
  62. Widget getIconImage(path) {
  63. return Padding(
  64. padding: const EdgeInsets.fromLTRB(0.0, 0.0, 10.0, 0.0),
  65. child: Image.asset(path, width: IMAGE_ICON_WIDTH, height: IMAGE_ICON_WIDTH),
  66. );
  67. }
  68. renderRow(BuildContext ctx, int i) {
  69. var item = listData[i];
  70. if (item is String) {
  71. switch (item) {
  72. case TAG_START:
  73. return Divider(height: 1.0,);
  74. break;
  75. case TAG_END:
  76. return Divider(height: 1.0,);
  77. break;
  78. case TAG_CENTER:
  79. return Padding(
  80. padding: const EdgeInsets.fromLTRB(50.0, 0.0, 0.0, 0.0),
  81. child: Divider(height: 1.0,),
  82. );
  83. break;
  84. case TAG_BLANK:
  85. return Container(
  86. height: 20.0,
  87. );
  88. break;
  89. }
  90. } else if (item is ListItem) {
  91. var listItemContent = Padding(
  92. padding: const EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 15.0),
  93. child: Row(
  94. children: [
  95. getIconImage(item.icon),
  96. Expanded(
  97. child: Text(item.title, style: titleTextStyle,)
  98. ),
  99. rightArrowIcon
  100. ],
  101. ),
  102. );
  103. return InkWell(
  104. onTap: () {
  105. handleListItemClick(ctx, item);
  106. },
  107. child: listItemContent,
  108. );
  109. }
  110. }
  111. void handleListItemClick(BuildContext ctx, ListItem item) {
  112. String title = item.title;
  113. if (title == "扫一扫") {
  114. scan();
  115. } else if (title == "线下活动") {
  116. Navigator.of(ctx).push(MaterialPageRoute(
  117. builder: (context) {
  118. return OfflineActivityPage();
  119. }
  120. ));
  121. } else if (title == "码云推荐") {
  122. Navigator.of(ctx).push(MaterialPageRoute(
  123. builder: (context) {
  124. return CommonWebPage(title: "码云推荐", url: "https://m.gitee.com/explore");
  125. }
  126. ));
  127. } else if (title == "代码片段") {
  128. Navigator.of(ctx).push(MaterialPageRoute(
  129. builder: (context) {
  130. return CommonWebPage(title: "代码片段", url: "https://m.gitee.com/gists");
  131. }
  132. ));
  133. } else if (title == "开源软件") {
  134. Navigator.of(ctx).push(MaterialPageRoute(
  135. builder: (context) {
  136. return CommonWebPage(title: "开源软件", url: "https://m.gitee.com/explore");
  137. }
  138. ));
  139. } else if (title == "码云封面人物") {
  140. Navigator.of(ctx).push(MaterialPageRoute(
  141. builder: (context) {
  142. return CommonWebPage(title: "码云封面人物", url: "https://m.gitee.com/gitee-stars/");
  143. }
  144. ));
  145. }
  146. }
  147. Future scan() async {
  148. try {
  149. String barcode = await BarcodeScanner.scan();
  150. print(barcode);
  151. } on Exception catch (e) {
  152. print(e);
  153. }
  154. }
  155. @override
  156. Widget build(BuildContext context) {
  157. return Padding(
  158. padding: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
  159. child: ListView.builder(
  160. itemCount: listData.length,
  161. itemBuilder: (context, i) => renderRow(context, i),
  162. ),
  163. );
  164. }
  165. }
  166. class ListItem {
  167. String icon;
  168. String title;
  169. ListItem({this.icon, this.title});
  170. }