Files
fcs/lib/pages/faq/faq_edit_page.dart

259 lines
7.8 KiB
Dart
Raw Normal View History

2020-10-07 02:33:06 +06:30
import 'package:fcs/domain/constants.dart';
import 'package:fcs/domain/entities/faq.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.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_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
2020-09-07 16:05:28 +06:30
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2021-09-10 14:29:55 +06:30
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
2020-09-11 16:14:36 +06:30
import 'package:provider/provider.dart';
2020-09-07 16:05:28 +06:30
2020-09-18 21:33:41 +06:30
const info = "Select additional page";
2020-09-07 16:05:28 +06:30
class FAQEditor extends StatefulWidget {
2021-09-10 12:02:08 +06:30
final FAQ? faq;
2020-09-07 16:05:28 +06:30
const FAQEditor({this.faq});
@override
_FAQEditorState createState() => _FAQEditorState();
}
class _FAQEditorState extends State<FAQEditor> {
2020-09-11 16:14:36 +06:30
TextEditingController _sn = new TextEditingController();
2020-09-07 16:05:28 +06:30
TextEditingController _engQ = new TextEditingController();
TextEditingController _mmQ = new TextEditingController();
TextEditingController _engA = new TextEditingController();
TextEditingController _mmA = new TextEditingController();
2020-09-18 21:33:41 +06:30
TextEditingController _pageLabelEng = new TextEditingController();
TextEditingController _pageLabelMm = new TextEditingController();
2020-09-07 16:05:28 +06:30
final _formKey = GlobalKey<FormState>();
bool _isLoading = false;
bool _isNew = false;
2020-09-18 21:33:41 +06:30
String _pageLink = info;
2021-09-10 12:02:08 +06:30
FAQ _faq = new FAQ();
2020-09-18 21:33:41 +06:30
2020-09-07 16:05:28 +06:30
@override
void initState() {
super.initState();
_isNew = widget.faq == null;
if (widget.faq != null) {
2021-09-10 12:02:08 +06:30
_faq = widget.faq!;
_sn.text = _faq.sn.toString();
2021-09-10 15:23:13 +06:30
_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 ?? '';
2020-09-07 16:05:28 +06:30
}
}
@override
Widget build(BuildContext context) {
2020-09-11 16:14:36 +06:30
final snBox = InputText(
controller: _sn,
2020-09-15 07:13:41 +06:30
labelTextKey: "faq.edit.sn",
2020-09-11 16:14:36 +06:30
maxLines: 1,
2020-09-15 07:13:41 +06:30
withBorder: false,
2020-09-11 16:14:36 +06:30
textInputType: TextInputType.number,
);
final questionEngBox = InputText(
2020-09-07 16:05:28 +06:30
controller: _engQ,
maxLines: 2,
withBorder: true,
);
2020-09-11 16:14:36 +06:30
final answerEngBox = InputText(
controller: _engA,
2020-09-07 16:05:28 +06:30
maxLines: 5,
withBorder: true,
);
2020-09-11 16:14:36 +06:30
final questionMmBox = InputText(
controller: _mmQ,
2020-09-07 16:05:28 +06:30
maxLines: 2,
withBorder: true,
);
2020-09-11 16:14:36 +06:30
final answerMmBox = InputText(
2020-09-07 16:05:28 +06:30
controller: _mmA,
maxLines: 5,
withBorder: true,
);
2020-09-18 21:33:41 +06:30
final pageLinkBox = DropdownButton<String>(
value: _pageLink,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: primaryColor,
),
2021-09-10 12:02:08 +06:30
onChanged: (String? newValue) {
2024-01-09 13:11:22 +06:30
if (newValue != null)
setState(() {
_pageLink = newValue;
});
2020-09-18 21:33:41 +06:30
},
2020-10-15 03:06:13 +06:30
items: <String>[
info,
page_buying_instructions,
page_payment_methods,
page_rates
].map<DropdownMenuItem<String>>((String value) {
2020-09-18 21:33:41 +06:30
return DropdownMenuItem<String>(
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",
);
2020-09-07 16:05:28 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
2020-10-15 03:06:13 +06:30
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: new IconButton(
icon: new Icon(CupertinoIcons.back, color: primaryColor),
onPressed: () {
if (isDataChanged()) {
showConfirmDialog(context, "back.button_confirm", () {
Navigator.of(context).pop();
});
} else {
Navigator.of(context).pop();
}
},
2020-10-15 03:06:13 +06:30
),
backgroundColor: Colors.white,
shadowColor: Colors.transparent,
title: LocalText(
context, _isNew ? 'faq.add.title' : 'faq.edit.title',
color: primaryColor, fontSize: 20),
actions: [
IconButton(
2020-10-17 01:40:24 +06:30
icon: Icon(
Icons.delete,
color: primaryColor,
),
2020-10-15 03:06:13 +06:30
onPressed: _delete,
)
],
),
body: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.only(left: 24.0, right: 24.0),
child: ListView(
children: <Widget>[
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,
)
],
),
),
)));
2020-09-07 16:05:28 +06:30
}
2020-09-11 16:14:36 +06:30
_save() async {
2020-09-07 16:05:28 +06:30
setState(() {
_isLoading = true;
});
try {
2024-01-09 13:11:22 +06:30
int sn = int.parse(_sn.text);
2020-09-11 16:14:36 +06:30
FAQModel faqModel = Provider.of<FAQModel>(context, listen: false);
FAQ _faq = FAQ(
sn: sn,
questionEng: _engQ.text,
answerEng: _engA.text,
questionMm: _mmQ.text,
2020-09-18 21:33:41 +06:30
answerMm: _mmA.text,
pageLinkLabelEng: _pageLabelEng.text,
pageLinkLabelMm: _pageLabelMm.text,
pageLink: _pageLink);
2020-09-11 16:14:36 +06:30
if (_isNew) {
await faqModel.addFAQ(_faq);
} else {
2021-09-10 12:02:08 +06:30
_faq.id = this._faq.id;
2020-09-11 16:14:36 +06:30
await faqModel.updateFAQ(_faq);
}
2020-09-07 16:05:28 +06:30
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
2020-09-18 21:33:41 +06:30
_delete() {
showConfirmDialog(context, "faq.edit.delete.confirm", _deleteFAQ);
}
_deleteFAQ() async {
setState(() {
_isLoading = true;
});
try {
FAQModel faqModel = Provider.of<FAQModel>(context, listen: false);
2021-09-10 12:02:08 +06:30
await faqModel.deleteFAQ(this._faq);
2020-09-18 21:33:41 +06:30
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;
}
2020-09-07 16:05:28 +06:30
}