map_page.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import 'package:application/service/map_watch.dart';
  2. import 'package:application/view/home/home_controller.dart';
  3. import 'package:common_pub/model/distance.dart';
  4. import '../../../service/api.dart' as api;
  5. import '../../../utils.dart';
  6. import 'package:common_pub/model/position.dart' as m;
  7. import '../../../widget.dart';
  8. typedef MapInfo = api.ToMapSimpleV2;
  9. class MapPageController extends GetxController {
  10. @override
  11. void onInit() {
  12. super.onInit();
  13. mapGetMore();
  14. }
  15. Future<void> mapGetMore() async {
  16. if (isMapGetMoreLoading.value) {
  17. return;
  18. }
  19. isMapGetMoreLoading.value = true;
  20. await tryApi(() async {
  21. final r = await api.ApiService.to.stub.toMapListV2(api.MapListRequestV2()
  22. ..limit = 20
  23. ..offset = mapList.length);
  24. mapList.addAll(r.list);
  25. return;
  26. }, onFinally: () {
  27. isMapGetMoreLoading.value = false;
  28. });
  29. mapListScrollController.addListener(() {
  30. if (mapListScrollController.position.pixels ==
  31. mapListScrollController.position.maxScrollExtent) {
  32. //达到最大滚动位置
  33. mapGetMore();
  34. }
  35. });
  36. }
  37. final mapList = <MapInfo>[].obs;
  38. final isMapGetMoreLoading = false.obs;
  39. final mapListScrollController = ScrollController();
  40. final Rx<m.Position?> position = Rx(null);
  41. }
  42. class MapPage extends StatelessWidget {
  43. const MapPage({super.key});
  44. @override
  45. Widget build(BuildContext context) {
  46. return GetBuilder(
  47. init: MapPageController(),
  48. builder: (c) {
  49. return Container(
  50. width: double.infinity,
  51. height: double.infinity,
  52. margin: const EdgeInsets.all(20),
  53. padding: const EdgeInsets.fromLTRB(58, 28, 58, 28),
  54. decoration: BoxDecoration(
  55. color: Colors.white, borderRadius: BorderRadius.circular(16)),
  56. child: Column(
  57. children: [
  58. const Row(
  59. children: [
  60. TitlePoint(),
  61. Text(' 地图列表'),
  62. ],
  63. ),
  64. Expanded(
  65. child: Obx(() => GridView.builder(
  66. itemCount: c.mapList.length,
  67. controller: c.mapListScrollController,
  68. scrollDirection: Axis.horizontal,
  69. gridDelegate:
  70. const SliverGridDelegateWithFixedCrossAxisCount(
  71. //设置列数
  72. crossAxisCount: 2,
  73. //设置横向间距
  74. crossAxisSpacing: 10,
  75. //设置主轴间距
  76. mainAxisSpacing: 10,
  77. childAspectRatio: 1.3),
  78. itemBuilder: (context, i) {
  79. return Obx(() {
  80. final data = c.mapList[i];
  81. final id = MapWatchService.instance?.id;
  82. final s = id == MapId(data.mapId);
  83. return GalleryCardWidget(
  84. data: data,
  85. position: c.position.value,
  86. isSelected: s);
  87. });
  88. })))
  89. ],
  90. ),
  91. );
  92. });
  93. }
  94. }
  95. class GalleryCardWidget extends StatelessWidget {
  96. final MapInfo data;
  97. final m.Position? position;
  98. final bool isSelected;
  99. final HomeController c = Get.find();
  100. GalleryCardWidget(
  101. {super.key,
  102. required this.data,
  103. required this.position,
  104. required this.isSelected});
  105. void onTap(int id) async {
  106. await MapWatchService.setMapById(MapId(id));
  107. c.selectMapName.value = data.name;
  108. c.tabController.index = 2;
  109. }
  110. @override
  111. Widget build(BuildContext context) {
  112. var distance = '--';
  113. if (data.hasDistance()) {
  114. distance = Distance(m: data.distance).toString();
  115. }
  116. return GestureDetector(
  117. onTap: () => onTap(data.mapId),
  118. child: Card(
  119. color: Colors.white,
  120. shadowColor: isSelected ? Colors.red : null,
  121. surfaceTintColor: Colors.white,
  122. shape: const RoundedRectangleBorder(
  123. borderRadius: BorderRadius.all(Radius.circular(5.44))),
  124. clipBehavior: Clip.antiAlias,
  125. elevation: isSelected ? 8 : 4,
  126. child: Column(
  127. crossAxisAlignment: CrossAxisAlignment.start,
  128. children: [
  129. AspectRatio(
  130. aspectRatio: 1.1,
  131. child:
  132. AppNetImage(netImage: data.image, fit: BoxFit.fitHeight)),
  133. Expanded(
  134. child: Padding(
  135. padding: const EdgeInsets.all(6),
  136. child: Column(
  137. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  138. crossAxisAlignment: CrossAxisAlignment.start,
  139. children: [
  140. Text(
  141. data.name,
  142. style: const TextStyle(
  143. fontSize: 13.5,
  144. fontWeight: FontWeight.w500,
  145. ),
  146. textAlign: TextAlign.start,
  147. maxLines: 1,
  148. overflow: TextOverflow.ellipsis,
  149. ),
  150. Text(
  151. data.description,
  152. style: const TextStyle(
  153. fontSize: 10,
  154. color: Color(0xffc6c6c6),
  155. ),
  156. textAlign: TextAlign.start,
  157. maxLines: 1,
  158. overflow: TextOverflow.ellipsis,
  159. ),
  160. const Spacer(),
  161. DefaultTextStyle(
  162. style: const TextStyle(
  163. color: Colors.black, fontSize: 10),
  164. child: Row(
  165. crossAxisAlignment: CrossAxisAlignment.end,
  166. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  167. children: [
  168. Image.asset(Assets.imagesIcMapScale,
  169. height: 9.6),
  170. Text(' 1:${data.mapScaleNumber}'),
  171. const Spacer(),
  172. Image.asset(Assets.imagesIcLocation,
  173. height: 9.6),
  174. Text(' $distance'),
  175. ],
  176. ))
  177. ],
  178. ))),
  179. ],
  180. ),
  181. ),
  182. );
  183. }
  184. Widget wImage() {
  185. return Stack(
  186. children: [
  187. SizedBox(
  188. height: double.infinity,
  189. child: AppNetImage(netImage: data.image, fit: BoxFit.fitHeight)),
  190. ],
  191. );
  192. }
  193. }