index.dart 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. getMachinery(location ?? '112.087465,32.687507').then((value) {
  49. final list = <CardListModel>[];
  50. value['records'].forEach((item) {
  51. list.add(CardListModel.fromJson(item));
  52. });
  53. cardTypeList(list);
  54. EasyLoading.dismiss();
  55. });
  56. }
  57. //全部农机分类
  58. void _geList() {
  59. getMachineryType().then((value) {
  60. EasyLoading.show(status: '数据加载中...');
  61. final list = <CardTypeModel>[];
  62. List classtypeId = [];
  63. List<CardTypeModel> newTabs = [
  64. CardTypeModel.fromJson({'name': '全部'})
  65. ];
  66. value['records'].forEach((item) {
  67. list.add(CardTypeModel.fromJson(item));
  68. });
  69. list.insertAll(0, newTabs);
  70. tabText(list);
  71. tabText().forEach((item) {
  72. classtypeId.add(item.typeId);
  73. });
  74. classId(classtypeId);
  75. hasTopLoad = true;
  76. _controller = TabController(length: tabText.value.length, vsync: this);
  77. print(hasTopLoad);
  78. EasyLoading.dismiss();
  79. });
  80. }
  81. void _handleTabChange(i) {
  82. index = i;
  83. if (index == 0) {
  84. _getAllCarsList();
  85. } else {
  86. final typeId = classId.value[index];
  87. print('typeId+$typeId');
  88. EasyLoading.show(status: '数据加载中...');
  89. typeMachinery(location ?? '112.087465,32.687507', typeId.toString())
  90. .then((value) {
  91. final list = <CardListModel>[];
  92. value['records'].forEach((item) {
  93. list.add(CardListModel.fromJson(item));
  94. });
  95. cardTypeList(list);
  96. EasyLoading.dismiss();
  97. });
  98. }
  99. }
  100. @override
  101. Widget build(BuildContext context) {
  102. return Scaffold(
  103. resizeToAvoidBottomInset: false,
  104. appBar: AppBar(
  105. centerTitle: true,
  106. backgroundColor: Colors.transparent,
  107. foregroundColor: const Color(0xFF333333),
  108. elevation: 0,
  109. toolbarHeight: 44.w,
  110. title: Text(
  111. '更多',
  112. style: TextStyle(
  113. color: const Color(0xFF333333),
  114. fontWeight: FontWeight.bold,
  115. fontSize: 17.sp,
  116. letterSpacing: 2),
  117. ),
  118. ),
  119. body: SafeArea(
  120. child: Container(
  121. alignment: Alignment.center,
  122. decoration: BoxDecoration(color: Colors.white),
  123. child: !hasTopLoad
  124. ? ListView(children: [
  125. Column(
  126. children: [
  127. Container(
  128. padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
  129. width: 350.w,
  130. child: TypeHeader(
  131. type: false,
  132. ),
  133. ),
  134. Obx(() => TabBar(
  135. labelStyle: TextStyle(
  136. fontSize: ScreenUtil().setSp(19),
  137. fontWeight: FontWeight.bold),
  138. //选中的样式
  139. unselectedLabelStyle:
  140. TextStyle(fontSize: ScreenUtil().setSp(16)),
  141. //未选中的样式
  142. controller: _controller,
  143. isScrollable: true,
  144. //可滚动
  145. indicatorColor: Colors.black,
  146. //指示器的颜色
  147. labelColor: Colors.black,
  148. //选中文字颜色
  149. unselectedLabelColor: Color(0xffadadad),
  150. // indicatorSize: TabBarIndicatorSize.label, //指示器与文字等宽
  151. tabs: tabText.value
  152. .map((e) => Tab(text: e.name.toString()))
  153. .toList(),
  154. onTap: (int i) {
  155. _handleTabChange(i);
  156. },
  157. )),
  158. Container(
  159. child: Obx(() => Column(
  160. children: cardTypeList.value.length > 0
  161. ? cardTypeList()
  162. .map((item) => CarsCard(item: item))
  163. .toList()
  164. : [NullCard(text: '暂无农机信息!')],
  165. )),
  166. )
  167. ],
  168. )
  169. ])
  170. : Text('加载中...'),
  171. )));
  172. }
  173. }