2020-09-04 15:30:10 +06:30
|
|
|
import 'dart:convert';
|
|
|
|
|
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/domain/vo/term.dart';
|
|
|
|
|
import 'package:fcs/helpers/theme.dart';
|
|
|
|
|
import 'package:fcs/pages/term/model/term_model.dart';
|
|
|
|
|
import 'package:fcs/pages/main/util.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';
|
2020-09-04 15:30:10 +06:30
|
|
|
import 'package:flutter/material.dart';
|
2021-09-11 08:27:27 +06:30
|
|
|
import 'package:flutter_icons_null_safety/flutter_icons_null_safety.dart';
|
2020-09-04 15:30:10 +06:30
|
|
|
import 'package:provider/provider.dart';
|
2021-09-13 17:36:52 +06:30
|
|
|
import 'package:zefyrka/zefyrka.dart';
|
2021-09-11 08:27:27 +06:30
|
|
|
// import 'package:zefyr/zefyr.dart';
|
2020-09-04 15:30:10 +06:30
|
|
|
|
|
|
|
|
typedef void ProfileCallback();
|
|
|
|
|
|
|
|
|
|
class TermEdit extends StatefulWidget {
|
2021-09-13 17:36:52 +06:30
|
|
|
final Term? term;
|
2021-09-11 08:27:27 +06:30
|
|
|
TermEdit({required this.term});
|
2020-09-04 15:30:10 +06:30
|
|
|
@override
|
|
|
|
|
_TermEditState createState() => _TermEditState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _TermEditState extends State<TermEdit> {
|
|
|
|
|
/// Allows to control the editor and the document.
|
2021-09-13 17:36:52 +06:30
|
|
|
late ZefyrController _controllerEng;
|
|
|
|
|
late ZefyrController _controllerMm;
|
2020-09-04 15:30:10 +06:30
|
|
|
|
|
|
|
|
/// Zefyr editor like any other input field requires a focus node.
|
2021-09-13 17:36:52 +06:30
|
|
|
late FocusNode _focusNodeEng;
|
|
|
|
|
late FocusNode _focusNodeMm;
|
2021-09-11 08:27:27 +06:30
|
|
|
bool _isLoading = false;
|
2020-09-04 15:30:10 +06:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_isLoading = false;
|
|
|
|
|
|
|
|
|
|
// Here we must load the document and pass it to Zefyr controller.
|
2021-09-13 17:36:52 +06:30
|
|
|
_controllerEng = ZefyrController(_loadDocument(widget.term!.termEng ?? ""));
|
|
|
|
|
_controllerMm = ZefyrController(_loadDocument(widget.term!.termMm ?? ""));
|
|
|
|
|
_focusNodeEng = FocusNode();
|
|
|
|
|
_focusNodeMm = FocusNode();
|
2020-09-04 15:30:10 +06:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Loads the document to be edited in Zefyr.
|
2021-09-13 17:36:52 +06:30
|
|
|
NotusDocument _loadDocument(String data) {
|
|
|
|
|
late NotusDocument doc;
|
|
|
|
|
try {
|
|
|
|
|
doc = NotusDocument.fromJson(jsonDecode(data));
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
if (doc == null) {
|
|
|
|
|
doc = NotusDocument();
|
|
|
|
|
}
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
2020-09-04 15:30:10 +06:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2020-09-15 07:13:41 +06:30
|
|
|
final savebtn =
|
|
|
|
|
fcsButton(context, getLocalString(context, "btn.save"), callack: _save);
|
2020-09-18 04:04:21 +06:30
|
|
|
|
|
|
|
|
return DefaultTabController(
|
|
|
|
|
length: 2,
|
|
|
|
|
child: LocalProgress(
|
|
|
|
|
inAsyncCall: _isLoading,
|
|
|
|
|
child: Scaffold(
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
title: LocalLargeTitle(
|
|
|
|
|
context,
|
|
|
|
|
"term",
|
|
|
|
|
color: primaryColor,
|
|
|
|
|
),
|
|
|
|
|
leading: IconButton(
|
|
|
|
|
icon: Icon(
|
|
|
|
|
CupertinoIcons.back,
|
|
|
|
|
color: primaryColor,
|
|
|
|
|
size: 30,
|
|
|
|
|
),
|
2020-12-04 17:28:21 +06:30
|
|
|
onPressed: () {
|
2020-12-08 20:24:15 +06:30
|
|
|
Navigator.of(context).pop();
|
2020-12-04 17:28:21 +06:30
|
|
|
},
|
2020-09-07 16:05:28 +06:30
|
|
|
),
|
2020-09-18 04:04:21 +06:30
|
|
|
shadowColor: Colors.transparent,
|
|
|
|
|
backgroundColor: Colors.white,
|
2020-10-07 02:33:06 +06:30
|
|
|
actions: [
|
|
|
|
|
IconButton(
|
|
|
|
|
onPressed: _unfocus,
|
|
|
|
|
icon: Icon(Icons.check, color: primaryColor))
|
|
|
|
|
],
|
2020-09-18 04:04:21 +06:30
|
|
|
bottom: TabBar(
|
|
|
|
|
onTap: (index) {
|
|
|
|
|
// Tab index when user select it, it start from zero
|
|
|
|
|
},
|
|
|
|
|
tabs: [
|
|
|
|
|
Tab(
|
|
|
|
|
icon: Image.asset(
|
|
|
|
|
'icons/flags/png/us.png',
|
|
|
|
|
package: 'country_icons',
|
|
|
|
|
fit: BoxFit.fitWidth,
|
|
|
|
|
width: 25,
|
|
|
|
|
)),
|
|
|
|
|
Tab(
|
|
|
|
|
icon: Image.asset(
|
|
|
|
|
'icons/flags/png/mm.png',
|
|
|
|
|
package: 'country_icons',
|
|
|
|
|
fit: BoxFit.fitWidth,
|
|
|
|
|
width: 25,
|
|
|
|
|
)),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
body: ListView(
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
height: MediaQuery.of(context).size.height - 200,
|
|
|
|
|
child: TabBarView(
|
|
|
|
|
children: [
|
2021-09-13 17:36:52 +06:30
|
|
|
textEditor(_controllerEng, _focusNodeEng),
|
|
|
|
|
textEditor(_controllerMm, _focusNodeMm),
|
2020-09-18 04:04:21 +06:30
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
savebtn,
|
|
|
|
|
],
|
2020-09-04 15:30:10 +06:30
|
|
|
),
|
|
|
|
|
),
|
2020-09-18 04:04:21 +06:30
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 17:36:52 +06:30
|
|
|
Widget textEditor(ZefyrController controller, FocusNode focusNode) {
|
|
|
|
|
return ListView(
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
height: MediaQuery.of(context).size.height - 200,
|
|
|
|
|
child: ZefyrEditor(
|
|
|
|
|
autofocus: false,
|
|
|
|
|
padding: EdgeInsets.all(16),
|
|
|
|
|
controller: controller,
|
|
|
|
|
focusNode: focusNode,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
SizedBox(
|
|
|
|
|
height: 10,
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-09-04 15:30:10 +06:30
|
|
|
|
2020-10-07 02:33:06 +06:30
|
|
|
_unfocus() {
|
|
|
|
|
FocusScope.of(context).unfocus();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 16:05:28 +06:30
|
|
|
_save() async {
|
2020-09-04 15:30:10 +06:30
|
|
|
setState(() {
|
|
|
|
|
_isLoading = true;
|
|
|
|
|
});
|
|
|
|
|
try {
|
2021-09-13 17:36:52 +06:30
|
|
|
final contentsEng = jsonEncode(_controllerEng.document);
|
|
|
|
|
final contentsMm = jsonEncode(_controllerMm.document);
|
2021-09-11 08:27:27 +06:30
|
|
|
// print('contents => $contentsEng');
|
2021-09-13 17:36:52 +06:30
|
|
|
TermModel termModel = Provider.of<TermModel>(context, listen: false);
|
|
|
|
|
await termModel.saveTerm(Term(termEng: contentsEng, termMm: contentsMm));
|
2020-09-04 15:30:10 +06:30
|
|
|
} catch (e) {
|
|
|
|
|
showMsgDialog(context, "Error", e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
_isLoading = false;
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|