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/register.dart

215 lines
9.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/api_routes/http_status_manger.dart';
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/extensions/context_extension.dart';
import 'package:test_sa/models/hospital.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/models/user.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/hospitals/hospital_button.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
class Register extends StatefulWidget {
static final String id = "/register";
@override
_RegisterState createState() => _RegisterState();
}
class _RegisterState extends State<Register> {
UserProvider _userProvider;
SettingProvider _settingProvider;
double _width;
double _height;
User _user = User();
bool _obscurePassword = 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;
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(
padding: const EdgeInsets.all(20),
children: [
//AppNameBar(),
//SizedBox(height: 16,),
Hero(
tag: "logo",
child: Padding(
padding: const EdgeInsets.all(16),
child: Image(
height: _height / 6,
image: AssetImage("assets/images/logo.png"),
),
),
),
ATextFormField(
initialValue: _user.userName,
hintText: context.translation.name,
prefixIconData: Icons.account_circle,
style: Theme.of(context).textTheme.headline6,
validator: (value) => Validator.hasValue(value) ? null : context.translation.nameValidateMessage,
onSaved: (value) {
_user.userName = value;
},
),
const SizedBox(height: 12),
ATextFormField(
initialValue: _user.email,
hintText: context.translation.email,
prefixIconData: Icons.email,
textInputType: TextInputType.emailAddress,
style: Theme.of(context).textTheme.headline6,
validator: (value) => Validator.isEmail(value) ? null : context.translation.emailValidateMessage,
onSaved: (value) {
_user.email = value;
},
),
const SizedBox(height: 12),
ATextFormField(
initialValue: _user.password,
hintText: context.translation.password,
prefixIconData: Icons.vpn_key_sharp,
style: Theme.of(context).textTheme.headline6,
obscureText: _obscurePassword,
validator: (value) => Validator.isValidPassword(value) ? null : context.translation.passwordValidateMessage,
showPassword: () {
_obscurePassword = !_obscurePassword;
setState(() {});
},
onSaved: (value) {
_user.password = value;
},
onChange: (value) {
_user.password = value;
},
),
const SizedBox(height: 12),
ATextFormField(
initialValue: _user.password,
prefixIconData: Icons.vpn_key_sharp,
hintText: context.translation.confirmPassword,
style: Theme.of(context).textTheme.headline6,
obscureText: _obscurePassword,
validator: (value) => _user.password == value ? null : context.translation.confirmPasswordValidateMessage,
showPassword: () {
_obscurePassword = !_obscurePassword;
setState(() {});
},
),
const SizedBox(height: 12),
HospitalButton(
hospital: Hospital(id: _user.clientId, name: _user.clientName),
onHospitalPick: (hospital) {
_user.clientId = hospital.id;
_user.clientName = hospital.name;
setState(() {});
},
),
// const SizedBox(height: 12),
// DepartmentButton(
// department: Department(id: _user.departmentId, name: _user.departmentName),
// onDepartmentPick: (department) {
// _user.departmentId = department.id;
// _user.departmentName = department.name;
// setState(() {});
// },
// ),
const SizedBox(height: 12),
ATextFormField(
initialValue: _user.phoneNumber,
hintText: context.translation.phoneNumber,
style: Theme.of(context).textTheme.headline6,
prefixIconData: Icons.phone_android,
validator: (value) => Validator.isPhoneNumber(value) ? null : context.translation.phoneNumberValidateMessage,
textInputType: TextInputType.phone,
onSaved: (value) {
_user.phoneNumber = value;
},
),
SizedBox(height: 8),
// ATextFormField(
// initialValue: _user.whatsApp,
// hintText: context.translation.whatsApp,
// style: Theme.of(context).textTheme.headline6,
// prefixIconData: FontAwesomeIcons.whatsapp,
// prefixIconSize: 36,
// validator: (value) => Validator.isPhoneNumber(value) ? null : context.translation.phoneNumberValidateMessage,
// textInputType: TextInputType.phone,
// onSaved: (value) {
// _user.whatsApp = value;
// },
// ),
const SizedBox(height: 12),
AButton(
text: context.translation.signUp,
onPressed: () async {
if (!_formKey.currentState.validate()) return;
_formKey.currentState.save();
if (_user.clientId == null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(context.translation.hospitalRequired),
));
return;
}
if (_user.departmentId == null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(context.translation.uniteRequired),
));
return;
}
int status = await _userProvider.register(
user: _user,
host: _settingProvider.host,
);
if (status >= 200 && status < 300) {
Fluttertoast.showToast(msg: context.translation.activationAlert);
Navigator.of(context).pop();
} else {
String errorMessage = status == 402
? context.translation.nameExist
: status == 401
? context.translation.emailExist
: HttpStatusManger.getStatusMessage(status: status, subtitle: context.translation);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(errorMessage),
));
}
},
),
],
),
),
ABackButton(),
],
),
),
),
);
}
}