import 'package:fcs/constants.dart'; import 'package:fcs/domain/entities/faq.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/faq/model/faq_model.dart'; import 'package:fcs/pages/faq/widgets.dart'; import 'package:fcs/pages/main/util.dart'; import 'package:fcs/pages/widgets/input_text.dart'; import 'package:fcs/pages/widgets/local_app_bar.dart'; import 'package:fcs/pages/widgets/progress.dart'; import 'package:flutter/material.dart'; import 'package:flutter_vector_icons/flutter_vector_icons.dart'; import 'package:provider/provider.dart'; const info = "Select additional page"; class FAQEditor extends StatefulWidget { final FAQ? faq; const FAQEditor({this.faq}); @override _FAQEditorState createState() => _FAQEditorState(); } class _FAQEditorState extends State { TextEditingController _sn = new TextEditingController(); TextEditingController _engQ = new TextEditingController(); TextEditingController _mmQ = new TextEditingController(); TextEditingController _engA = new TextEditingController(); TextEditingController _mmA = new TextEditingController(); TextEditingController _pageLabelEng = new TextEditingController(); TextEditingController _pageLabelMm = new TextEditingController(); final _faqFormKey = GlobalKey(); bool _isLoading = false; bool _isNew = false; String _pageLink = info; FAQ _faq = new FAQ(); @override void initState() { super.initState(); _isNew = widget.faq == null; if (widget.faq != null) { _faq = widget.faq!; _sn.text = _faq.sn.toString(); _engQ.text = _faq.questionEng ?? ""; _mmQ.text = _faq.questionMm ?? ''; _engA.text = _faq.answerEng ?? ''; _mmA.text = _faq.answerMm ?? ''; _pageLabelEng.text = _faq.pageLinkLabelEng ?? ""; _pageLabelMm.text = _faq.pageLinkLabelMm ?? ""; _pageLink = _faq.pageLink ?? ''; } } @override Widget build(BuildContext context) { final snBox = InputText( controller: _sn, labelTextKey: "faq.edit.sn", withBorder: false, textInputType: TextInputType.number, autovalidateMode: AutovalidateMode.onUserInteraction, validator: (value) { if (value == null || value.isEmpty || value.length == 0) { return "Please insert S/N"; } return null; }, ); final questionEngBox = InputText( controller: _engQ, maxLines: 2, withBorder: true, ); final answerEngBox = InputText( controller: _engA, maxLines: 5, withBorder: true, ); final questionMmBox = InputText( controller: _mmQ, maxLines: 2, withBorder: true, ); final answerMmBox = InputText( controller: _mmA, maxLines: 5, withBorder: true, ); final pageLinkBox = DropdownButton( isExpanded: true, value: _pageLink, style: TextStyle(color: Colors.deepPurple), underline: Container( height: 2, color: primaryColor, ), onChanged: (String? newValue) { if (newValue != null) setState(() { _pageLink = newValue; }); }, items: [ info, page_buying_instructions, page_payment_methods, page_rates ].map>((String value) { return DropdownMenuItem( value: value, child: Text( value, style: TextStyle(color: value == info ? Colors.black : primaryColor), ), ); }).toList(), ); final pageLabelEngBox = InputText( controller: _pageLabelEng, labelTextKey: "faq.edit.page.label.eng", ); final pageLabelMmBox = InputText( controller: _pageLabelMm, labelTextKey: "faq.edit.page.label.mm", ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: LocalAppBar( labelKey: _isNew ? 'faq.add.title' : 'faq.edit.title', backgroundColor: Colors.white, arrowColor: primaryColor, labelColor: primaryColor, onBack: () { if (isDataChanged()) { showConfirmDialog(context, "back.button_confirm", () { Navigator.of(context).pop(); }); } else { Navigator.of(context).pop(); } }, actions: [ _isNew ? const SizedBox() : IconButton( icon: Icon( Icons.delete, color: primaryColor, ), onPressed: _delete, ) ], ), body: Form( key: _faqFormKey, child: Padding( padding: EdgeInsets.only(left: 24.0, right: 24.0), child: ListView( children: [ snBox, Center(child: itemTitle(context, "faq.edit.eng")), subItemTitle(context, "faq.edit.question", iconData: SimpleLineIcons.question), questionEngBox, subItemTitle(context, "faq.edit.answer", iconData: MaterialCommunityIcons.message_reply_text), answerEngBox, Divider(), Center(child: itemTitle(context, "faq.edit.mm")), subItemTitle(context, "faq.edit.question", iconData: SimpleLineIcons.question), questionMmBox, subItemTitle(context, "faq.edit.answer", iconData: MaterialCommunityIcons.message_reply_text), answerMmBox, Divider(), Center(child: itemTitle(context, "faq.edit.page")), pageLinkBox, pageLabelEngBox, pageLabelMmBox, fcsButton(context, getLocalString(context, "btn.save"), callack: _save), SizedBox( height: 20, ) ], ), ), ))); } _save() async { if (!_faqFormKey.currentState!.validate()) { return null; } setState(() { _isLoading = true; }); try { int sn = int.parse(_sn.text); FAQModel faqModel = Provider.of(context, listen: false); FAQ _faq = FAQ( sn: sn, questionEng: _engQ.text, answerEng: _engA.text, questionMm: _mmQ.text, answerMm: _mmA.text, pageLinkLabelEng: _pageLabelEng.text, pageLinkLabelMm: _pageLabelMm.text, pageLink: _pageLink); if (_isNew) { await faqModel.addFAQ(_faq); } else { _faq.id = this._faq.id; await faqModel.updateFAQ(_faq); } Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", "Invalid S/N"); } finally { setState(() { _isLoading = false; }); } } _delete() { showConfirmDialog(context, "faq.edit.delete.confirm", _deleteFAQ); } _deleteFAQ() async { setState(() { _isLoading = true; }); try { FAQModel faqModel = Provider.of(context, listen: false); await faqModel.deleteFAQ(this._faq); Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); } finally { setState(() { _isLoading = false; }); } } isDataChanged() { return _sn.text != "" || _engQ.text != "" || _engA.text != "" || _mmQ.text != "" || _mmA.text != "" || _pageLabelEng.text != "" || _pageLabelMm.text != "" || _pageLink != info; } }