index.dart 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import 'dart:convert';
  2. import 'package:farmer_client/pages/home/widgets/home/widgets/headers.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_easyloading/flutter_easyloading.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. import 'package:get/get.dart';
  7. import '../../models/app.dart';
  8. import '../../models/entities/CardListModel.dart';
  9. import '../../models/entities/CardTypeModel.dart';
  10. import '../../services/homeAPI.dart';
  11. import '../../widgets/CarsCard.dart';
  12. import '../../widgets/NullCard.dart';
  13. import '../../widgets/Search.dart';
  14. class MoreCars extends StatefulWidget {
  15. const MoreCars({Key? key}) : super(key: key);
  16. @override
  17. State<MoreCars> createState() => _MoreCarsState();
  18. }
  19. class _MoreCarsState extends State<MoreCars> with TickerProviderStateMixin {
  20. late TabController _controller;
  21. final tabText = Rx<List<CardTypeModel>>([]);
  22. final classId = Rx<List<dynamic>>([]);
  23. final cardTypeList = Rx<List<CardListModel>>([]);
  24. String? location = AppController.t.locationStr;
  25. late int _selectedIndex;
  26. bool hasTopLoad = false;
  27. int index = 0;
  28. @override
  29. void initState() {
  30. // TODO: implement initState
  31. super.initState();
  32. _controller = TabController(length: tabText.value.length, vsync: this);
  33. // _controller.addListener(() {
  34. // setState(() => _selectedIndex = _controller.index);
  35. // print("liucheng-> ${_controller.indexIsChanging}");
  36. // });
  37. _geList();
  38. _getAllCarsList();
  39. }
  40. @override
  41. void dispose() {
  42. super.dispose();
  43. _controller.dispose();
  44. }
  45. //进来拿到全部农机
  46. void _getAllCarsList() {
  47. EasyLoading.show(status: '数据加载中...');
  48. Map<String, dynamic> params = {'pageNum': 1,'location':location};
  49. getMachinery(params).then((value) {
  50. final list = <CardListModel>[];
  51. value['records'].forEach((item) {
  52. list.add(CardListModel.fromJson(item));
  53. });
  54. cardTypeList(list);
  55. EasyLoading.dismiss();
  56. });
  57. }
  58. //全部农机分类
  59. void _geList() {
  60. getMachineryType().then((value) {
  61. EasyLoading.show(status: '数据加载中...');
  62. final list = <CardTypeModel>[];
  63. List classtypeId = [];
  64. List<CardTypeModel> newTabs = [
  65. CardTypeModel.fromJson({'name': '全部'})
  66. ];
  67. value['records'].forEach((item) {
  68. list.add(CardTypeModel.fromJson(item));
  69. });
  70. list.insertAll(0, newTabs);
  71. tabText(list);
  72. tabText().forEach((item) {
  73. classtypeId.add(item.typeId);
  74. });
  75. classId(classtypeId);
  76. hasTopLoad = true;
  77. _controller = TabController(length: tabText.value.length, vsync: this);
  78. print(hasTopLoad);
  79. EasyLoading.dismiss();
  80. });
  81. }
  82. void _handleTabChange(i) {
  83. index = i;
  84. if (index == 0) {
  85. _getAllCarsList();
  86. } else {
  87. final typeId = classId.value[index];
  88. print('typeId+$typeId');
  89. EasyLoading.show(status: '数据加载中...');
  90. typeMachinery(location ?? '112.087465,32.687507', typeId.toString())
  91. .then((value) {
  92. final list = <CardListModel>[];
  93. value['records'].forEach((item) {
  94. list.add(CardListModel.fromJson(item));
  95. });
  96. cardTypeList(list);
  97. EasyLoading.dismiss();
  98. });
  99. }
  100. }
  101. @override
  102. Widget build(BuildContext context) {
  103. return Scaffold(
  104. resizeToAvoidBottomInset: false,
  105. appBar: AppBar(
  106. centerTitle: true,
  107. backgroundColor: Colors.transparent,
  108. foregroundColor: const Color(0xFF333333),
  109. elevation: 0,
  110. toolbarHeight: 44.w,
  111. title: Text(
  112. '更多',
  113. style: TextStyle(
  114. color: const Color(0xFF333333),
  115. fontWeight: FontWeight.bold,
  116. fontSize: 17.sp,
  117. letterSpacing: 2),
  118. ),
  119. ),
  120. body: SafeArea(
  121. child: Container(
  122. alignment: Alignment.center,
  123. decoration: BoxDecoration(color: Colors.white),
  124. child: !hasTopLoad
  125. ? ListView(children: [
  126. Column(
  127. children: [
  128. Container(
  129. padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
  130. width: 350.w,
  131. child: TypeHeader(
  132. type: false,
  133. ),
  134. ),
  135. Obx(() => TabBar(
  136. labelStyle: TextStyle(
  137. fontSize: ScreenUtil().setSp(19),
  138. fontWeight: FontWeight.bold),
  139. //选中的样式
  140. unselectedLabelStyle:
  141. TextStyle(fontSize: ScreenUtil().setSp(16)),
  142. //未选中的样式
  143. controller: _controller,
  144. isScrollable: true,
  145. //可滚动
  146. indicatorColor: Colors.black,
  147. //指示器的颜色
  148. labelColor: Colors.black,
  149. //选中文字颜色
  150. unselectedLabelColor: Color(0xffadadad),
  151. // indicatorSize: TabBarIndicatorSize.label, //指示器与文字等宽
  152. tabs: tabText.value
  153. .map((e) => Tab(text: e.name.toString()))
  154. .toList(),
  155. onTap: (int i) {
  156. _handleTabChange(i);
  157. },
  158. )),
  159. Container(
  160. child: Obx(() => Column(
  161. children: cardTypeList.value.length > 0
  162. ? cardTypeList()
  163. .map((item) => CarsCard(item: item))
  164. .toList()
  165. : [NullCard(text: '暂无农机信息!')],
  166. )),
  167. )
  168. ],
  169. )
  170. ])
  171. : Text('加载中...'),
  172. )));
  173. }
  174. }