Files
fcs/lib/pages/profile/profile_page.dart

316 lines
9.5 KiB
Dart
Raw Normal View History

2020-10-11 02:17:23 +06:30
import 'package:fcs/domain/entities/user.dart';
2020-10-08 11:38:05 +06:30
import 'package:fcs/domain/vo/delivery_address.dart';
2020-10-11 02:17:23 +06:30
import 'package:fcs/domain/vo/privilege.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/localization/transalation.dart';
2020-10-08 15:54:43 +06:30
import 'package:fcs/pages/delivery_address/delivery_address_list.dart';
2020-10-11 02:17:23 +06:30
import 'package:fcs/pages/delivery_address/delivery_address_row.dart';
2020-10-08 15:54:43 +06:30
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:fcs/pages/main/model/main_model.dart';
2020-10-11 02:17:23 +06:30
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/profile/profile_currency_edit.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/profile/profile_edit.dart';
2020-10-11 02:17:23 +06:30
import 'package:fcs/pages/staff/model/staff_model.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
2020-10-13 07:50:25 +06:30
import 'package:fcs/pages/widgets/defalut_delivery_address.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/display_text.dart';
import 'package:fcs/pages/widgets/fcs_id_icon.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
2020-09-12 03:34:52 +06:30
import 'package:flutter/cupertino.dart';
2020-08-30 21:26:37 +06:30
import 'package:flutter/material.dart';
2020-09-13 21:49:39 +06:30
import 'package:flutter/services.dart';
2020-10-11 02:17:23 +06:30
import 'package:flutter_icons/flutter_icons.dart';
2020-08-30 21:26:37 +06:30
import 'package:provider/provider.dart';
2020-05-29 07:45:27 +06:30
2020-09-13 21:49:39 +06:30
import '../../helpers/theme.dart';
2020-05-29 07:45:27 +06:30
typedef void ProfileCallback();
class Profile extends StatefulWidget {
@override
_ProfileState createState() => _ProfileState();
}
class _ProfileState extends State<Profile> {
2020-09-13 21:49:39 +06:30
GlobalKey key = GlobalKey();
2020-05-29 07:45:27 +06:30
bool _isLoading = false;
String selectedLanguage;
TextEditingController bizNameController = new TextEditingController();
static final List<String> languagesList = Translation().supportedLanguages;
static final List<String> languageCodesList =
Translation().supportedLanguagesCodes;
final Map<dynamic, dynamic> languagesMap = {
languagesList[0]: languageCodesList[0],
languagesList[1]: languageCodesList[1],
};
buildLanguage(LanguageModel languageModel) async {
var lan = await languageModel.load();
if (this.selectedLanguage != lan) {
setState(() {
this.selectedLanguage = lan;
});
}
}
2020-10-08 15:54:43 +06:30
@override
void initState() {
super.initState();
}
2020-05-29 07:45:27 +06:30
@override
Widget build(BuildContext context) {
MainModel mainModel = Provider.of<MainModel>(context);
2020-09-16 02:29:50 +06:30
if (mainModel.user == null) {
return Container();
}
2020-10-11 02:17:23 +06:30
DeliveryAddressModel deliveryAddressModel =
Provider.of<DeliveryAddressModel>(context);
2020-09-13 21:49:39 +06:30
final namebox = DisplayText(
2020-10-11 02:17:23 +06:30
text: mainModel.user.name + " (${mainModel.user.status})",
2020-10-08 11:38:05 +06:30
labelTextKey: "profile.name",
2020-09-13 21:49:39 +06:30
iconData: Icons.person,
);
2020-10-11 02:17:23 +06:30
final currencyBox = DisplayText(
text: mainModel.user.preferCurrency,
labelTextKey: "profile.currency",
iconData: FontAwesome5.money_bill_alt,
);
2020-06-24 16:06:40 +06:30
2020-09-13 21:49:39 +06:30
final phonenumberbox = DisplayText(
text: mainModel.user.phone,
2020-10-08 11:38:05 +06:30
labelTextKey: "profile.phone",
2020-09-13 21:49:39 +06:30
iconData: Icons.phone,
);
final fcsIDBox = Row(
children: [
Expanded(
child: DisplayText(
text: mainModel.user.fcsID,
2020-10-08 11:38:05 +06:30
labelTextKey: "customer.fcs.id",
2020-09-18 04:04:21 +06:30
icon: FcsIDIcon(),
2020-05-29 07:45:27 +06:30
),
2020-09-13 21:49:39 +06:30
),
IconButton(
icon: Icon(Icons.content_copy, color: Colors.grey),
onPressed: () => _copy(
getLocalString(context, "customer.fcs.id"), mainModel.user.fcsID),
)
],
);
2020-10-08 15:54:43 +06:30
2020-09-13 21:49:39 +06:30
final usaShippingAddressBox = Row(
children: [
Expanded(
child: DisplayText(
text: mainModel.setting.usaAddress,
2020-10-08 11:38:05 +06:30
labelTextKey: "profile.usa.shipping.address",
2020-09-13 21:49:39 +06:30
iconData: Icons.location_on,
2020-05-29 07:45:27 +06:30
),
2020-09-13 21:49:39 +06:30
),
IconButton(
icon: Icon(Icons.content_copy, color: Colors.grey),
onPressed: () => _copy(
getLocalString(context, "profile.usa.shipping.address"),
mainModel.setting.usaAddress),
)
],
2020-05-29 07:45:27 +06:30
);
2020-09-13 21:49:39 +06:30
final logoutbutton = fcsButton(
context, getLocalString(context, "profile.logout"),
callack: _logout, iconData: Icons.exit_to_app);
2020-05-29 07:45:27 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
2020-09-13 21:49:39 +06:30
key: key,
2020-05-29 07:45:27 +06:30
appBar: AppBar(
2020-09-13 21:49:39 +06:30
centerTitle: true,
2020-09-12 03:34:52 +06:30
leading: IconButton(
icon: Icon(
CupertinoIcons.back,
2020-09-13 21:49:39 +06:30
size: 35,
color: primaryColor,
2020-09-12 03:34:52 +06:30
),
onPressed: () => Navigator.of(context).pop(),
),
2020-09-18 04:04:21 +06:30
title: LocalText(
context,
"profile.title",
fontSize: 20,
color: primaryColor,
2020-05-29 07:45:27 +06:30
),
2020-09-13 21:49:39 +06:30
shadowColor: Colors.transparent,
backgroundColor: Colors.white,
2020-05-29 07:45:27 +06:30
),
2020-09-13 21:49:39 +06:30
body: Padding(
padding: const EdgeInsets.all(8.0),
2020-10-11 02:17:23 +06:30
child: ListView(
2020-09-13 21:49:39 +06:30
children: <Widget>[
2020-10-11 02:17:23 +06:30
Row(
children: <Widget>[
Expanded(child: namebox),
Padding(
padding: const EdgeInsets.only(right: 0),
child: IconButton(
icon: Icon(Icons.edit, color: Colors.grey),
onPressed: _editName),
)
],
2020-06-25 16:19:23 +06:30
),
2020-10-11 02:17:23 +06:30
phonenumberbox,
fcsIDBox,
usaShippingAddressBox,
Row(
children: <Widget>[
Expanded(child: currencyBox),
Padding(
padding: const EdgeInsets.only(right: 0),
child: IconButton(
icon: Icon(Icons.edit, color: Colors.grey),
onPressed: _editCurrency),
)
],
),
2020-10-13 07:50:25 +06:30
DefaultDeliveryAddress(
labelKey: "profile.default.delivery.address",
deliveryAddress: deliveryAddressModel.defalutAddress,
onTap: () {
Navigator.push(
context, BottomUpPageRoute(DeliveryAddressList()));
},
),
2020-10-11 02:17:23 +06:30
getPrivilegeBox(context),
SizedBox(height: 15),
2020-09-13 21:49:39 +06:30
logoutbutton,
SizedBox(height: 25)
],
),
2020-05-29 07:45:27 +06:30
),
),
);
}
Widget getPrivilegeBox(BuildContext context) {
2020-10-11 02:17:23 +06:30
User user = Provider.of<MainModel>(context, listen: false).user;
List<Privilege> _privileges =
Provider.of<StaffModel>(context, listen: false).privileges;
2020-05-29 15:53:37 +06:30
2020-10-11 02:17:23 +06:30
if (user == null || user.isCustomer()) return Container();
List<Privilege> privileges = [];
user.privileges.forEach((e) {
var p = _privileges.firstWhere((p) => p.id == e, orElse: () => null);
if (p != null) {
privileges.add(p);
}
});
return Column(
children: <Widget>[
DisplayText(
labelTextKey: "profile.privileges",
iconData: MaterialCommunityIcons.clipboard_check_outline,
2020-05-29 07:45:27 +06:30
),
2020-10-11 02:17:23 +06:30
Padding(
padding: const EdgeInsets.only(left: 30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: getRowPrivilegeWidget(privileges)),
)
],
2020-05-29 07:45:27 +06:30
);
}
List<Widget> getRowPrivilegeWidget(List<Privilege> privileges) {
return privileges.map((p) {
return Container(
2020-10-11 02:17:23 +06:30
padding: EdgeInsets.all(3.0),
2020-05-29 07:45:27 +06:30
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2020-10-11 02:17:23 +06:30
Icon(
p.iconData,
color: Colors.black38,
),
2020-05-29 07:45:27 +06:30
SizedBox(
2020-10-11 02:17:23 +06:30
width: 10,
2020-05-29 07:45:27 +06:30
),
2020-10-11 02:17:23 +06:30
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("${p.name}",
style: TextStyle(
fontSize: 16.0,
fontStyle: FontStyle.normal,
color: primaryColor,
)),
Text(
"${p.desc}",
style: TextStyle(
fontSize: 14.0,
fontStyle: FontStyle.normal,
color: Colors.black38),
),
],
2020-05-29 07:45:27 +06:30
),
)
],
),
);
}).toList();
}
2020-09-13 21:49:39 +06:30
_copy(String title, String data) {
Clipboard.setData(ClipboardData(text: data));
_showToast(title);
}
2020-10-11 02:17:23 +06:30
_showToast(String title) {
2020-09-13 21:49:39 +06:30
final ScaffoldState scaffold = key.currentState;
scaffold.showSnackBar(
SnackBar(
content: Text('copied "$title" data to clipboard'),
backgroundColor: secondaryColor,
duration: Duration(seconds: 1),
),
);
}
_editName() {
Navigator.of(context)
.push<void>(CupertinoPageRoute(builder: (context) => ProfileEdit()));
}
2020-10-11 02:17:23 +06:30
_editCurrency() {
Navigator.of(context).push<void>(
CupertinoPageRoute(builder: (context) => ProfileCurrencyEdit()));
}
2020-09-13 21:49:39 +06:30
_logout() {
showConfirmDialog(context, "profile.logout.confirm", () async {
setState(() {
_isLoading = true;
});
2020-09-22 03:52:48 +06:30
try {
await context.read<MainModel>().signout();
Navigator.of(context).pushNamedAndRemoveUntil(
"/welcome", ModalRoute.withName('/welcome'));
} catch (e) {} finally {
Future.delayed(Duration(seconds: 1), () {
if (mounted) {
setState(() {
_isLoading = false;
});
}
});
}
2020-09-13 21:49:39 +06:30
});
}
2020-05-29 07:45:27 +06:30
}