You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
7.7KB

  1. import 'package:farm_tpf/data/repository/repository.dart';
  2. import 'package:farm_tpf/models/index.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  5. import 'package:farm_tpf/presentation/screens/plot/widget_search.dart';
  6. import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_detail.dart';
  7. import 'package:farm_tpf/utils/const_assets.dart';
  8. import 'package:farm_tpf/utils/const_color.dart';
  9. import 'package:farm_tpf/utils/pref.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter_bloc/flutter_bloc.dart';
  12. import 'package:farm_tpf/utils/const_string.dart';
  13. import 'package:farm_tpf/utils/formatter.dart';
  14. import 'package:get/get.dart';
  15. import 'bloc/plot_bloc.dart';
  16. class PlotListScreen extends StatefulWidget {
  17. @override
  18. _PlotListScreenState createState() => _PlotListScreenState();
  19. }
  20. class _PlotListScreenState extends State<PlotListScreen> {
  21. var pref = LocalPref();
  22. var token;
  23. var client;
  24. String pushkey = "";
  25. String currentFullName = "";
  26. @override
  27. void initState() {
  28. super.initState();
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. return BlocProvider(
  33. create: (context) => PlotBloc(repository: Repository())..add(DataFetched()),
  34. child: HoldInfinityWidget(),
  35. );
  36. }
  37. }
  38. class HoldInfinityWidget extends StatelessWidget {
  39. final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  40. @override
  41. Widget build(BuildContext context) {
  42. return Scaffold(backgroundColor: Colors.white, key: _scaffoldKey, body: SafeArea(child: InfinityView()));
  43. }
  44. }
  45. class InfinityView extends StatefulWidget {
  46. @override
  47. _InfinityViewState createState() => _InfinityViewState();
  48. }
  49. class _InfinityViewState extends State<InfinityView> {
  50. final _scrollController = ScrollController();
  51. final _scrollThreshold = 250.0;
  52. late PlotBloc _plotBloc;
  53. @override
  54. void initState() {
  55. _scrollController.addListener(() {
  56. final maxScroll = _scrollController.position.maxScrollExtent;
  57. final currentScroll = _scrollController.position.pixels;
  58. if (maxScroll - currentScroll < _scrollThreshold) {
  59. _plotBloc.add(DataFetched());
  60. }
  61. });
  62. _plotBloc = BlocProvider.of<PlotBloc>(context);
  63. super.initState();
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return Column(
  68. crossAxisAlignment: CrossAxisAlignment.start,
  69. children: <Widget>[
  70. const SizedBox(
  71. height: 8,
  72. ),
  73. Container(
  74. padding: const EdgeInsets.all(8),
  75. color: Colors.white,
  76. child: const Text(
  77. 'Danh sách lô trồng',
  78. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
  79. )),
  80. WidgetSearch(),
  81. Expanded(child: BlocBuilder<PlotBloc, PlotState>(
  82. builder: (context, state) {
  83. if (state is PlotFailure) {
  84. return Center(child: Text(state.errorString));
  85. }
  86. if (state is PlotSuccess) {
  87. if (state.items.isEmpty) {
  88. return const Center(child: Text(label_list_empty));
  89. }
  90. return RefreshIndicator(
  91. child: ListView.builder(
  92. physics: const AlwaysScrollableScrollPhysics(),
  93. itemBuilder: (BuildContext context, int index) {
  94. return index >= state.items.length ? BottomLoader() : ItemInfinityWidget(item: state.items[index]);
  95. },
  96. itemCount: state.hasReachedMax ? state.items.length : state.items.length + 1,
  97. controller: _scrollController,
  98. ),
  99. onRefresh: () async {
  100. _plotBloc.add(OnRefresh());
  101. });
  102. }
  103. return Center(
  104. child: LoadingListPage(),
  105. );
  106. },
  107. ))
  108. ],
  109. );
  110. }
  111. @override
  112. void dispose() {
  113. _scrollController.dispose();
  114. super.dispose();
  115. }
  116. }
  117. class ItemInfinityWidget extends StatelessWidget {
  118. final Crop item;
  119. const ItemInfinityWidget({Key? key, required this.item}) : super(key: key);
  120. @override
  121. Widget build(BuildContext context) {
  122. var backgroundColor;
  123. var textColor;
  124. switch (item.status) {
  125. case "STATUS_ARE_ACTIVE":
  126. backgroundColor = Colors.white;
  127. textColor = AppColors.DEFAULT;
  128. break;
  129. case "STATUS_FINISHED":
  130. backgroundColor = AppColors.DEFAULT;
  131. textColor = Colors.white;
  132. break;
  133. default:
  134. backgroundColor = Colors.white;
  135. textColor = Colors.black;
  136. }
  137. return InkWell(
  138. child: Container(
  139. margin: const EdgeInsets.all(8),
  140. decoration: BoxDecoration(
  141. color: backgroundColor,
  142. border: Border.all(color: Colors.grey, width: 0.35),
  143. borderRadius: BorderRadius.circular(8),
  144. boxShadow: [BoxShadow(color: Colors.grey, blurRadius: 3, offset: const Offset(0, 3))]),
  145. padding: const EdgeInsets.all(8),
  146. child: Row(
  147. children: [
  148. Container(
  149. child: Stack(
  150. children: [
  151. Image.asset(
  152. AppAssets.tempImage,
  153. width: 65,
  154. height: 65,
  155. ),
  156. Positioned(
  157. child: Container(
  158. color: Colors.grey.withOpacity(0.5),
  159. width: 65,
  160. height: 20,
  161. child: Text(item.areaM2?.formatNumtoStringDecimal().toString() ?? '' + " m\u00B2",
  162. textAlign: TextAlign.center, style: const TextStyle(color: Colors.white)),
  163. ),
  164. bottom: 0,
  165. )
  166. ],
  167. ),
  168. ),
  169. const SizedBox(
  170. width: 12,
  171. ),
  172. Expanded(
  173. child: Container(
  174. height: 75,
  175. child: Column(
  176. crossAxisAlignment: CrossAxisAlignment.start,
  177. mainAxisSize: MainAxisSize.min,
  178. children: [
  179. Text("${item.code ?? ''} - ${item.suppliesName ?? ''}", style: TextStyle(color: textColor, fontWeight: FontWeight.bold)),
  180. const Expanded(
  181. child: SizedBox(),
  182. ),
  183. Row(
  184. children: [
  185. Icon(
  186. Icons.access_time,
  187. size: 16,
  188. color: textColor,
  189. ),
  190. const SizedBox(
  191. width: 4,
  192. ),
  193. Expanded(
  194. child: Text(item.startDate?.format_DDMMYY_HHmm().toString() ?? '', style: TextStyle(color: textColor)),
  195. ),
  196. ],
  197. )
  198. ],
  199. ),
  200. ),
  201. )
  202. ],
  203. ),
  204. ),
  205. onTap: () {
  206. // Get.to(
  207. // () => PlotDetailScreen(
  208. // cropId: item.id?.toInt(),
  209. // initialIndex: 0,
  210. // cropType: item.type?.toInt() ?? -1,
  211. // ),
  212. // );
  213. Navigator.push(
  214. context,
  215. MaterialPageRoute(
  216. builder: (BuildContext context) => PlotDetailScreen(
  217. cropId: item.id?.toInt(),
  218. initialIndex: 0,
  219. cropType: item.type?.toInt() ?? -1,
  220. ),
  221. ),
  222. );
  223. });
  224. }
  225. }