register page completed
parent
0a8aeb96d8
commit
9d271357d9
|
|
@ -3,7 +3,7 @@ import 'package:flutter/services.dart';
|
||||||
|
|
||||||
class NCFormField extends StatelessWidget {
|
class NCFormField extends StatelessWidget {
|
||||||
final String label;
|
final String label;
|
||||||
final String value;
|
final TextEditingController controller;
|
||||||
final bool? obscureText;
|
final bool? obscureText;
|
||||||
final void Function(String)? onChanged;
|
final void Function(String)? onChanged;
|
||||||
final String? Function(String?)? validator;
|
final String? Function(String?)? validator;
|
||||||
|
|
@ -12,7 +12,7 @@ class NCFormField extends StatelessWidget {
|
||||||
const NCFormField({
|
const NCFormField({
|
||||||
super.key,
|
super.key,
|
||||||
required this.label,
|
required this.label,
|
||||||
required this.value,
|
required this.controller,
|
||||||
this.onChanged,
|
this.onChanged,
|
||||||
this.validator,
|
this.validator,
|
||||||
this.inputFormatters,
|
this.inputFormatters,
|
||||||
|
|
@ -24,7 +24,7 @@ class NCFormField extends StatelessWidget {
|
||||||
height: 80,
|
height: 80,
|
||||||
child: TextFormField(
|
child: TextFormField(
|
||||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||||
initialValue: value,
|
controller: controller,
|
||||||
validator: validator,
|
validator: validator,
|
||||||
onChanged: onChanged,
|
onChanged: onChanged,
|
||||||
inputFormatters: inputFormatters,
|
inputFormatters: inputFormatters,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
class UserModel {
|
class UserModel {
|
||||||
int? id;
|
int? id;
|
||||||
|
String? message;
|
||||||
String? name;
|
String? name;
|
||||||
String? companyname;
|
String? companyname;
|
||||||
String? email;
|
String? email;
|
||||||
|
|
@ -13,6 +14,7 @@ class UserModel {
|
||||||
|
|
||||||
UserModel.fromJson(Map<String, dynamic> json) {
|
UserModel.fromJson(Map<String, dynamic> json) {
|
||||||
id = json['id'];
|
id = json['id'];
|
||||||
|
message = json['message'] ?? '';
|
||||||
name = json['name'];
|
name = json['name'];
|
||||||
companyname = json['companyname'];
|
companyname = json['companyname'];
|
||||||
email = json['email'];
|
email = json['email'];
|
||||||
|
|
@ -26,6 +28,7 @@ class UserModel {
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
final Map<String, dynamic> data = <String, dynamic>{};
|
final Map<String, dynamic> data = <String, dynamic>{};
|
||||||
data['id'] = id;
|
data['id'] = id;
|
||||||
|
data['message'] = message;
|
||||||
data['name'] = name;
|
data['name'] = name;
|
||||||
data['companyname'] = companyname;
|
data['companyname'] = companyname;
|
||||||
data['email'] = email;
|
data['email'] = email;
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,8 @@ class _LoginPageState extends State<LoginPage> {
|
||||||
|
|
||||||
final formKey = GlobalKey<FormState>();
|
final formKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
String email = '';
|
TextEditingController email = TextEditingController();
|
||||||
String password = '';
|
TextEditingController password = TextEditingController();
|
||||||
return Container(
|
return Container(
|
||||||
color: const Color.fromRGBO(166, 0, 249, 1),
|
color: const Color.fromRGBO(166, 0, 249, 1),
|
||||||
child: Center(
|
child: Center(
|
||||||
|
|
@ -61,24 +61,22 @@ class _LoginPageState extends State<LoginPage> {
|
||||||
children: [
|
children: [
|
||||||
NCFormField(
|
NCFormField(
|
||||||
label: 'Email',
|
label: 'Email',
|
||||||
value: email,
|
controller: email,
|
||||||
validator: Validatorless.multiple(
|
validator: Validatorless.multiple(
|
||||||
[
|
[
|
||||||
Validatorless.email('Email inválido'),
|
Validatorless.email('Email inválido'),
|
||||||
Validatorless.required('Email é obrigatório'),
|
Validatorless.required('Email é obrigatório'),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
onChanged: (value) => email = value,
|
|
||||||
),
|
),
|
||||||
const SizedBox(
|
const SizedBox(
|
||||||
height: 16.0,
|
height: 16.0,
|
||||||
),
|
),
|
||||||
NCFormField(
|
NCFormField(
|
||||||
label: 'Senha',
|
label: 'Senha',
|
||||||
value: password,
|
controller: password,
|
||||||
obscureText: true,
|
obscureText: true,
|
||||||
validator: Validatorless.required('Senha é obrigatória'),
|
validator: Validatorless.required('Senha é obrigatória'),
|
||||||
onChanged: (value) => password = value,
|
|
||||||
),
|
),
|
||||||
const SizedBox(
|
const SizedBox(
|
||||||
height: 32.0,
|
height: 32.0,
|
||||||
|
|
@ -92,7 +90,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
var result = await processAuth(formKey, store, email, password);
|
var result = await processAuth(formKey, store, email.text, password.text);
|
||||||
if (result) {
|
if (result) {
|
||||||
switch (store.userType) {
|
switch (store.userType) {
|
||||||
case 'A':
|
case 'A':
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import 'package:motion_toast/motion_toast.dart';
|
||||||
import 'package:motion_toast/resources/arrays.dart';
|
import 'package:motion_toast/resources/arrays.dart';
|
||||||
import 'package:pcastlivetv/components/nc_form_field.dart';
|
import 'package:pcastlivetv/components/nc_form_field.dart';
|
||||||
import 'package:pcastlivetv/globals.dart';
|
import 'package:pcastlivetv/globals.dart';
|
||||||
|
import 'package:pcastlivetv/models/user_model.dart';
|
||||||
import 'package:pcastlivetv/routes.dart';
|
import 'package:pcastlivetv/routes.dart';
|
||||||
import 'package:pcastlivetv/stores/login_store.dart';
|
import 'package:pcastlivetv/stores/login_store.dart';
|
||||||
import 'package:validatorless/validatorless.dart';
|
import 'package:validatorless/validatorless.dart';
|
||||||
|
|
@ -25,12 +26,12 @@ class _RegisterPageState extends State<RegisterPage> {
|
||||||
|
|
||||||
final formKey = GlobalKey<FormState>();
|
final formKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
String name = '';
|
TextEditingController name = TextEditingController();
|
||||||
String companyname = '';
|
TextEditingController companyname = TextEditingController();
|
||||||
String email = '';
|
TextEditingController email = TextEditingController();
|
||||||
String password = '';
|
TextEditingController password = TextEditingController();
|
||||||
String password2 = '';
|
TextEditingController password2 = TextEditingController();
|
||||||
String channel = '';
|
TextEditingController channel = TextEditingController();
|
||||||
|
|
||||||
return Container(
|
return Container(
|
||||||
color: const Color.fromRGBO(166, 0, 249, 1),
|
color: const Color.fromRGBO(166, 0, 249, 1),
|
||||||
|
|
@ -69,21 +70,17 @@ class _RegisterPageState extends State<RegisterPage> {
|
||||||
),
|
),
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
NCFormField(label: 'Nome', value: name, validator: Validatorless.required('Nome é obrigatório'), onChanged: (value) => name = value),
|
NCFormField(label: 'Nome', controller: name, validator: Validatorless.required('Nome é obrigatório')),
|
||||||
NCFormField(label: 'Empresa', value: companyname, validator: Validatorless.required('Nome da empresa é obrigatório'), onChanged: (value) => companyname = value),
|
NCFormField(label: 'Empresa', controller: companyname, validator: Validatorless.required('Nome da empresa é obrigatório')),
|
||||||
|
NCFormField(label: 'Email', controller: email, validator: Validatorless.multiple([Validatorless.email('Email inválido'), Validatorless.required('Email é obrigatório')])),
|
||||||
|
NCFormField(label: 'Senha', controller: password, obscureText: true, validator: Validatorless.required('Senha é obrigatória')),
|
||||||
NCFormField(
|
NCFormField(
|
||||||
label: 'Email',
|
label: 'Confirme Senha',
|
||||||
value: email,
|
controller: password2,
|
||||||
validator: Validatorless.multiple([Validatorless.email('Email inválido'), Validatorless.required('Email é obrigatório')]),
|
obscureText: true,
|
||||||
onChanged: (value) => email = value),
|
validator: Validatorless.multiple([Validatorless.required('Senha é obrigatória'), Validatorless.compare(password, 'Senhas não conferem')]),
|
||||||
NCFormField(label: 'Senha', value: password, obscureText: true, validator: Validatorless.required('Senha é obrigatória'), onChanged: (value) => password = value),
|
),
|
||||||
NCFormField(
|
NCFormField(label: 'Canal', controller: channel, validator: Validatorless.required('Nome do canal é obrigatório')),
|
||||||
label: 'Confirme Senha',
|
|
||||||
value: password,
|
|
||||||
obscureText: true,
|
|
||||||
validator: Validatorless.required('Senha de confirmação é obrigatória'),
|
|
||||||
onChanged: (value) => password2 = value),
|
|
||||||
NCFormField(label: 'Canal', value: channel, validator: Validatorless.required('Nome do canal é obrigatório'), onChanged: (value) => channel = value),
|
|
||||||
const SizedBox(height: 16.0),
|
const SizedBox(height: 16.0),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
|
|
@ -94,9 +91,9 @@ class _RegisterPageState extends State<RegisterPage> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
var result = await registerUser(name, companyname, email, password, password2, channel);
|
var result = await registerUser(formKey, store, name.text, companyname.text, email.text, password.text, password2.text, channel.text);
|
||||||
if (result) {
|
if (result) {
|
||||||
var result = await processAuth(formKey, store, email, password);
|
var result = await processAuth(formKey, store, email.text, password.text);
|
||||||
if (result) {
|
if (result) {
|
||||||
router.go('/user');
|
router.go('/user');
|
||||||
}
|
}
|
||||||
|
|
@ -139,36 +136,45 @@ class _RegisterPageState extends State<RegisterPage> {
|
||||||
).show(context);
|
).show(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> registerUser(String name, String companyname, String email, String password, String password2, String channel) async {
|
Future<bool> registerUser(GlobalKey<FormState> formKey, LoginStore store, String name, String companyname, String email, String password, String password2, String channel) async {
|
||||||
var data = json.encode({
|
if (formKey.currentState!.validate()) {
|
||||||
"name": name,
|
formKey.currentState!.save();
|
||||||
"companyname": companyname,
|
store.email = email;
|
||||||
"email": email,
|
store.password = password;
|
||||||
"password": password,
|
var data = json.encode({
|
||||||
"channel": channel,
|
"name": name,
|
||||||
"usertype": "U",
|
"companyname": companyname,
|
||||||
"blocked": "N",
|
"email": email,
|
||||||
"cancelled": "N",
|
"password": password,
|
||||||
"createdby": "Site",
|
"channel": channel,
|
||||||
});
|
"usertype": "U",
|
||||||
|
"blocked": "N",
|
||||||
|
"cancelled": "N",
|
||||||
|
"createdby": "Site",
|
||||||
|
});
|
||||||
|
|
||||||
var dio = Dio();
|
var dio = Dio();
|
||||||
|
|
||||||
var response = await dio.request(
|
var response = await dio.request(
|
||||||
Endpoints.register,
|
Endpoints.register,
|
||||||
options: Options(method: 'POST', contentType: 'application/json'),
|
options: Options(method: 'POST', contentType: 'application/json'),
|
||||||
data: data,
|
data: data,
|
||||||
);
|
);
|
||||||
|
|
||||||
switch (response.statusCode) {
|
switch (response.statusCode) {
|
||||||
case 200:
|
case 200:
|
||||||
print(json.encode(response.data));
|
var u = UserModel.fromJson(response.data);
|
||||||
break;
|
if (u.message != "") {
|
||||||
case 406:
|
_displayErrorMotionToast(u.message!);
|
||||||
_displayErrorMotionToast('Usuário já cadastrado');
|
return false;
|
||||||
break;
|
}
|
||||||
default:
|
break;
|
||||||
_displayErrorMotionToast(response.statusMessage!);
|
case 406:
|
||||||
|
_displayErrorMotionToast('Usuário já cadastrado');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
_displayErrorMotionToast(response.statusMessage!);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Future.value(true);
|
return Future.value(true);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue