index.dart 5.8KB

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