277 lines
10 KiB
Dart
277 lines
10 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:brasil_fields/brasil_fields.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:pcastlivetv/components/nc_form_field.dart';
|
|
import 'package:pcastlivetv/globals.dart';
|
|
import 'package:pcastlivetv/models/user_model.dart';
|
|
import 'package:pcastlivetv/routes.dart';
|
|
import 'package:pcastlivetv/services/snackbar_service.dart';
|
|
import 'package:pcastlivetv/stores/login_store.dart';
|
|
import 'package:validatorless/validatorless.dart';
|
|
import 'package:velocity_x/velocity_x.dart';
|
|
|
|
class RegisterPage extends StatefulWidget {
|
|
const RegisterPage({super.key});
|
|
|
|
@override
|
|
State<RegisterPage> createState() => _RegisterPageState();
|
|
}
|
|
|
|
class _RegisterPageState extends State<RegisterPage> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
LoginStore store = VxState.store as LoginStore;
|
|
|
|
final formKey = GlobalKey<FormState>();
|
|
|
|
TextEditingController name = TextEditingController();
|
|
TextEditingController companyname = TextEditingController();
|
|
TextEditingController email = TextEditingController();
|
|
TextEditingController url = TextEditingController();
|
|
TextEditingController cpfCnpj = TextEditingController();
|
|
TextEditingController password = TextEditingController();
|
|
TextEditingController password2 = TextEditingController();
|
|
TextEditingController channel = TextEditingController();
|
|
|
|
return Container(
|
|
color: const Color.fromRGBO(166, 0, 249, 1),
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Card(
|
|
elevation: 20,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
side: const BorderSide(
|
|
width: 1,
|
|
color: Colors.black,
|
|
),
|
|
),
|
|
child: Column(
|
|
children: [
|
|
const SizedBox(
|
|
height: 32.0,
|
|
),
|
|
"Registrar-se".text.xl5.bold.make(),
|
|
const SizedBox(
|
|
height: 32.0,
|
|
),
|
|
SizedBox(
|
|
width: MediaQuery.of(context).size.width / 2,
|
|
child: Form(
|
|
key: formKey,
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(
|
|
left: 64.0,
|
|
right: 64,
|
|
bottom: 32,
|
|
),
|
|
child: Column(
|
|
children: [
|
|
NCFormField(
|
|
label: 'Nome',
|
|
controller: name,
|
|
validator: Validatorless.required('Nome é obrigatório'),
|
|
),
|
|
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: 'CPF/CNPJ',
|
|
controller: cpfCnpj,
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.digitsOnly,
|
|
CpfOuCnpjFormatter(),
|
|
],
|
|
validator: (value) {
|
|
if (value!.isEmpty) {
|
|
return 'CPF/CNPJ é obrigatório';
|
|
} else if (CPFValidator.isValid(value) || CNPJValidator.isValid(value)) {
|
|
return null;
|
|
}
|
|
return 'CPF ou CNPJ inválido';
|
|
},
|
|
),
|
|
NCFormField(
|
|
label: 'Site',
|
|
controller: url,
|
|
validator: Validatorless.required('Endereço do site é obrigatório'),
|
|
),
|
|
NCFormField(
|
|
label: 'Senha',
|
|
controller: password,
|
|
obscureText: true,
|
|
validator: Validatorless.required('Senha é obrigatória'),
|
|
),
|
|
NCFormField(
|
|
label: 'Confirme Senha',
|
|
controller: password2,
|
|
obscureText: true,
|
|
validator: Validatorless.multiple([
|
|
Validatorless.required('Senha é obrigatória'),
|
|
Validatorless.compare(password, 'Senhas não conferem'),
|
|
]),
|
|
),
|
|
NCFormField(
|
|
label: 'Canal',
|
|
controller: channel,
|
|
validator: Validatorless.multiple([
|
|
Validatorless.required('Nome do canal é obrigatório'),
|
|
Validatorless.onlyCharacters('Nome do canal deve conter apenas letras e números'),
|
|
]),
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color.fromRGBO(166, 0, 249, 1),
|
|
foregroundColor: Colors.white,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
onPressed: () async {
|
|
var result = await registerUser(
|
|
formKey,
|
|
store,
|
|
name.text,
|
|
companyname.text,
|
|
email.text,
|
|
password.text,
|
|
password2.text,
|
|
channel.text,
|
|
cpfCnpj.text,
|
|
url.text,
|
|
);
|
|
if (result) {
|
|
var result = await processAuth(formKey, store, email.text, password.text);
|
|
if (result) {
|
|
router.go('/user');
|
|
}
|
|
}
|
|
},
|
|
child: const Padding(
|
|
padding: EdgeInsets.all(8.0),
|
|
child: Text('Registrar', style: TextStyle(fontSize: 24)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<bool> registerUser(
|
|
GlobalKey<FormState> formKey,
|
|
LoginStore store,
|
|
String name,
|
|
String companyname,
|
|
String email,
|
|
String password,
|
|
String password2,
|
|
String channel,
|
|
String cpfCnpj,
|
|
String url,
|
|
) async {
|
|
if (formKey.currentState!.validate()) {
|
|
formKey.currentState!.save();
|
|
store.email = email;
|
|
store.password = password;
|
|
var data = json.encode({
|
|
"name": name,
|
|
"companyname": companyname,
|
|
"url": url,
|
|
"email": email,
|
|
"password": password,
|
|
"channel": channel,
|
|
"cpfcnpj": cpfCnpj,
|
|
"usertype": "U",
|
|
"blocked": "N",
|
|
"cancelled": "N",
|
|
"createdby": "Site",
|
|
});
|
|
|
|
var dio = Dio();
|
|
|
|
var response = await dio.request(
|
|
Endpoints.register,
|
|
options: Options(method: 'POST', contentType: 'application/json'),
|
|
data: data,
|
|
);
|
|
|
|
switch (response.statusCode) {
|
|
case 200:
|
|
var u = UserModel.fromJson(response.data);
|
|
if (u.message != "") {
|
|
SnackBarService.showSnackBar(context: context, message: u.message!);
|
|
return false;
|
|
}
|
|
break;
|
|
case 406:
|
|
SnackBarService.showSnackBar(context: context, message: 'Usuário já cadastrado');
|
|
break;
|
|
default:
|
|
SnackBarService.showSnackBar(context: context, message: response.statusMessage!);
|
|
}
|
|
}
|
|
return Future.value(true);
|
|
}
|
|
|
|
Future<bool> processAuth(GlobalKey<FormState> formKey, LoginStore store, String email, String password) async {
|
|
Function? close;
|
|
if (formKey.currentState!.validate()) {
|
|
formKey.currentState!.save();
|
|
store.email = email;
|
|
store.password = password;
|
|
try {
|
|
close = context.showLoading(
|
|
msg: '',
|
|
bgColor: Colors.transparent,
|
|
textSize: 20,
|
|
);
|
|
|
|
await store.login();
|
|
close();
|
|
|
|
if (store.isLogged) {
|
|
return true;
|
|
}
|
|
|
|
if (store.message != "") {
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
close!();
|
|
SnackBarService.showSnackBar(context: context, message: e.toString().replaceAll("Exception: ", ""));
|
|
}
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
}
|