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.

234 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. ),
  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 Center(child: Text(label_list_empty));
  89. }
  90. return RefreshIndicator(
  91. child: ListView.builder(
  92. physics: AlwaysScrollableScrollPhysics(),
  93. itemBuilder: (BuildContext context, int index) {
  94. return index >= (state.items ?? []).length ? BottomLoader() : ItemInfinityWidget(item: state.items?[index]);
  95. },
  96. itemCount: (state.hasReachedMax ?? false) ? (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 TbCropDTO 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 GestureDetector(
  138. child: Container(
  139. margin: 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: Offset(0, 3))]),
  145. padding: 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: TextStyle(color: Colors.white)),
  163. ),
  164. bottom: 0,
  165. )
  166. ],
  167. ),
  168. ),
  169. 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. Expanded(
  181. child: SizedBox(),
  182. ),
  183. Row(
  184. children: [
  185. Icon(
  186. Icons.access_time,
  187. size: 16,
  188. color: textColor,
  189. ),
  190. 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. Navigator.push(
  207. context,
  208. MaterialPageRoute(
  209. builder: (BuildContext context) => PlotDetailScreen(
  210. cropId: item.id ?? -1,
  211. initialIndex: 0,
  212. cropType: item.tbCropTypeId ?? -1,
  213. ),
  214. ),
  215. );
  216. });
  217. }
  218. }