import 'package:fcs/domain/entities/role.dart'; import 'package:fcs/domain/vo/shipping_address.dart'; import 'package:fcs/localization/app_translations.dart'; import 'package:fcs/localization/transalation.dart'; import 'package:fcs/pages/main/model/language_model.dart'; import 'package:fcs/pages/main/model/main_model.dart'; import 'package:fcs/pages/profile/profile_edit.dart'; import 'package:fcs/pages/shipment_address/model/shipment_address_model.dart'; import 'package:fcs/pages/shipment_address/shipping_address_editor.dart'; import 'package:fcs/pages/shipment_address/shipping_address_row.dart'; import 'package:fcs/pages/widgets/bottom_up_page_route.dart'; 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'; import 'package:fcs/pages/main/util.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; import '../../helpers/theme.dart'; typedef void ProfileCallback(); class Profile extends StatefulWidget { @override _ProfileState createState() => _ProfileState(); } class _ProfileState extends State { GlobalKey key = GlobalKey(); bool _isLoading = false; String selectedLanguage; TextEditingController bizNameController = new TextEditingController(); static final List languagesList = Translation().supportedLanguages; static final List languageCodesList = Translation().supportedLanguagesCodes; final Map 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) { MainModel mainModel = Provider.of(context); if (mainModel.user == null) { return Container(); } final namebox = DisplayText( text: mainModel.user.name, labelTextKey: getLocalString(context, "profile.name"), iconData: Icons.person, ); final phonenumberbox = DisplayText( text: mainModel.user.phone, labelTextKey: getLocalString(context, "profile.phone"), iconData: Icons.phone, ); final fcsIDBox = Row( children: [ Expanded( child: DisplayText( text: mainModel.user.fcsID, labelTextKey: getLocalString(context, "customer.fcs.id"), icon: FcsIDIcon(), ), ), IconButton( icon: Icon(Icons.content_copy, color: Colors.grey), onPressed: () => _copy( getLocalString(context, "customer.fcs.id"), mainModel.user.fcsID), ) ], ); final usaShippingAddressBox = Row( children: [ Expanded( child: DisplayText( text: mainModel.setting.usaAddress, labelTextKey: getLocalString(context, "profile.usa.shipping.address"), iconData: Icons.location_on, ), ), IconButton( icon: Icon(Icons.content_copy, color: Colors.grey), onPressed: () => _copy( getLocalString(context, "profile.usa.shipping.address"), mainModel.setting.usaAddress), ) ], ); final logoutbutton = fcsButton( context, getLocalString(context, "profile.logout"), callack: _logout, iconData: Icons.exit_to_app); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( key: key, appBar: AppBar( centerTitle: true, leading: IconButton( icon: Icon( CupertinoIcons.back, size: 35, color: primaryColor, ), onPressed: () => Navigator.of(context).pop(), ), title: LocalText( context, "profile.title", fontSize: 20, color: primaryColor, ), shadowColor: Colors.transparent, backgroundColor: Colors.white, actions: [], ), body: Padding( padding: const EdgeInsets.all(8.0), child: Column( children: [ Expanded( child: ListView( shrinkWrap: true, children: [ Row( children: [ Expanded(child: namebox), Padding( padding: const EdgeInsets.only(right: 0), child: IconButton( icon: Icon(Icons.edit, color: Colors.grey), onPressed: _editName), ) ], ), // mainModel.isCustomer() // ? Container() // : getPrivilegeBox(context), phonenumberbox, fcsIDBox, usaShippingAddressBox, DisplayText( text: mainModel.user.status, labelTextKey: getLocalString(context, "customer.status"), iconData: Icons.add_alarm, ), // getShippingAddressList(context), ], ), ), logoutbutton, SizedBox(height: 25) ], ), ), ), ); } Widget getShippingAddressList(BuildContext context) { var shipmentModel = Provider.of(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: [ 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 getAddressList( BuildContext context, List 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(); } List privileges = [ Privilege(name: 'Manage shipment'), Privilege(name: 'Manage pickups'), Privilege(name: 'Manage packages'), Privilege(name: 'Manage deliveries'), Privilege(name: 'Admin') ]; Widget getPrivilegeBox(BuildContext context) { var languageModel = Provider.of(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: "Myanmar3"), ), children: [ Align( alignment: Alignment.topLeft, child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: getRowPrivilegeWidget(privileges)), ), ) ], ), ); } List getRowPrivilegeWidget(List privileges) { return privileges.map((p) { return Container( padding: EdgeInsets.all(8.0), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ 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(); } _copy(String title, String data) { Clipboard.setData(ClipboardData(text: data)); _showToast(title); } void _showToast(String title) { 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(CupertinoPageRoute(builder: (context) => ProfileEdit())); } _logout() { showConfirmDialog(context, "profile.logout.confirm", () async { setState(() { _isLoading = true; }); try { await context.read().signout(); Navigator.of(context).pushNamedAndRemoveUntil( "/welcome", ModalRoute.withName('/welcome')); } catch (e) {} finally { Future.delayed(Duration(seconds: 1), () { if (mounted) { setState(() { _isLoading = false; }); } }); } }); } }