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.

201 lines
6.4KB

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