From 8601b2ac6b9b305d1348d8cc4f9c5b77f06d64cb Mon Sep 17 00:00:00 2001 From: Nilo Roberto C Paim Date: Tue, 19 Sep 2023 18:23:36 -0300 Subject: [PATCH] feature: changes on register page / useer model --- lib/components/nc_form_field.dart | 2 +- lib/models/event_model.dart | 33 ++++++++ lib/models/user_model.dart | 8 +- lib/pages/login_page.dart | 25 +----- lib/pages/register_page.dart | 117 +++++++++++++++++++++-------- lib/services/snackbar_service.dart | 35 ++++----- pubspec.lock | 16 ++++ pubspec.yaml | 2 + 8 files changed, 161 insertions(+), 77 deletions(-) create mode 100644 lib/models/event_model.dart diff --git a/lib/components/nc_form_field.dart b/lib/components/nc_form_field.dart index dc72507..9d0ddf6 100644 --- a/lib/components/nc_form_field.dart +++ b/lib/components/nc_form_field.dart @@ -21,7 +21,7 @@ class NCFormField extends StatelessWidget { @override Widget build(BuildContext context) { return SizedBox( - height: 80, + height: 70, child: TextFormField( autovalidateMode: AutovalidateMode.onUserInteraction, controller: controller, diff --git a/lib/models/event_model.dart b/lib/models/event_model.dart new file mode 100644 index 0000000..9a7cfab --- /dev/null +++ b/lib/models/event_model.dart @@ -0,0 +1,33 @@ +class Events { + int? id; + String? name; + String? description; + String? startDt; + int? user; + String? eventtype; + String? transmitted; + + Events({this.id, this.name, this.description, this.startDt, this.user, this.eventtype, this.transmitted}); + + Events.fromJson(Map json) { + id = json['id']; + name = json['name']; + description = json['description']; + startDt = json['startDt']; + user = json['user']; + eventtype = json['eventtype']; + transmitted = json['transmitted']; + } + + Map toJson() { + final Map data = {}; + data['id'] = id; + data['name'] = name; + data['description'] = description; + data['startDt'] = startDt; + data['user'] = user; + data['eventtype'] = eventtype; + data['transmitted'] = transmitted; + return data; + } +} diff --git a/lib/models/user_model.dart b/lib/models/user_model.dart index 360f105..0f0d104 100644 --- a/lib/models/user_model.dart +++ b/lib/models/user_model.dart @@ -3,20 +3,24 @@ class UserModel { String? message; String? name; String? companyname; + String? url; String? email; String? channel; + String? cpfcnpj; String? usertype; String? blocked; String? cancelled; String? createdby; - UserModel({this.id, this.name, this.companyname, this.email, this.channel, this.usertype, this.blocked, this.cancelled, this.createdby}); + UserModel({this.id, this.name, this.companyname, this.email, this.channel, this.usertype, this.blocked, this.cancelled, this.createdby, this.cpfcnpj, this.url}); UserModel.fromJson(Map json) { id = json['id']; message = json['message'] ?? ''; name = json['name']; companyname = json['companyname']; + url = json['url']; + cpfcnpj = json['cpfcnpj']; email = json['email']; channel = json['channel']; usertype = json['usertype']; @@ -31,6 +35,8 @@ class UserModel { data['message'] = message; data['name'] = name; data['companyname'] = companyname; + data['url'] = url; + data['cpfcnpj'] = cpfcnpj; data['email'] = email; data['channel'] = channel; data['usertype'] = usertype; diff --git a/lib/pages/login_page.dart b/lib/pages/login_page.dart index cc1427e..74db2b0 100644 --- a/lib/pages/login_page.dart +++ b/lib/pages/login_page.dart @@ -1,8 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:motion_toast/motion_toast.dart'; -import 'package:motion_toast/resources/arrays.dart'; import 'package:pcastlivetv/components/nc_form_field.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'; @@ -105,7 +104,7 @@ class _LoginPageState extends State { default: } } else { - _displayErrorMotionToast(store.message!); + SnackBarService.showSnackBar(context: context, message: store.message!); } }, child: const Padding( @@ -127,23 +126,6 @@ class _LoginPageState extends State { ); } - void _displayErrorMotionToast(String message) { - MotionToast.error( - title: const Text( - "ERRO", - style: TextStyle( - fontWeight: FontWeight.bold, - ), - ), - description: Text(message), - position: MotionToastPosition.top, - barrierColor: Colors.black.withOpacity(0.3), - width: 300, - height: 80, - dismissable: false, - ).show(context); - } - Future processAuth(GlobalKey formKey, LoginStore store, String email, String password) async { Function? close; if (formKey.currentState!.validate()) { @@ -169,9 +151,8 @@ class _LoginPageState extends State { } } catch (e) { close!(); - _displayErrorMotionToast(e.toString().replaceAll("Exception: ", "")); + SnackBarService.showSnackBar(context: context, message: e.toString().replaceAll("Exception: ", "")); } - // SnackBarService.showSnackBar(content: e.toString().replaceAll("Exception: ", ""), error: true); return false; } return false; diff --git a/lib/pages/register_page.dart b/lib/pages/register_page.dart index 2952efb..5f0fa3e 100644 --- a/lib/pages/register_page.dart +++ b/lib/pages/register_page.dart @@ -1,13 +1,14 @@ import 'dart:convert'; +import 'package:brasil_fields/brasil_fields.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; -import 'package:motion_toast/motion_toast.dart'; -import 'package:motion_toast/resources/arrays.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'; @@ -29,6 +30,8 @@ class _RegisterPageState extends State { TextEditingController name = TextEditingController(); TextEditingController companyname = TextEditingController(); TextEditingController email = TextEditingController(); + TextEditingController url = TextEditingController(); + TextEditingController cpfCnpj = TextEditingController(); TextEditingController password = TextEditingController(); TextEditingController password2 = TextEditingController(); TextEditingController channel = TextEditingController(); @@ -70,27 +73,69 @@ class _RegisterPageState extends State { ), 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: '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')], + [ + Validatorless.email('Email inválido'), + Validatorless.required('Email é obrigatório'), + ], ), ), - NCFormField(label: 'Senha', controller: password, obscureText: true, validator: Validatorless.required('Senha é obrigatória')), + 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')]), + 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')]), + 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( @@ -102,7 +147,18 @@ class _RegisterPageState extends State { ), ), onPressed: () async { - var result = await registerUser(formKey, store, name.text, companyname.text, email.text, password.text, password2.text, channel.text); + 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) { @@ -130,24 +186,18 @@ class _RegisterPageState extends State { ); } - void _displayErrorMotionToast(String message) { - MotionToast.error( - title: const Text( - "ERRO", - style: TextStyle( - fontWeight: FontWeight.bold, - ), - ), - description: Text(message), - position: MotionToastPosition.top, - barrierColor: Colors.black.withOpacity(0.3), - width: 300, - height: 80, - dismissable: false, - ).show(context); - } - - Future registerUser(GlobalKey formKey, LoginStore store, String name, String companyname, String email, String password, String password2, String channel) async { + Future registerUser( + GlobalKey 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; @@ -155,9 +205,11 @@ class _RegisterPageState extends State { var data = json.encode({ "name": name, "companyname": companyname, + "url": url, "email": email, "password": password, "channel": channel, + "cpfcnpj": cpfCnpj, "usertype": "U", "blocked": "N", "cancelled": "N", @@ -176,15 +228,15 @@ class _RegisterPageState extends State { case 200: var u = UserModel.fromJson(response.data); if (u.message != "") { - _displayErrorMotionToast(u.message!); + SnackBarService.showSnackBar(context: context, message: u.message!); return false; } break; case 406: - _displayErrorMotionToast('Usuário já cadastrado'); + SnackBarService.showSnackBar(context: context, message: 'Usuário já cadastrado'); break; default: - _displayErrorMotionToast(response.statusMessage!); + SnackBarService.showSnackBar(context: context, message: response.statusMessage!); } } return Future.value(true); @@ -215,9 +267,8 @@ class _RegisterPageState extends State { } } catch (e) { close!(); - _displayErrorMotionToast(e.toString().replaceAll("Exception: ", "")); + SnackBarService.showSnackBar(context: context, message: e.toString().replaceAll("Exception: ", "")); } - // SnackBarService.showSnackBar(content: e.toString().replaceAll("Exception: ", ""), error: true); return false; } return false; diff --git a/lib/services/snackbar_service.dart b/lib/services/snackbar_service.dart index 53b7b4b..60f8fbd 100644 --- a/lib/services/snackbar_service.dart +++ b/lib/services/snackbar_service.dart @@ -1,27 +1,22 @@ import 'package:flutter/material.dart'; - -import '../globals.dart'; +import 'package:motion_toast/motion_toast.dart'; +import 'package:motion_toast/resources/arrays.dart'; class SnackBarService { - static void showSnackBar({required String content, bool error = false}) { - snackbarKey.currentState?.showSnackBar( - SnackBar( - content: Text( - content, - textAlign: TextAlign.center, - style: const TextStyle( - fontSize: 20, - fontWeight: FontWeight.bold, - color: Colors.black, - ), + static void showSnackBar({required BuildContext context, required String message}) { + MotionToast.error( + title: const Text( + "ERRO", + style: TextStyle( + fontWeight: FontWeight.bold, ), - backgroundColor: error ? Colors.red : Colors.green, - duration: const Duration(seconds: 3), - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(10.0), - ), - elevation: 8.0, ), - ); + description: Text(message), + position: MotionToastPosition.top, + barrierColor: Colors.black.withOpacity(0.3), + width: 300, + height: 80, + dismissable: false, + ).show(context); } } diff --git a/pubspec.lock b/pubspec.lock index 72f51a4..b7d5c1a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -25,6 +25,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.1" + brasil_fields: + dependency: "direct main" + description: + name: brasil_fields + sha256: "765f59e4e22fbfbc10d59d0208d369bad611c714419093ad14668a41863e49a8" + url: "https://pub.dev" + source: hosted + version: "1.14.0" characters: dependency: transitive description: @@ -49,6 +57,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.17.2" + cpf_cnpj_validator: + dependency: "direct main" + description: + name: cpf_cnpj_validator + sha256: "2d52caefe720f7428d8fd12b5dc3a9603e137ae4eafe032b8f1bd104d3656bfb" + url: "https://pub.dev" + source: hosted + version: "2.0.0" cupertino_icons: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index fd8264c..11746e6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -20,6 +20,8 @@ dependencies: flutter_web_plugins: sdk: flutter + brasil_fields: ^1.14.0 + cpf_cnpj_validator: ^2.0.0 dev_dependencies: flutter_test: