index.dart 5.8KB

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