import 'dart:convert'; 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:pcastlivetv/components/nc_form_field.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 createState() => _RegisterPageState(); } class _RegisterPageState extends State { @override Widget build(BuildContext context) { LoginStore store = VxState.store as LoginStore; final formKey = GlobalKey(); String name = ''; String companyname = ''; String email = ''; String password = ''; String channel = ''; 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', value: name, validator: Validatorless.required('Nome é obrigatório'), onChanged: (value) => name = value, ), const SizedBox( height: 16.0, ), NCFormField( label: 'Empresa', value: companyname, validator: Validatorless.required('Nome da empresa é obrigatório'), onChanged: (value) => companyname = value, ), const SizedBox( height: 16.0, ), NCFormField( label: 'Email', value: email, validator: Validatorless.multiple( [ Validatorless.email('Email inválido'), Validatorless.required('Email é obrigatório'), ], ), onChanged: (value) => email = value, ), const SizedBox( height: 16.0, ), NCFormField( label: 'Senha', value: password, obscureText: true, validator: Validatorless.required('Senha é obrigatória'), onChanged: (value) => password = value, ), const SizedBox( height: 32.0, ), NCFormField( label: 'Canal', value: channel, validator: Validatorless.required('Nome do canal é obrigatório'), onChanged: (value) => channel = value, ), 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 data = json.encode( { "name": name, "companyname": companyname, "email": email, "password": password, "channel": channel, "usertype": "U", "blocked": "N", "cancelled": "N", "createdby": "Site", }, ); var dio = Dio(); var response = await dio.request( 'http://localhost:8112/register', options: Options( method: 'POST', contentType: 'application/json', ), data: data, ); switch (response.statusCode) { case 200: print(json.encode(response.data)); break; case 406: print('Usuário já cadastrado'); break; default: print(response.statusMessage); } }, child: const Padding( padding: EdgeInsets.all(8.0), child: Text('Registrar', style: TextStyle(fontSize: 24)), ), ), ], ), ), ), ), ], ), ), ], ), ), ), ); } 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()) { 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!(); _displayErrorMotionToast(e.toString().replaceAll("Exception: ", "")); } // SnackBarService.showSnackBar(content: e.toString().replaceAll("Exception: ", ""), error: true); return false; } return false; } }