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.

195 lines
6.6KB

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