You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/views/pages/user/profile_page.dart

217 lines
9.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
3 years ago
import 'package:test_sa/controllers/api_routes/http_status_manger.dart';
3 years ago
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/controllers/validator/validator.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/models/user.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/widgets/app_text_form_field.dart';
import 'package:test_sa/views/widgets/buttons/app_back_button.dart';
import 'package:test_sa/views/widgets/buttons/app_button.dart';
import 'package:test_sa/views/widgets/departments/department_button.dart';
import 'package:test_sa/views/widgets/hospitals/hospital_button.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
class ProfilePage extends StatefulWidget {
static final String id = "/user/profile";
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
UserProvider _userProvider;
SettingProvider _settingProvider;
double _width;
double _height;
User _user = User();
bool _firstTime = true;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
_userProvider = Provider.of<UserProvider>(context);
_settingProvider = Provider.of<SettingProvider>(context);
_width = MediaQuery.of(context).size.width;
_height = MediaQuery.of(context).size.height;
Subtitle _subtitle = AppLocalization.of(context).subtitle;
if(_firstTime){
_user = User.fromJson(_userProvider.user.toJson());
_firstTime = false;
}
return Scaffold(
key: _scaffoldKey,
body:LoadingManager(
isLoading: _userProvider.isLoading,
isFailedLoading: false,
stateCode: 200,
onRefresh: () async {},
child: SafeArea(
child: Stack(
children: [
Form(
key: _formKey,
child: ListView(
children: [
//AppNameBar(),
Hero(
tag: "logo",
child: Image(
height: _height/4,
image: AssetImage("assets/images/logo.png"),
),
),
Container(
padding: EdgeInsets.symmetric(horizontal: 16,vertical: 16),
margin: EdgeInsets.symmetric(horizontal: 16),
decoration: BoxDecoration(
color: AColors.primaryColor,
borderRadius: BorderRadius.circular(AppStyle.getBorderRadius(context)),
boxShadow: [
BoxShadow(
color: AColors.grey,
offset: Offset(0,-1),
)
]
),
child: Column(
children: [
ATextFormField(
initialValue: _user.userName,
hintText: _subtitle.name,
enable: false,
prefixIconData: Icons.account_circle,
style: Theme.of(context).textTheme.headline6,
validator: (value) => Validator.hasValue(value)
? null : _subtitle.nameValidateMessage,
onSaved: (value){
_user.userName = value;
},
),
SizedBox(height: 8,),
ATextFormField(
initialValue: _user.email,
hintText: _subtitle.email,
enable: false,
prefixIconData: Icons.email,
textInputType: TextInputType.emailAddress,
style: Theme.of(context).textTheme.headline6,
validator: (value) => Validator.isEmail(value)
? null : _subtitle.emailValidateMessage,
onSaved: (value){
_user.email = value;
},
),
SizedBox(height: 8,),
AbsorbPointer(
child: HospitalButton(
hospital: _user.hospital,
onHospitalPick: (hospital){
_user.hospital = hospital;
setState(() {});
},
),
),
SizedBox(height: 8,),
DepartmentButton(
department: _user.department,
onDepartmentPick: (department){
_user.department = department;
setState(() {});
},
),
SizedBox(height: 8,),
ATextFormField(
initialValue: _user.phoneNumber,
hintText: _subtitle.phoneNumber,
style: Theme.of(context).textTheme.headline6,
prefixIconData: Icons.phone_android,
validator: (value) =>
Validator.isPhoneNumber(value) ? null : _subtitle.phoneNumberValidateMessage,
textInputType: TextInputType.phone,
onSaved: (value){
_user.phoneNumber = value;
},
),
SizedBox(height: 8,),
ATextFormField(
initialValue: _user.whatsApp,
hintText: _subtitle.whatsApp,
style: Theme.of(context).textTheme.headline6,
prefixIconData: FontAwesomeIcons.whatsapp,
prefixIconSize: 36,
validator: (value) =>
Validator.isPhoneNumber(value) ? null : _subtitle.phoneNumberValidateMessage,
textInputType: TextInputType.phone,
onSaved: (value){
_user.whatsApp = value;
},
),
],
),
),
SizedBox(height: 16,),
Center(
child: SizedBox(
height: _width / 8,
width: _width/1.2,
child: AButton(
text: _subtitle.update,
onPressed: () async {
if(!_formKey.currentState.validate())
return;
_formKey.currentState.save();
if(_user.department?.id == null){
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
_subtitle.unitRequired
),
)
);
return;
}
int status = await _userProvider.updateProfile(
user: _user,
host: _settingProvider.host,
);
if(status >= 200 && status < 300){
_settingProvider.setUser(_userProvider.user);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
_subtitle.requestCompleteSuccessfully
),
)
);
}else{
String errorMessage = HttpStatusManger.getStatusMessage(
status: status, subtitle: _subtitle);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
errorMessage
),
)
);
}
},
),
),
),
SizedBox(height: 32,),
],
),
),
ABackButton(),
],
),
),
),
);
}
}