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.

207 lines
6.6KB

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