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.

204 lines
6.5KB

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