map_page.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(data: data, position: c.position.value, isSelected: s);
  84. }) ;
  85. })))
  86. ],
  87. ),
  88. );
  89. });
  90. }
  91. }
  92. class GalleryCardWidget extends StatelessWidget {
  93. final MapInfo data;
  94. final m.Position? position;
  95. final bool isSelected;
  96. final HomeController c = Get.find();
  97. GalleryCardWidget({
  98. super.key,
  99. required this.data,
  100. required this.position, required this.isSelected});
  101. void onTap(int id) async {
  102. await MapWatchService.setMapById(MapId(id));
  103. c.selectMapName.value = data.name;
  104. }
  105. @override
  106. Widget build(BuildContext context) {
  107. var distance = '--';
  108. if ( data.hasDistance()) {
  109. distance = Distance(m: data.distance).toString();
  110. }
  111. return GestureDetector(
  112. onTap: ()=>onTap(data.mapId),
  113. child: Card(
  114. color: Colors.white,
  115. shadowColor: isSelected? Colors.red: null,
  116. surfaceTintColor: Colors.white,
  117. shape: const RoundedRectangleBorder(
  118. borderRadius: BorderRadius.all(Radius.circular(5.44))),
  119. clipBehavior: Clip.antiAlias,
  120. elevation: isSelected?8:4,
  121. child: Column(
  122. crossAxisAlignment: CrossAxisAlignment.start,
  123. children: [
  124. AspectRatio(aspectRatio: 1.1, child: AppNetImage(netImage: data.image, fit: BoxFit.fitHeight)),
  125. Expanded(
  126. child: Padding(
  127. padding: const EdgeInsets.all(6),
  128. child: Column(
  129. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  130. crossAxisAlignment: CrossAxisAlignment.start,
  131. children: [
  132. Text(
  133. data.name,
  134. style: const TextStyle(
  135. fontSize: 13.5,
  136. fontWeight: FontWeight.w500,
  137. ),
  138. textAlign: TextAlign.start,
  139. maxLines: 1,
  140. overflow: TextOverflow.ellipsis,
  141. ),
  142. Text(
  143. data.description,
  144. style: const TextStyle(
  145. fontSize: 10,
  146. color: Color(0xffc6c6c6),
  147. ),
  148. textAlign: TextAlign.start,
  149. maxLines: 1,
  150. overflow: TextOverflow.ellipsis,
  151. ),
  152. const Spacer(),
  153. DefaultTextStyle(
  154. style: const TextStyle(
  155. color: Colors.black, fontSize: 10),
  156. child: Row(
  157. crossAxisAlignment: CrossAxisAlignment.end,
  158. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  159. children: [
  160. Image.asset(Assets.imagesIcMapScale,
  161. height: 9.6),
  162. Text(' 1:${data.mapScaleNumber}'),
  163. const Spacer(),
  164. Image.asset(Assets.imagesIcLocation,
  165. height: 9.6),
  166. Text(' $distance'),
  167. ],
  168. ))
  169. ],
  170. ))),
  171. ],
  172. ),
  173. ),
  174. );
  175. }
  176. Widget wImage() {
  177. return Stack(
  178. children: [
  179. SizedBox(
  180. height: double.infinity,
  181. child: AppNetImage(netImage: data.image, fit: BoxFit.fitHeight)
  182. ),
  183. ],
  184. );
  185. }
  186. }