Files
fcs/lib/pages/profile_page.dart
2020-06-30 16:11:58 +06:30

435 lines
14 KiB
Dart

import 'package:fcs/model/shipment_model.dart';
import 'package:fcs/pages_fcs/shipping_address_row.dart';
import 'package:fcs/vo/shipping_address.dart';
import 'package:fcs/widget/bottom_up_page_route.dart';
import 'package:fcs/widget/local_text.dart';
import 'package:fcs/widget/my_data_table.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:package_info/package_info.dart';
import 'package:provider/provider.dart';
import 'package:fcs/model/language_model.dart';
import 'package:fcs/model/main_model.dart';
import 'package:fcs/model/user_model.dart';
import 'package:fcs/pages/profile_edit.dart';
import 'package:fcs/pages/util.dart';
import 'package:fcs/vo/role.dart';
import 'package:fcs/widget/localization/app_translations.dart';
import 'package:fcs/widget/localization/transalation.dart';
import 'package:fcs/widget/progress.dart';
import '../theme/theme.dart';
import 'profile_setting.dart';
import 'shipping_address_editor.dart';
typedef void ProfileCallback();
class Profile extends StatefulWidget {
@override
_ProfileState createState() => _ProfileState();
}
class _ProfileState extends State<Profile> {
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;
});
}
}
@override
Widget build(BuildContext context) {
var languageModel = Provider.of<LanguageModel>(context);
MainModel mainModel = Provider.of<MainModel>(context);
// buildLanguage(languageModel);
_selectedDropdown(String selected) {
setState(() {
selectedLanguage = selected;
languageModel.saveLanguage(selectedLanguage);
});
}
final namebox = Container(
// padding: EdgeInsets.only(left: 25.0, right: 25.0),
padding: EdgeInsets.only(top: 10, left: 25.0, right: 25.0),
child: Container(
height: 45.0,
child: Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 0.0),
child: Text(
AppTranslations.of(context).text("profile.name"),
style: languageModel.isEng
? TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal)
: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal,
fontFamily: "MyanmarUnicode"),
),
),
SizedBox(
width: 30,
),
Container(
child: Center(
child: Text(
mainModel.user == null ? "" : mainModel.user.name,
style:
TextStyle(fontSize: 16.0, fontStyle: FontStyle.normal),
),
),
)
],
),
));
final phonenumberbox = Container(
padding: EdgeInsets.only(left: 25.0, right: 25.0),
height: 45.0,
child: Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 0.0),
child: Text(
AppTranslations.of(context).text("profile.phone"),
style: languageModel.isEng
? TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal)
: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal,
fontFamily: "MyanmarUnicode"),
),
),
SizedBox(
width: 27,
),
Container(
child: Center(
child: Text(
mainModel.user == null
? ""
: mainModel.user.phone == null ? '' : mainModel.user.phone,
style: TextStyle(fontSize: 16.0, fontStyle: FontStyle.normal),
),
),
)
],
),
);
final emailBox = Container(
padding: EdgeInsets.only(top: 10, left: 25.0, right: 25.0),
child: Row(
children: <Widget>[
Text(
AppTranslations.of(context).text("profile.email"),
style: languageModel.isEng
? TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal)
: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal,
fontFamily: "MyanmarUnicode"),
),
SizedBox(
width: 35,
),
Text(
mainModel.user == null
? ""
: mainModel.user.email == null || mainModel.user.email == ''
? ''
: mainModel.user.email,
style: TextStyle(fontSize: 16.0, fontStyle: FontStyle.normal),
),
],
),
);
final languageBox = Container(
padding: EdgeInsets.only(bottom: 0, top: 7, left: 25.0, right: 25.0),
child: Container(
height: 45.0,
child: Row(
children: <Widget>[
Text(
AppTranslations.of(context).text("profile.language"),
style: languageModel.isEng
? TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal)
: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal,
fontFamily: "MyanmarUnicode"),
),
Container(
width: 140,
padding: EdgeInsets.only(left: 30),
child: Theme(
data: new ThemeData(
canvasColor: Colors.white,
),
child: DropdownButton(
hint: Text("English"),
value: selectedLanguage,
isExpanded: true,
iconSize: 40,
items: languagesList
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: _selectedDropdown),
)),
],
),
));
final logoutbutton = Container(
padding: EdgeInsets.only(left: 20.0, right: 24.0),
child: Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Card(
elevation: 23,
child: Container(
height: 45.0,
child: ButtonTheme(
minWidth: 900.0,
height: 100.0,
child: FlatButton.icon(
onPressed: () {
showConfirmDialog(context, "profile.logout.confirm",
() async {
setState(() {
_isLoading = true;
});
await mainModel.logout();
Navigator.of(context).pushNamedAndRemoveUntil(
"/home", ModalRoute.withName('/home'));
Future.delayed(Duration(seconds: 1), () {
if (mounted) {
setState(() {
_isLoading = false;
});
}
});
});
},
label: Text(
AppTranslations.of(context).text("profile.logout"),
style: languageModel.isEng
? TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal)
: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal,
fontFamily: "MyanmarUnicode")),
icon: Icon(
Icons.exit_to_app,
),
),
),
),
)));
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
title: Text(
AppTranslations.of(context).text("profile.title"),
),
backgroundColor: primaryColor,
actions: <Widget>[],
),
body: Column(
children: <Widget>[
Expanded(
child: ListView(
// padding: EdgeInsets.only(left: 25.0, right: 25.0),
shrinkWrap: true,
children: <Widget>[
Row(
children: <Widget>[
namebox,
Padding(
padding: const EdgeInsets.only(left: 18.0),
child: Icon(Icons.edit),
)
],
),
mainModel.isBuyer() ? Container() : getPrivilegeBox(context),
phonenumberbox,
mainModel.user == null
? Container()
: mainModel.user.email == null ||
mainModel.user.email == ''
? Container()
: emailBox,
languageBox,
getShippingAddressList(context),
],
),
),
logoutbutton,
SizedBox(height: 25)
],
),
),
);
}
Widget getShippingAddressList(BuildContext context) {
var shipmentModel = Provider.of<ShipmentModel>(context);
return Container(
padding: EdgeInsets.only(top: 5, left: 10),
child: ExpansionTile(
title: Text(
"My Addresses",
style: TextStyle(
fontWeight: FontWeight.bold, fontStyle: FontStyle.normal),
),
children: <Widget>[
Column(
children: getAddressList(context, shipmentModel.shippingAddresses),
),
Container(
padding: EdgeInsets.only(top: 20, bottom: 15, right: 15),
child: Align(
alignment: Alignment.bottomRight,
child: Container(
width: 130,
height: 40,
child: FloatingActionButton.extended(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
onPressed: () {
Navigator.push(
context,
BottomUpPageRoute(ShippingAddressEditor()),
);
},
icon: Icon(Icons.add),
label: Text(
'Add New\nAddress',
style: TextStyle(fontSize: 12),
),
backgroundColor: primaryColor,
),
),
),
)
],
),
);
}
List<Widget> getAddressList(
BuildContext context, List<ShippingAddress> addresses) {
return addresses.asMap().entries.map((s) {
return InkWell(
onTap: () {
Navigator.push(
context,
BottomUpPageRoute(ShippingAddressEditor(shippingAddress: s.value)),
);
},
child: ShippingAddressRow(shippingAddress: s.value, index: s.key),
);
}).toList();
}
Widget getPrivilegeBox(BuildContext context) {
var languageModel = Provider.of<LanguageModel>(context);
var userModel = Provider.of<UserModel>(context);
return ListTileTheme(
contentPadding: EdgeInsets.all(0),
child: ExpansionTile(
title: Text(
AppTranslations.of(context).text("profile.privilege"),
style: languageModel.isEng
? TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal,
)
: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.normal,
fontFamily: "MyanmarUnicode"),
),
children: <Widget>[
Align(
alignment: Alignment.topLeft,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:
getRowPrivilegeWidget(userModel.getUserPrivileges())),
),
)
],
),
);
}
List<Widget> getRowPrivilegeWidget(List<Privilege> privileges) {
return privileges.map((p) {
return Container(
padding: EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(p.name,
style: TextStyle(fontSize: 16.0, fontStyle: FontStyle.normal)),
SizedBox(
width: 30,
),
Container(
child: Text(
"- ${p.desc}",
style: TextStyle(fontSize: 16.0, fontStyle: FontStyle.normal),
),
)
],
),
);
}).toList();
}
}