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.

726 lines
31KB

  1. import 'dart:convert';
  2. import 'package:farm_tpf/custom_model/Media.dart';
  3. import 'package:farm_tpf/custom_model/Nursery.dart';
  4. import 'package:farm_tpf/custom_model/NurseryDetail.dart';
  5. import 'package:farm_tpf/data/api/app_exception.dart';
  6. import 'package:farm_tpf/data/repository/repository.dart';
  7. import 'package:farm_tpf/models/index.dart';
  8. import 'package:farm_tpf/presentation/custom_widgets/bloc/media_helper_bloc.dart';
  9. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  10. import 'package:farm_tpf/presentation/custom_widgets/widget_media_picker.dart';
  11. import 'package:farm_tpf/presentation/screens/actions/bloc/action_detail_bloc.dart';
  12. import 'package:farm_tpf/presentation/screens/actions/nursery/bloc/expansion_list_bloc.dart';
  13. import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_file_controller.dart';
  14. import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_supply.dart';
  15. import 'package:farm_tpf/presentation/screens/resources/sc_resource_helper.dart';
  16. import 'package:farm_tpf/utils/bloc/bloc/status_add_form_bloc.dart';
  17. import 'package:farm_tpf/utils/const_color.dart';
  18. import 'package:farm_tpf/utils/const_common.dart';
  19. import 'package:farm_tpf/utils/const_string.dart';
  20. import 'package:farm_tpf/utils/const_style.dart';
  21. import 'package:farm_tpf/utils/pref.dart';
  22. import 'package:farm_tpf/utils/validators.dart';
  23. import 'package:flutter/material.dart';
  24. import 'package:flutter_bloc/flutter_bloc.dart';
  25. import 'package:flutter_cache_manager/flutter_cache_manager.dart';
  26. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  27. import 'package:get/get.dart';
  28. import 'package:get/state_manager.dart';
  29. import 'package:intl/intl.dart';
  30. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  31. import 'package:mime/mime.dart';
  32. import 'package:pattern_formatter/pattern_formatter.dart';
  33. import 'package:farm_tpf/utils/formatter.dart';
  34. class EditActionNurseryScreen extends StatefulWidget {
  35. final int cropId;
  36. final bool isEdit;
  37. final int activityId;
  38. EditActionNurseryScreen(
  39. {@required this.cropId, this.isEdit = false, this.activityId});
  40. @override
  41. _EditActionNurseryState createState() => _EditActionNurseryState();
  42. }
  43. class _EditActionNurseryState extends State<EditActionNurseryScreen> {
  44. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  45. final _repository = Repository();
  46. GlobalKey<FormState> _formKey = GlobalKey();
  47. bool _autoValidate = false;
  48. Nursery _nursery = Nursery();
  49. var pref = LocalPref();
  50. TextEditingController _substrateController = TextEditingController();
  51. TextEditingController _seedLengthController = TextEditingController();
  52. TextEditingController _quantityController = TextEditingController();
  53. TextEditingController _seedIncubationTimeController = TextEditingController();
  54. TextEditingController _descriptionController = TextEditingController();
  55. TextEditingController _workerNameController = TextEditingController();
  56. TextEditingController _trayNumberController = TextEditingController();
  57. final _executeByController = TextEditingController();
  58. String executeTimeView;
  59. DateTime executeTime = DateTime.now();
  60. List<NurseryDetail> currentNurseryDetail = List<NurseryDetail>();
  61. int currentIndexUpdate = -1;
  62. bool isResetForm = true;
  63. final changeSupply = Get.put(ChangeSupply());
  64. int selectedSupplyId = -1;
  65. List<String> filePaths = List<String>();
  66. var changeFileController = Get.put(ChangeFileController());
  67. Future<Null> getSharedPrefs() async {
  68. var currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME);
  69. _executeByController.text = currentFullName ?? "";
  70. }
  71. @override
  72. void initState() {
  73. super.initState();
  74. getSharedPrefs();
  75. changeSupply.initValue();
  76. changeFileController.initValue();
  77. _nursery.nurseryDetail = new List<NurseryDetail>();
  78. var parsedExecuteDate =
  79. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
  80. _nursery.executeDate = "$parsedExecuteDate";
  81. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime);
  82. _nursery.cropId = widget.cropId;
  83. }
  84. _validateInputs() async {
  85. if (_formKey.currentState.validate()) {
  86. _formKey.currentState.save();
  87. LoadingDialog.showLoadingDialog(context);
  88. _nursery.nurseryDetail = currentNurseryDetail;
  89. filePaths = Get.find<ChangeFileController>().files;
  90. var activityNursery = jsonEncode(_nursery.toJson()).toString();
  91. //ADD NEW
  92. if (_nursery.activityId == null) {
  93. _repository.createAction((value) {
  94. LoadingDialog.hideLoadingDialog(context);
  95. Get.back(result: value);
  96. Get.snackbar(label_add_success, "Hoạt động ươm",
  97. snackPosition: SnackPosition.BOTTOM);
  98. }, (error) {
  99. LoadingDialog.hideLoadingDialog(context);
  100. _scaffoldKey.currentState.showSnackBar(SnackBar(
  101. content: Row(
  102. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  103. children: <Widget>[
  104. Flexible(child: Text(AppException.handleError(error))),
  105. Icon(Icons.error),
  106. ],
  107. ),
  108. backgroundColor: Colors.red,
  109. duration: Duration(seconds: 3),
  110. ));
  111. },
  112. apiAddAction: ConstCommon.apiAddNursery,
  113. paramActivity: ConstCommon.paramsActionNursery,
  114. activityAction: activityNursery,
  115. filePaths: filePaths);
  116. } else {
  117. //UPDATE
  118. _repository.updateAction((value) {
  119. LoadingDialog.hideLoadingDialog(context);
  120. Get.back(result: value);
  121. Get.snackbar(label_update_success, "Hoạt động ươm",
  122. snackPosition: SnackPosition.BOTTOM);
  123. }, (error) {
  124. LoadingDialog.hideLoadingDialog(context);
  125. _scaffoldKey.currentState.showSnackBar(SnackBar(
  126. content: Row(
  127. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  128. children: <Widget>[
  129. Flexible(child: Text(AppException.handleError(error))),
  130. Icon(Icons.error),
  131. ],
  132. ),
  133. backgroundColor: Colors.red,
  134. duration: Duration(seconds: 3),
  135. ));
  136. },
  137. apiUpdateAction: ConstCommon.apiUpdateNursery,
  138. paramActivity: ConstCommon.paramsActionNursery,
  139. activityAction: activityNursery,
  140. filePaths: filePaths);
  141. }
  142. } else {
  143. _autoValidate = true;
  144. }
  145. }
  146. Widget _btnExecuteTimePicker() {
  147. return FlatButton(
  148. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  149. onPressed: () {
  150. DatePicker.showDateTimePicker(context,
  151. showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
  152. setState(() {
  153. var parsedDate =
  154. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(date);
  155. _nursery.executeDate = "$parsedDate";
  156. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(date);
  157. });
  158. }, currentTime: executeTime, locale: LocaleType.vi);
  159. },
  160. child: Container(
  161. padding:
  162. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  163. decoration: BoxDecoration(
  164. border: kBorderTextField,
  165. ),
  166. child: Row(
  167. children: [
  168. Expanded(
  169. child: Text(
  170. //TODO: check condition
  171. executeTimeView == null ? "$executeTime" : executeTimeView,
  172. style: TextStyle(fontSize: 14.0, color: Colors.black87),
  173. )),
  174. Icon(
  175. Icons.date_range,
  176. color: Colors.blue,
  177. ),
  178. ],
  179. )));
  180. }
  181. Widget _btnSelectSubstrates() {
  182. return FlatButton(
  183. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  184. onPressed: () {
  185. Navigator.of(context)
  186. .push(MaterialPageRoute(
  187. builder: (_) => ResourceHelperScreen(
  188. titleName: "Giá thể",
  189. type: ConstCommon.supplyTypeSubStrate,
  190. selectedId: Get.find<ChangeSupply>().selectedSupplyId),
  191. fullscreenDialog: false))
  192. .then((value) {
  193. if (value != null) {
  194. var result = value as Supply;
  195. _nursery.substratesId = result.id;
  196. changeSupply.change(result);
  197. print("Home: $value");
  198. }
  199. });
  200. },
  201. child: Container(
  202. padding:
  203. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  204. decoration: BoxDecoration(
  205. border: kBorderTextField,
  206. ),
  207. child: Row(
  208. children: [
  209. GetBuilder<ChangeSupply>(
  210. builder: (_) => Expanded(
  211. child: Text(
  212. changeSupply.selectedSupplyName == null
  213. ? "Loại giá thể"
  214. : changeSupply.selectedSupplyName.toString(),
  215. style: TextStyle(
  216. fontSize: 14.0, color: Colors.black87)))),
  217. Icon(
  218. Icons.arrow_drop_down,
  219. color: Colors.grey,
  220. ),
  221. ],
  222. )));
  223. }
  224. Widget _seedLengthField() {
  225. return TextFormField(
  226. keyboardType: TextInputType.numberWithOptions(decimal: true),
  227. inputFormatters: [
  228. ThousandsFormatter(
  229. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  230. ],
  231. decoration: InputDecoration(labelText: "Chiều dài mần"),
  232. controller: _seedLengthController,
  233. validator: (String value) {
  234. return Validators.validNumber(value, "Chiều dài mần");
  235. },
  236. onSaved: (newValue) {
  237. _nursery.seedLength = newValue.parseDoubleThousand();
  238. },
  239. );
  240. }
  241. Widget _quantityField() {
  242. return TextFormField(
  243. keyboardType: TextInputType.numberWithOptions(decimal: true),
  244. inputFormatters: [
  245. ThousandsFormatter(
  246. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  247. ],
  248. decoration: InputDecoration(labelText: "Số lượng hạt gieo"),
  249. controller: _quantityController,
  250. validator: (String value) {
  251. return Validators.validNumber(value, "Số lượng hạt gieo");
  252. },
  253. onSaved: (newValue) {
  254. _nursery.quantity = newValue.parseDoubleThousand();
  255. },
  256. );
  257. }
  258. Widget _seedIncubationTimeField() {
  259. return TextFormField(
  260. keyboardType: TextInputType.numberWithOptions(decimal: true),
  261. inputFormatters: [
  262. ThousandsFormatter(
  263. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  264. ],
  265. decoration: InputDecoration(labelText: "Thời gian ngâm hạt"),
  266. controller: _seedIncubationTimeController,
  267. validator: (String value) {
  268. return Validators.validNumber(value, "Thời gian ngâm hạt");
  269. },
  270. onSaved: (newValue) {
  271. _nursery.seedIncubationTime = newValue.parseDoubleThousand();
  272. },
  273. );
  274. }
  275. Widget _desciptionField() {
  276. return TextFormField(
  277. keyboardType: TextInputType.text,
  278. decoration: InputDecoration(labelText: "Ghi chú"),
  279. controller: _descriptionController,
  280. onSaved: (newValue) {
  281. _nursery.description = newValue;
  282. },
  283. );
  284. }
  285. Widget _executeByField() {
  286. return TextFormField(
  287. keyboardType: TextInputType.text,
  288. decoration: InputDecoration(labelText: "Người thực hiện"),
  289. enabled: false,
  290. controller: _executeByController,
  291. onSaved: (newValue) {},
  292. );
  293. }
  294. Widget _btnAddWorker() {
  295. //TODO :check flow error sua item -> xoa list -> bam nut them
  296. return Builder(builder: (context) {
  297. return BlocConsumer<StatusAddFormBloc, StatusAddFormState>(
  298. listener: (context, state) {
  299. if (state is Edit) {
  300. isResetForm = false;
  301. _workerNameController.text = state.nurseryDetail.workerName;
  302. _trayNumberController.text = state.nurseryDetail.trayNumber;
  303. } else if (state is Delete) {
  304. if (currentIndexUpdate == state.index) {
  305. isResetForm = true;
  306. _workerNameController.text = "";
  307. _trayNumberController.text = "";
  308. }
  309. } else {
  310. isResetForm = true;
  311. _workerNameController.text = "";
  312. _trayNumberController.text = "";
  313. }
  314. }, builder: (context, state) {
  315. return Container(
  316. padding: EdgeInsets.all(8.0),
  317. decoration: BoxDecoration(
  318. shape: BoxShape.rectangle,
  319. borderRadius: BorderRadius.circular(10),
  320. color: Colors.white,
  321. border: Border.all(color: COLOR_CONST.DEFAULT)),
  322. child: Column(
  323. children: [
  324. TextFormField(
  325. keyboardType: TextInputType.text,
  326. controller: _workerNameController,
  327. decoration: InputDecoration(labelText: "Tên công nhân"),
  328. onSaved: (newValue) {},
  329. ),
  330. TextFormField(
  331. keyboardType: TextInputType.text,
  332. controller: _trayNumberController,
  333. decoration: InputDecoration(labelText: "Ươm khây số"),
  334. onSaved: (newValue) {},
  335. ),
  336. Align(
  337. alignment: Alignment.centerRight,
  338. child: Row(
  339. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  340. children: [
  341. isResetForm
  342. ? Container()
  343. : OutlineButton(
  344. shape: RoundedRectangleBorder(
  345. borderRadius: new BorderRadius.circular(8.0)),
  346. child: Text("Huỷ"),
  347. onPressed: () {
  348. context.bloc<StatusAddFormBloc>().add(Reset());
  349. }),
  350. FlatButton(
  351. color: COLOR_CONST.DEFAULT,
  352. shape: RoundedRectangleBorder(
  353. borderRadius: new BorderRadius.circular(8.0)),
  354. onPressed: () {
  355. if (_workerNameController.text.isEmpty ||
  356. _trayNumberController.text.isEmpty) {
  357. return;
  358. }
  359. NurseryDetail _nurseryDetail = NurseryDetail()
  360. ..workerName = _workerNameController.text
  361. ..trayNumber = _trayNumberController.text;
  362. if (state is Edit) {
  363. context.bloc<ExpansionListBloc>().add(Update(
  364. index: state.index,
  365. item: _nurseryDetail,
  366. items: state.items));
  367. } else {
  368. currentNurseryDetail.insert(0, _nurseryDetail);
  369. BlocProvider.of<ExpansionListBloc>(context)
  370. .add(AddNew(items: currentNurseryDetail));
  371. }
  372. context.bloc<StatusAddFormBloc>().add(Reset());
  373. },
  374. child: Text(
  375. (state is Edit) ? "Sửa" : "Thêm",
  376. style: TextStyle(color: Colors.white),
  377. ))
  378. ],
  379. ),
  380. ),
  381. ],
  382. ));
  383. });
  384. });
  385. }
  386. Widget _buildListAddWorker() {
  387. return Builder(builder: (context) {
  388. return BlocBuilder<ExpansionListBloc, ExpansionListState>(
  389. builder: (context, state) {
  390. if (state is ExpansionListSuccess) {
  391. currentNurseryDetail = state.items;
  392. if (currentNurseryDetail.isEmpty) {
  393. return Container();
  394. }
  395. return Container(
  396. height: 70,
  397. child: ListView.builder(
  398. physics: ClampingScrollPhysics(),
  399. scrollDirection: Axis.horizontal,
  400. shrinkWrap: true,
  401. itemCount: currentNurseryDetail.length,
  402. itemBuilder: (context, index) {
  403. return GestureDetector(
  404. onTap: () {
  405. print("edit worker");
  406. currentIndexUpdate = index;
  407. context.bloc<StatusAddFormBloc>().add(Changed(
  408. status: CRUDStatus.edit,
  409. index: index,
  410. nurseryDetail: currentNurseryDetail[index],
  411. items: currentNurseryDetail));
  412. },
  413. child: Card(
  414. child: Stack(
  415. alignment: Alignment.bottomCenter,
  416. overflow: Overflow.visible,
  417. children: <Widget>[
  418. Positioned(
  419. child: ClipRRect(
  420. borderRadius: BorderRadius.circular(8),
  421. child: Container(
  422. padding: EdgeInsets.all(4),
  423. width: 120,
  424. child: Column(
  425. children: [
  426. SizedBox(
  427. height: 12.0,
  428. ),
  429. Flexible(
  430. child: Text(
  431. currentNurseryDetail[index]
  432. .workerName ??
  433. "",
  434. overflow: TextOverflow.ellipsis,
  435. maxLines: 1),
  436. ),
  437. Flexible(
  438. child: Text(currentNurseryDetail[index]
  439. .trayNumber ??
  440. ""))
  441. ],
  442. ),
  443. ),
  444. )),
  445. Positioned(
  446. top: -10,
  447. right: -10,
  448. child: IconButton(
  449. icon: Icon(
  450. Icons.cancel,
  451. color: Colors.redAccent,
  452. ),
  453. onPressed: () {
  454. print("Delete worker");
  455. context.bloc<ExpansionListBloc>().add(
  456. DeleteItem(
  457. index: index,
  458. items: currentNurseryDetail));
  459. context.bloc<StatusAddFormBloc>().add(
  460. Changed(
  461. status: CRUDStatus.delete,
  462. index: index,
  463. nurseryDetail:
  464. currentNurseryDetail[index],
  465. items: currentNurseryDetail));
  466. }),
  467. )
  468. ],
  469. )));
  470. }));
  471. } else if (state is ExpansionListFailure) {
  472. return Container();
  473. } else {
  474. return Container();
  475. }
  476. });
  477. });
  478. }
  479. _actionAppBar() {
  480. IconButton iconButton;
  481. if (1 == 1) {
  482. iconButton = IconButton(
  483. icon: Icon(
  484. Icons.done,
  485. color: Colors.black,
  486. ),
  487. onPressed: () {
  488. FocusScopeNode currentFocus = FocusScope.of(context);
  489. if (!currentFocus.hasPrimaryFocus) {
  490. currentFocus.unfocus();
  491. }
  492. _validateInputs();
  493. },
  494. );
  495. return <Widget>[iconButton];
  496. }
  497. return <Widget>[Container()];
  498. }
  499. @override
  500. Widget build(BuildContext context) => KeyboardDismisser(
  501. gestures: [
  502. GestureType.onTap,
  503. GestureType.onPanUpdateDownDirection,
  504. ],
  505. child: Scaffold(
  506. key: _scaffoldKey,
  507. appBar: AppBar(
  508. centerTitle: true,
  509. title: Text(plot_action_nursery),
  510. actions: _actionAppBar()),
  511. body: KeyboardDismisser(
  512. child: MultiBlocProvider(
  513. providers: [
  514. BlocProvider<ExpansionListBloc>(
  515. create: (context) => ExpansionListBloc(),
  516. ),
  517. BlocProvider<StatusAddFormBloc>(
  518. create: (context) => StatusAddFormBloc(),
  519. ),
  520. BlocProvider<ActionDetailBloc>(
  521. create: (context) =>
  522. ActionDetailBloc(repository: Repository())
  523. ..add(FetchData(
  524. isNeedFetchData: widget.isEdit,
  525. apiActivity: ConstCommon.apiDetailNursery,
  526. activityId: widget.activityId))),
  527. BlocProvider<MediaHelperBloc>(
  528. create: (context) =>
  529. MediaHelperBloc()..add(ChangeListMedia(items: [])),
  530. )
  531. ],
  532. child: Form(
  533. key: _formKey,
  534. autovalidate: _autoValidate,
  535. child: SingleChildScrollView(
  536. padding: EdgeInsets.all(8.0),
  537. child: BlocConsumer<ActionDetailBloc,
  538. ActionDetailState>(
  539. listener: (context, state) async {
  540. if (state is ActionDetailFailure) {
  541. print("fail");
  542. LoadingDialog.hideLoadingDialog(context);
  543. } else if (state is ActionDetailSuccess) {
  544. LoadingDialog.hideLoadingDialog(context);
  545. print("success");
  546. print(state.item);
  547. _nursery = Nursery.fromJson(state.item);
  548. _seedLengthController.text = _nursery
  549. .seedLength
  550. .formatNumtoStringDecimal();
  551. _quantityController.text = _nursery.quantity
  552. .formatNumtoStringDecimal();
  553. _seedIncubationTimeController.text = _nursery
  554. .seedIncubationTime
  555. .formatNumtoStringDecimal();
  556. _descriptionController.text =
  557. _nursery.description;
  558. _executeByController.text =
  559. _nursery.executeBy;
  560. try {
  561. executeTime =
  562. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
  563. .parse(_nursery.executeDate);
  564. } catch (_) {}
  565. executeTimeView =
  566. DateFormat("dd/MM/yyyy HH:mm")
  567. .format(executeTime);
  568. //Show media
  569. if (_nursery.media != null) {
  570. await cacheFiles(_nursery.media)
  571. .then((value) {
  572. print("then: ${value.length}");
  573. BlocProvider.of<MediaHelperBloc>(context)
  574. .add(ChangeListMedia(items: value));
  575. }).whenComplete(() {
  576. print("completed");
  577. });
  578. }
  579. //Show worker
  580. if (_nursery.nurseryDetail.length > 0) {
  581. BlocProvider.of<ExpansionListBloc>(context)
  582. .add(AddNew(
  583. items: _nursery.nurseryDetail));
  584. }
  585. //change subStrates
  586. if (_nursery.substratesId != null) {
  587. Get.find<ChangeSupply>().changeByIdAndName(
  588. _nursery.substratesId,
  589. _nursery.substrateName);
  590. }
  591. } else if (state is ActionDetailInitial) {
  592. print("init");
  593. } else if (state is ActionDetailLoading) {
  594. print("loading");
  595. LoadingDialog.showLoadingDialog(context);
  596. }
  597. },
  598. builder: (context, state) {
  599. return Column(
  600. children: <Widget>[
  601. Container(
  602. width: double.infinity,
  603. child: Text(
  604. "Ngày thực hiện",
  605. style: TextStyle(
  606. color: Colors.black54,
  607. fontSize: 13.0),
  608. ),
  609. ),
  610. _btnExecuteTimePicker(),
  611. SizedBox(
  612. height: 8.0,
  613. ),
  614. Container(
  615. width: double.infinity,
  616. child: Text(
  617. "Loại giá thể",
  618. style: TextStyle(
  619. color: Colors.black54,
  620. fontSize: 13.0),
  621. ),
  622. ),
  623. _btnSelectSubstrates(),
  624. SizedBox(
  625. height: 8.0,
  626. ),
  627. _seedLengthField(),
  628. SizedBox(
  629. height: 8.0,
  630. ),
  631. _quantityField(),
  632. SizedBox(
  633. height: 8.0,
  634. ),
  635. _seedIncubationTimeField(),
  636. SizedBox(
  637. height: 8.0,
  638. ),
  639. _desciptionField(),
  640. SizedBox(
  641. height: 8.0,
  642. ),
  643. _executeByField(),
  644. SizedBox(
  645. height: 8.0,
  646. ),
  647. _btnAddWorker(),
  648. SizedBox(
  649. height: 8.0,
  650. ),
  651. _buildListAddWorker(),
  652. SizedBox(
  653. height: 8.0,
  654. ),
  655. BlocBuilder<MediaHelperBloc,
  656. MediaHelperState>(
  657. builder: (context, state) {
  658. if (state is MediaHelperSuccess) {
  659. print("length: " +
  660. state.items.length.toString());
  661. return WidgetMediaPicker(
  662. currentItems: state.items,
  663. onChangeFiles: (filePaths) async {
  664. Get.find<ChangeFileController>()
  665. .addAllFile(filePaths);
  666. });
  667. } else {
  668. return Center(
  669. child: CircularProgressIndicator());
  670. }
  671. }),
  672. ],
  673. );
  674. },
  675. ),
  676. ))))));
  677. @override
  678. void dispose() {
  679. _substrateController.dispose();
  680. _seedLengthController.dispose();
  681. _quantityController.dispose();
  682. _seedIncubationTimeController.dispose();
  683. _descriptionController.dispose();
  684. _executeByController.dispose();
  685. super.dispose();
  686. }
  687. Future<List<Media>> cacheFiles(String existedMedias) async {
  688. var medias = List<Media>();
  689. var mediaPathsLocal = List<String>();
  690. var mediaPaths = existedMedias.split(";");
  691. for (int i = 0; i < mediaPaths.length; i++) {
  692. var tempFile = await DefaultCacheManager()
  693. .getSingleFile(ConstCommon.baseImageUrl + mediaPaths[i]);
  694. print(tempFile.path);
  695. var isVideo = lookupMimeType(tempFile.path) == "video/mp4";
  696. print("file type: " + lookupMimeType(tempFile.path));
  697. Media media = Media()
  698. ..pathFile = tempFile.path
  699. ..isVideo = isVideo;
  700. medias.add(media);
  701. mediaPathsLocal.add(tempFile.path);
  702. }
  703. Get.find<ChangeFileController>().addAllFile(filePaths);
  704. return medias;
  705. }
  706. }