DiscoveryPage.dart 5.1 KB

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