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.

212 lines
7.0KB

  1. import 'package:farm_tpf/custom_model/SuppliesUsing.dart';
  2. import 'package:farm_tpf/custom_model/Supply.dart';
  3. import 'package:farm_tpf/data/repository/repository.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  7. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  8. import 'package:farm_tpf/presentation/screens/resources/bloc/supply_bloc.dart';
  9. import 'package:farm_tpf/presentation/screens/resources/widget_search.dart';
  10. import 'package:farm_tpf/utils/const_string.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:flutter_bloc/flutter_bloc.dart';
  13. import 'package:get/get.dart';
  14. import 'package:farm_tpf/utils/formatter.dart';
  15. class ResourceHelperScreen extends StatefulWidget {
  16. final String type;
  17. final int selectedId;
  18. final String titleName;
  19. final List<SuppliesUsing> currentItems;
  20. final int currentEditId;
  21. ResourceHelperScreen(
  22. {@required this.type,
  23. @required this.selectedId,
  24. @required this.titleName,
  25. @required this.currentItems,
  26. @required this.currentEditId});
  27. @override
  28. _ResourceHelperScreenState createState() => _ResourceHelperScreenState();
  29. }
  30. class _ResourceHelperScreenState extends State<ResourceHelperScreen> {
  31. @override
  32. Widget build(BuildContext context) {
  33. return BlocProvider(
  34. create: (context) => SupplyBloc(repository: Repository())
  35. ..add(DataFetched(type: widget.type, selectedId: widget.selectedId)),
  36. child: HoldInfinityWidget(
  37. selectedId: widget.selectedId,
  38. type: widget.type,
  39. titleName: widget.titleName,
  40. currentItems: widget.currentItems,
  41. currentEditId: widget.currentEditId,
  42. ),
  43. );
  44. }
  45. }
  46. class HoldInfinityWidget extends StatelessWidget {
  47. final int selectedId;
  48. final String type;
  49. final String titleName;
  50. final List<SuppliesUsing> currentItems;
  51. final int currentEditId;
  52. HoldInfinityWidget(
  53. {@required this.selectedId,
  54. @required this.type,
  55. @required this.titleName,
  56. @required this.currentItems,
  57. @required this.currentEditId});
  58. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  59. @override
  60. Widget build(BuildContext context) {
  61. return Scaffold(
  62. backgroundColor: Colors.white,
  63. key: _scaffoldKey,
  64. appBar: AppBarWidget(),
  65. body: Column(
  66. crossAxisAlignment: CrossAxisAlignment.start,
  67. children: <Widget>[
  68. Padding(
  69. padding: const EdgeInsets.all(8.0),
  70. child: Text(
  71. '$titleName',
  72. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
  73. ),
  74. ),
  75. WidgetSearch(
  76. type: type,
  77. selectedId: selectedId,
  78. ),
  79. Expanded(
  80. child: InfinityView(
  81. selectedId: selectedId,
  82. type: type,
  83. currentItems: currentItems,
  84. currentEditId: currentEditId,
  85. ))
  86. ],
  87. ));
  88. }
  89. }
  90. class InfinityView extends StatefulWidget {
  91. final int selectedId;
  92. final String type;
  93. final List<SuppliesUsing> currentItems;
  94. final int currentEditId;
  95. InfinityView(
  96. {@required this.selectedId,
  97. @required this.type,
  98. @required this.currentItems,
  99. @required this.currentEditId});
  100. @override
  101. _InfinityViewState createState() => _InfinityViewState();
  102. }
  103. class _InfinityViewState extends State<InfinityView> {
  104. SupplyBloc _supplyBloc;
  105. @override
  106. void initState() {
  107. _supplyBloc = BlocProvider.of<SupplyBloc>(context);
  108. _supplyBloc
  109. .add(DataFetched(type: widget.type, selectedId: widget.selectedId));
  110. super.initState();
  111. }
  112. @override
  113. Widget build(BuildContext context) {
  114. return BlocBuilder<SupplyBloc, SupplyState>(
  115. builder: (context, state) {
  116. if (state is SupplyFailure) {
  117. return Center(child: Text(label_error_get_data));
  118. }
  119. if (state is SupplySuccess) {
  120. if (state.items.isEmpty) {
  121. return Center(child: Text(label_list_empty));
  122. }
  123. return RefreshIndicator(
  124. child: ListView.builder(
  125. itemBuilder: (BuildContext context, int index) {
  126. return index >= state.items.length
  127. ? BottomLoader()
  128. : ItemInfinityWidget(
  129. item: state.items[index],
  130. currentItems: widget.currentItems,
  131. currentEditId: widget.currentEditId,
  132. );
  133. },
  134. itemCount: state.items.length),
  135. onRefresh: () async {
  136. _supplyBloc.add(OnRefresh(
  137. type: widget.type, selectedId: widget.selectedId));
  138. });
  139. }
  140. return Center(
  141. child: LoadingListPage(),
  142. );
  143. },
  144. );
  145. }
  146. @override
  147. void dispose() {
  148. super.dispose();
  149. }
  150. }
  151. class ItemInfinityWidget extends StatelessWidget {
  152. final Supply item;
  153. final List<SuppliesUsing> currentItems;
  154. final int currentEditId;
  155. const ItemInfinityWidget(
  156. {Key key,
  157. @required this.item,
  158. @required this.currentItems,
  159. @required this.currentEditId})
  160. : super(key: key);
  161. @override
  162. Widget build(BuildContext context) {
  163. return GestureDetector(
  164. child: Container(
  165. decoration: BoxDecoration(
  166. border: Border(
  167. bottom: BorderSide(color: Colors.grey, width: 0.35))),
  168. child: RadioListTile(
  169. title: Row(
  170. children: [
  171. Expanded(child: Text(item.tbSuppliesName.toString())),
  172. Text(
  173. "${item.quantity.formatNumtoStringDecimal()} ${item.unit}")
  174. ],
  175. ),
  176. subtitle: Text(item.tbWarehouseName.toString()),
  177. value: item,
  178. groupValue: item.isSelected == false ? null : item,
  179. onChanged: (Supply value) {
  180. if (value.quantity <= 0) {
  181. Utils.showSnackBarWarning(message: "Vật tư đã hết");
  182. } else {
  183. var editedId = (currentEditId > 0) ? currentEditId : -1;
  184. if (currentItems
  185. .map((e) => e.tbSuppliesInWarehouseId)
  186. .contains(item.id) &&
  187. item.id != editedId) {
  188. Utils.showSnackBarWarning(
  189. message:
  190. "Vật tư đã được thêm, vui lòng chọn vật tư khác");
  191. } else {
  192. //close nackbar if open
  193. if (Get.isSnackbarOpen) Get.back();
  194. Navigator.of(context).pop(value);
  195. }
  196. }
  197. })),
  198. onTap: () {});
  199. }
  200. }