2020-05-29 07:45:27 +06:30
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
import 'package:fcs/model/announcement_model.dart';
|
|
|
|
|
import 'package:fcs/model/language_model.dart';
|
2020-09-04 15:30:10 +06:30
|
|
|
import 'package:fcs/fcs/common/pages/util.dart';
|
|
|
|
|
import 'package:fcs/fcs/common/helpers/theme.dart';
|
2020-05-29 07:45:27 +06:30
|
|
|
import 'package:fcs/vo/announcement.dart';
|
|
|
|
|
import 'package:fcs/widget/local_text.dart';
|
|
|
|
|
import 'package:fcs/widget/localization/app_translations.dart';
|
|
|
|
|
import 'package:fcs/widget/progress.dart';
|
|
|
|
|
import 'package:zefyr/zefyr.dart';
|
|
|
|
|
|
|
|
|
|
class AnnouncementEditor extends StatefulWidget {
|
|
|
|
|
final Announcement announcement;
|
|
|
|
|
|
|
|
|
|
const AnnouncementEditor({Key key, this.announcement}) : super(key: key);
|
|
|
|
|
@override
|
|
|
|
|
_AnnouncementEditorState createState() => _AnnouncementEditorState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _AnnouncementEditorState extends State<AnnouncementEditor> {
|
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
|
static final _scafoldKey = new GlobalKey<ScaffoldState>();
|
|
|
|
|
TextEditingController nameController = new TextEditingController();
|
|
|
|
|
bool _isLoading = false;
|
|
|
|
|
FocusNode _focusNode;
|
|
|
|
|
ZefyrController _textController;
|
|
|
|
|
Announcement _announcement = new Announcement();
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
if (widget.announcement != null) {
|
|
|
|
|
_announcement = widget.announcement;
|
|
|
|
|
nameController.text = _announcement.name;
|
|
|
|
|
NotusDocument doc =
|
|
|
|
|
NotusDocument.fromJson(jsonDecode(widget.announcement.text));
|
|
|
|
|
_textController = ZefyrController(doc);
|
|
|
|
|
_focusNode = FocusNode();
|
|
|
|
|
} else {
|
|
|
|
|
_textController = ZefyrController(NotusDocument());
|
|
|
|
|
_focusNode = FocusNode();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
var languageModel = Provider.of<LanguageModel>(context);
|
|
|
|
|
|
|
|
|
|
final nameBox = Container(
|
|
|
|
|
padding: EdgeInsets.only(top: 10),
|
|
|
|
|
child: TextFormField(
|
|
|
|
|
controller: nameController,
|
|
|
|
|
autofocus: false,
|
|
|
|
|
decoration: new InputDecoration(
|
|
|
|
|
labelText: AppTranslations.of(context).text("announcement.name"),
|
|
|
|
|
labelStyle: languageModel.isEng ? labelStyle : labelStyleMM,
|
|
|
|
|
enabledBorder: UnderlineInputBorder(
|
|
|
|
|
borderSide: BorderSide(color: primaryColor, width: 1.0)),
|
|
|
|
|
focusedBorder: UnderlineInputBorder(
|
|
|
|
|
borderSide: BorderSide(color: primaryColor, width: 1.0)),
|
|
|
|
|
),
|
|
|
|
|
validator: (value) {
|
|
|
|
|
if (value.isEmpty) {
|
|
|
|
|
return AppTranslations.of(context).text("announcement.name_empty");
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final textEditor = ZefyrField(
|
|
|
|
|
height: 200.0,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
labelText: AppTranslations.of(context).text('announcement.desc'),
|
|
|
|
|
labelStyle: languageModel.isEng ? labelStyle : labelStyleMM,
|
|
|
|
|
),
|
|
|
|
|
controller: _textController,
|
|
|
|
|
focusNode: _focusNode,
|
|
|
|
|
autofocus: false,
|
|
|
|
|
physics: ClampingScrollPhysics(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return LocalProgress(
|
|
|
|
|
inAsyncCall: _isLoading,
|
|
|
|
|
child: Scaffold(
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
backgroundColor: primaryColor,
|
|
|
|
|
title: LocalText(
|
|
|
|
|
context,
|
|
|
|
|
"announcement.form.title",
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
fontSize: 20,
|
|
|
|
|
),
|
|
|
|
|
actions: <Widget>[
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: Icon(Icons.save),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
_save(context);
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
body: ZefyrScaffold(
|
|
|
|
|
key: _scafoldKey,
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: EdgeInsets.only(left: 20.0, right: 20.0),
|
|
|
|
|
child: Form(
|
|
|
|
|
key: _formKey,
|
|
|
|
|
child: ListView(
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
nameBox,
|
|
|
|
|
SizedBox(height: 10),
|
|
|
|
|
textEditor,
|
|
|
|
|
],
|
|
|
|
|
)),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _save(BuildContext context) {
|
|
|
|
|
if (!_formKey.currentState.validate()) return;
|
|
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
_isLoading = true;
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
_announcement.name = nameController.text;
|
|
|
|
|
final contents = jsonEncode(_textController.document);
|
|
|
|
|
_announcement.text = contents;
|
|
|
|
|
|
|
|
|
|
if (widget.announcement != null) {
|
|
|
|
|
Provider.of<AnnouncementModel>(context, listen: false)
|
|
|
|
|
.updateAnnouncement(_announcement);
|
|
|
|
|
} else {
|
|
|
|
|
Provider.of<AnnouncementModel>(context, listen: false)
|
|
|
|
|
.createAnnouncement(_announcement);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
showMsgDialog(context, "Error", e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
Future.delayed(const Duration(milliseconds: 3000), () {
|
|
|
|
|
_isLoading = false;
|
|
|
|
|
Navigator.pop<Announcement>(context, this._announcement);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|