Files
fcs/lib/pages/util.dart

573 lines
19 KiB
Dart
Raw Normal View History

2020-05-31 15:00:11 +06:30
import 'package:fcs/widget/label_widgets.dart';
2020-05-29 07:45:27 +06:30
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:fcs/model/announcement_model.dart';
import 'package:fcs/model/buyer_model.dart';
import 'package:fcs/model/do_model.dart';
import 'package:fcs/model/language_model.dart';
import 'package:fcs/model/notification_model.dart';
import 'package:fcs/model/po_model.dart';
import 'package:fcs/model/user_model.dart';
import 'package:fcs/theme/theme.dart';
import 'package:fcs/vo/buyer.dart';
import 'package:fcs/widget/local_text.dart';
import 'package:fcs/widget/localization/app_translations.dart';
import 'package:fcs/vo/notification.dart' as Noti;
import 'announcement.dart';
import 'buyer_info.dart';
import 'do/do_approve.dart';
import 'log_list.dart';
import 'my_registeration_info.dart';
import 'po/po_submission_form.dart';
import 'products_list.dart';
import 'user_editor.dart';
final log = Logger('Util');
void showMsgDialog(BuildContext context, String title, String msg) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: new Text(title),
content: new Text(msg),
actions: <Widget>[
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
var selectedColor;
void showColorPicker(BuildContext context, Color color, callback(Color color)) {
showDialog(
context: context,
child: AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0))),
title: const Text('Pick product color'),
content: SingleChildScrollView(
child: ColorPicker(
pickerColor: color.value == 0 ? Colors.red : color,
pickerAreaHeightPercent: 0.6,
onColorChanged: (Color value) {
selectedColor = value;
},
),
),
actions: <Widget>[
FlatButton(
child: const Text('Choose'),
onPressed: () {
callback(selectedColor == null ? Colors.red : selectedColor);
Navigator.of(context).pop();
},
),
FlatButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
);
}
Future<void> showConfirmDialog(
BuildContext context, String translationKey, ok(),
{List<String> translationVariables}) async {
await showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Center(
child: LocalText(
context,
translationKey,
translationVariables: translationVariables,
),
),
content: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
FlatButton(
child: Text(
AppTranslations.of(context).text('Cancel'),
style: Provider.of<LanguageModel>(context).isEng
? TextStyle()
: TextStyle(fontFamily: 'MyanmarUnicode'),
),
onPressed: () {
Navigator.of(context).pop();
}),
FlatButton(
color: primaryColor,
child: Text(AppTranslations.of(context).text('Ok'),
style: Provider.of<LanguageModel>(context).isEng
? TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold)
: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontFamily: 'MyanmarUnicode')),
onPressed: () async {
Navigator.of(context).pop();
await ok();
})
],
),
),
);
});
}
void showCommentDialog(BuildContext context, commentCallback(comment)) {
TextEditingController _comment = new TextEditingController();
showDialog(
context: context,
builder: (_) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0))),
content: Container(
width: 300.0,
height: 80.0,
child: Container(
child: TextFormField(
controller: _comment,
autofocus: false,
cursorColor: primaryColor,
maxLines: 3,
style: textStyle,
decoration: new InputDecoration(
labelText: "Comment",
labelStyle: labelStyle,
icon: Icon(
Icons.add_comment,
color: primaryColor,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0)),
),
validator: (value) {
if (value.isEmpty) {
return "Please enter comment";
}
return null;
},
),
),
),
actions: <Widget>[
FlatButton(
child: Text(
"Cancel",
style: labelStyle,
),
onPressed: () {
_comment.clear();
Navigator.of(context).pop();
}),
FlatButton(
color: primaryColor,
child: Text("Submit",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold)),
onPressed: () {
commentCallback(_comment.text);
Navigator.of(context).pop();
})
],
);
});
}
Widget getStatus(String status) {
2020-05-29 16:14:17 +06:30
return status == "Delivered"
2020-06-01 14:24:45 +06:30
? Text(status,
style: TextStyle(
color: primaryColor, fontSize: 18, fontWeight: FontWeight.bold))
2020-05-29 07:45:27 +06:30
: status == "rejected"
? Chip(
backgroundColor: Colors.red,
avatar: Icon(
Icons.remove,
color: Colors.white,
size: 14,
),
label: Text(
status,
style: TextStyle(color: Colors.white, fontSize: 12),
))
2020-05-29 16:14:17 +06:30
: status == "In progress"
2020-06-01 14:24:45 +06:30
? Text(
status,
style: TextStyle(color: Colors.white, fontSize: 12),
)
2020-05-31 15:00:11 +06:30
: status == "Pickuped"
2020-05-29 15:54:26 +06:30
? Text(
status,
2020-05-31 15:00:11 +06:30
style: TextStyle(
color: primaryColor,
fontSize: 18,
fontWeight: FontWeight.bold),
2020-05-29 15:54:26 +06:30
)
2020-05-31 15:00:11 +06:30
: status == "Pending" || status == "Rescheduled"
? Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.schedule),
),
Text(
status,
style: TextStyle(
color: primaryColor,
fontSize: 18,
fontWeight: FontWeight.bold),
)
],
2020-05-29 15:54:26 +06:30
)
2020-05-31 15:00:11 +06:30
: status == "Assigned"
? Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.check),
),
Text(
status,
style: TextStyle(
color: primaryColor,
fontSize: 18,
fontWeight: FontWeight.bold),
)
],
)
: status == "Canceled"
? Text(
status,
style: TextStyle(
color: primaryColor,
fontSize: 18,
fontWeight: FontWeight.bold),
)
: status == "Delivered"
? Text(
status,
style: TextStyle(
color: Colors.green, fontSize: 12),
)
2020-06-02 14:56:51 +06:30
: status == "Paid"
? Row(
children: <Widget>[
Padding(
padding:
const EdgeInsets.all(8.0),
child: Icon(Icons.check),
),
Text(
status,
style: TextStyle(
color: primaryColor,
fontSize: 18,
fontWeight: FontWeight.bold),
)
],
)
: Text(
status,
style: TextStyle(
color: primaryColor,
fontSize: 18,
fontWeight: FontWeight.bold),
);
2020-05-29 07:45:27 +06:30
}
call(BuildContext context, String phone) {
showConfirmDialog(context, "contact.phone.confim", () => launch("tel:$phone"),
translationVariables: ["$phone"]);
}
Future<void> displayNotiContent(
BuildContext context, Noti.Notification noti) async {
if (!noti.seen) {
Provider.of<NotificationModel>(context, listen: false).seenID(noti.id);
}
try {
if (noti.itemType == "buyer") {
BuyerModel buyerModel = Provider.of<BuyerModel>(context, listen: false);
Buyer buyer = await buyerModel.getBuyer(noti.itemID);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BuyerInfo(
buyer: buyer,
)),
);
} else if (noti.itemType == "announcement") {
AnnouncementModel announcementModel =
Provider.of<AnnouncementModel>(context, listen: false);
var announce = await announcementModel.getAnnouncement(noti.itemID);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AnnouncementPage(announcement: announce)),
);
} else if (noti.itemType == "reg") {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyRegisterationInfo()),
);
} else if (noti.itemType == "po") {
POSubmissionModel poModel =
Provider.of<POSubmissionModel>(context, listen: false);
var po = await poModel.getPO(noti.itemID);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => POSubmissionForm(
poSubmission: po,
)),
);
} else if (noti.itemType == "do") {
DOModel doModel = Provider.of<DOModel>(context, listen: false);
var _do = await doModel.getDO(noti.itemID);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DOApproval(doSubmission: _do)),
);
} else if (noti.itemType == "price") {
Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => ProductsList()));
} else if (noti.itemType == 'new_device_login') {
Navigator.of(context).push(MaterialPageRoute(builder: (_) => LogList()));
} else if (noti.itemType == 'user') {
UserModel userModel = Provider.of<UserModel>(context, listen: false);
var user = await userModel.getUser(noti.itemID);
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => UserEditor(
user: user,
viewOnly: true,
)));
}
} catch (e) {
log.warning("Error:$e \n ${noti.toString()}");
showMsgDialog(context, "Error", "Notification item not found!");
} finally {}
}
2020-05-31 15:00:11 +06:30
Widget nameWidget(String name) {
return Center(
child: Padding(
padding: const EdgeInsets.only(left: 10.0, top: 8),
child: Text(
name,
style: TextStyle(
color: Colors.black87, fontSize: 18, fontWeight: FontWeight.bold),
),
),
);
}
Widget phoneWidget(BuildContext context, String phone) {
return Container(
padding: EdgeInsets.only(top: 10),
child: Row(
children: <Widget>[
Icon(Icons.phone),
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: labeledText(context, phone, "user.phone"),
),
],
),
);
}
Widget fcsInput(String label, IconData iconData,
2020-06-01 14:24:45 +06:30
{TextEditingController controller, String value}) {
2020-06-02 14:56:51 +06:30
return TextFormField(
initialValue: value,
controller: controller,
cursorColor: primaryColor,
maxLines: null,
minLines: 1,
decoration: InputDecoration(
fillColor: Colors.white,
labelText: label,
labelStyle: TextStyle(fontSize: 16, color: Colors.grey),
filled: true,
icon: Icon(
iconData,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey, width: 1.0)),
));
// return Row(
// children: <Widget>[
// Padding(
// padding: const EdgeInsets.only(right: 8.0),
// child: Icon(iconData),
// ),
// Expanded(
// child: Container(
// child: Row(children: <Widget>[
// Expanded(
// child: TextFormField(
// initialValue: value,
// controller: controller,
// cursorColor: primaryColor,
// maxLines: null,
// minLines: 1,
// textAlign: TextAlign.left,
// decoration: new InputDecoration(
// contentPadding: EdgeInsets.only(top: 8),
// labelText: label,
// labelStyle: TextStyle(fontSize: 14, color: Colors.grey),
// enabledBorder: UnderlineInputBorder(
// borderSide: BorderSide(color: primaryColor, width: 1.0)),
// focusedBorder: UnderlineInputBorder(
// borderSide: BorderSide(color: primaryColor, width: 1.0)),
// ),
// )),
// ]),
// )),
// ],
// );
2020-05-31 15:00:11 +06:30
}
2020-06-01 14:42:42 +06:30
Widget fcsInputReadOnly(String label, IconData iconData,
{TextEditingController controller, String value}) {
2020-06-02 14:56:51 +06:30
return TextFormField(
initialValue: value,
controller: controller,
cursorColor: primaryColor,
maxLines: null,
minLines: 1,
decoration: InputDecoration(
fillColor: Colors.white,
border: InputBorder.none,
labelText: label,
labelStyle: TextStyle(fontSize: 16, color: Colors.grey),
filled: true,
icon: Icon(
iconData,
color: Colors.grey,
),
));
// return Row(
// children: <Widget>[
// Padding(
// padding: const EdgeInsets.only(right: 8.0),
// child: Icon(iconData),
// ),
// Expanded(
// child: Container(
// child: Row(children: <Widget>[
// Expanded(
// child: TextFormField(
// initialValue: value,
// controller: controller,
// cursorColor: primaryColor,
// maxLines: null,
// minLines: 1,
// readOnly: true,
// textAlign: TextAlign.left,
// decoration: new InputDecoration(
// border: InputBorder.none,
// contentPadding: EdgeInsets.only(top: 8),
// labelText: label,
// ),
// )),
// ]),
// )),
// ],
// );
2020-06-01 14:42:42 +06:30
}
2020-05-31 15:00:11 +06:30
Widget fcsDropDown(String label, IconData iconData,
{TextEditingController controller}) {
return Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Icon(iconData),
),
Expanded(
child: Container(
height: 50.0,
child: Row(children: <Widget>[
Expanded(child: _dropDown()),
]),
)),
],
);
}
Widget _dropDown() {
return DropdownButton<String>(
value: "Ko Nge",
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
// style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: primaryColor,
),
onChanged: (String newValue) {},
items: <String>['Ko Nge', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
}
2020-06-01 14:24:45 +06:30
Widget fcsButton(BuildContext context, String text, {Function callack}) {
2020-05-31 15:00:11 +06:30
return Container(
padding: EdgeInsets.only(left: 10, right: 10, top: 10),
child: Container(
height: 45.0,
decoration: BoxDecoration(
color: primaryColor,
shape: BoxShape.rectangle,
),
child: ButtonTheme(
minWidth: 900.0,
height: 100.0,
child: FlatButton(
onPressed: callack,
child: Text(text,
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.bold,
)),
),
),
),
);
}