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.

233 lines
7.4KB

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