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.

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