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.

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