370 lines
12 KiB
Dart
370 lines
12 KiB
Dart
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:brasil_fields/brasil_fields.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:get/get_utils/src/get_utils/get_utils.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:rkm_educa_app/componentes/text_custom.dart';
|
|
import 'package:rkm_educa_app/telas/tela_login.dart';
|
|
import '../componentes/appbar_custom.dart';
|
|
import '../componentes/buttom_custom.dart';
|
|
import '../componentes/input_text.dart';
|
|
import '../componentes/snackBars.dart';
|
|
import '../utils/cores.dart';
|
|
import '../utils/dimensoes.dart';
|
|
import '../utils/fixos.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class TelaCadastro extends StatefulWidget {
|
|
String api;
|
|
|
|
TelaCadastro({required this.api});
|
|
|
|
@override
|
|
State<TelaCadastro> createState() => _TelaCadastroState();
|
|
}
|
|
|
|
class _TelaCadastroState extends State<TelaCadastro> {
|
|
|
|
final inputCPF = TextEditingController();
|
|
final inputSenha = TextEditingController();
|
|
final inputRepita = TextEditingController();
|
|
final inputCodigo = TextEditingController();
|
|
String? cidade;
|
|
String cidadeQuery = '';
|
|
bool carregando = false;
|
|
int tentativas = 0;
|
|
bool smsEnviado = false;
|
|
String nroSMS = '';
|
|
Map? mapAluno;
|
|
|
|
verificacao(){
|
|
if(inputCPF.text.isNotEmpty){
|
|
if(inputSenha.text.isNotEmpty){
|
|
if(inputRepita.text.isNotEmpty){
|
|
if(inputSenha.text == inputRepita.text){
|
|
if(GetUtils.isCpf(inputCPF.text)){
|
|
if(cidadeQuery!=''){
|
|
carregando = true;
|
|
setState(() {});
|
|
buscarCadastro();
|
|
}else{
|
|
showSnackBar(context, 'Selecione sua cidade', Colors.red);
|
|
}
|
|
}else{
|
|
showSnackBar(context, 'CPF Inválido', Colors.red);
|
|
}
|
|
}else{
|
|
showSnackBar(context, 'Senhas não são idênticas', Colors.red);
|
|
}
|
|
}else{
|
|
showSnackBar(context, 'Repita sua senha', Colors.red);
|
|
}
|
|
}else{
|
|
showSnackBar(context, 'Preencha sua senha', Colors.red);
|
|
}
|
|
}else{
|
|
showSnackBar(context, 'Preencha seu CPF', Colors.red);
|
|
}
|
|
}
|
|
|
|
buscarCadastro()async{
|
|
final url = '${widget.api}/buscarcadastrocpf?CPF=${inputCPF.text}';
|
|
print(url);
|
|
final response = await http.get(Uri.parse(url));
|
|
if(response.body == 'false'){
|
|
buscarCPF();
|
|
}else{
|
|
carregando = false;
|
|
setState(() {});
|
|
showSnackBar(context, 'CPF já está cadastrado!', Colors.red);
|
|
}
|
|
}
|
|
|
|
buscarCPF()async{
|
|
final url = '${widget.api}/buscarCPFMae?CPF=${inputCPF.text}&CIDADE=${cidadeQuery.toUpperCase()}';
|
|
print(url);
|
|
final response1 = await http.get(Uri.parse(url));
|
|
mapAluno = response1.body == 'false'?{}:json.decode(response1.body);
|
|
String celularMae = mapAluno!['CELULAR_MAE']??'';
|
|
if(response1.body == 'false' || celularMae==''){
|
|
final url = '${widget.api}/buscarCPFPai?CPF=${inputCPF.text}&CIDADE=${cidadeQuery.toUpperCase()}';
|
|
print(url);
|
|
final response = await http.get(Uri.parse(url));
|
|
mapAluno = response.body == 'false'?{}:json.decode(response.body);
|
|
print(mapAluno);
|
|
print(mapAluno!['CD_ALUNO']);
|
|
// inserirDB(map);
|
|
tentativas = tentativas + 1;
|
|
if(tentativas <=3){
|
|
criarNumero(mapAluno!['DDD_CELULAR_PAI'].toString(),mapAluno!['CELULAR_PAI'].toString());
|
|
}else{
|
|
showSnackBar(context, 'Excedeu suas tentativas, entre em contato com a escola', Colors.red);
|
|
carregando = false;
|
|
setState(() {});
|
|
}
|
|
|
|
if(response.body == 'false'){
|
|
showSnackBar(context, 'CPF não encontrado entre em contato com a escola!', Colors.red);
|
|
carregando = false;
|
|
setState(() {});
|
|
}
|
|
}else{
|
|
mapAluno = json.decode(response1.body);
|
|
print(mapAluno);
|
|
print(mapAluno!['CD_ALUNO']);
|
|
// inserirDB(map);
|
|
tentativas = tentativas + 1;
|
|
if(tentativas <=3){
|
|
criarNumero(mapAluno!['DDD_CELULAR_MAE'].toString(),mapAluno!['CELULAR_MAE'].toString());
|
|
}else{
|
|
showSnackBar(context, 'Excedeu suas tentativas, entre em contato com a escola', Colors.red);
|
|
carregando = false;
|
|
setState(() {});
|
|
}
|
|
}
|
|
}
|
|
|
|
criarNumero(String DDD, String numero){
|
|
var random = Random();
|
|
nroSMS = (random.nextInt(900000) + 100000).toString();
|
|
apiSMS(DDD,numero);
|
|
}
|
|
|
|
apiSMS(String DDD, String numero)async{
|
|
final String apiUrl = 'https://api-rest.zenvia.com/services/send-sms';
|
|
final String authToken = '6nPV7rkTncLaugyoEBHQklHrvD5-GzDVMCeI';
|
|
final headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization':'cmttc2lzdGVtYXMucmVzdDp5Nkc5TnNlNDVV',
|
|
// 'Authorization':'Y2FicmV1dmEucmVzdDo0U0lmYktjUTgy',
|
|
'Cache-Control': 'no-cache'
|
|
};
|
|
|
|
final data = {
|
|
"sendSmsRequest": {
|
|
"from": "RKM EDUCA",
|
|
"to": "+55$DDD$numero",
|
|
"msg": "Seu codigo de acesso : $nroSMS",
|
|
"flashSms": true,
|
|
}
|
|
};
|
|
|
|
print('deu certo +55$DDD$numero');
|
|
|
|
final response = await http.post(
|
|
Uri.parse(apiUrl),
|
|
headers: headers,
|
|
body: json.encode(data),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
print('SMS sent successfully');
|
|
print(response.body);
|
|
smsEnviado = true;
|
|
carregando = false;
|
|
setState(() {});
|
|
showSnackBar(context, 'Código SMS enviado com sucesso!', Colors.green);
|
|
} else {
|
|
print(response.body);
|
|
print(response.statusCode);
|
|
}
|
|
}
|
|
|
|
verificacaoSMS(){
|
|
if(inputCodigo.text == nroSMS){
|
|
smsEnviado = false;
|
|
carregando = true;
|
|
setState(() {});
|
|
showSnackBar(context, 'Acesso validado com sucesso!', Colors.green);
|
|
inserirDB();
|
|
}else{
|
|
showSnackBar(context, 'Código incorreto, tente novamente!', Colors.red);
|
|
}
|
|
}
|
|
|
|
inserirDB()async{
|
|
final url = '${widget.api}/inserirpais';
|
|
|
|
final response = await http.post(
|
|
Uri.parse(url),
|
|
body:{
|
|
'CD_ALUNO' : mapAluno!['CD_ALUNO'],
|
|
'ALUNO': mapAluno!['NO_ALUNO'],
|
|
'CPF': inputCPF.text,
|
|
'NOME' : mapAluno!['NO_PAI'],
|
|
'SENHA' : inputSenha.text,
|
|
'PARENTESCO' : 'PAI',
|
|
'DATA_CADASTRO' : DateFormat('dd/MM/yyyy HH:mm').format(DateTime.now()),
|
|
'CONDICAO' : 'NOVO',
|
|
'CIDADE' : cidadeQuery.toUpperCase()
|
|
}
|
|
);
|
|
if(response.body == 'cadastrado com sucesso' || response.body == 'editado com sucesso'){
|
|
showSnackBar(context, 'Cadastro realizado com sucesso!', Colors.green);
|
|
|
|
inputCPF.clear();
|
|
inputSenha.clear();
|
|
inputRepita.clear();
|
|
carregando = false;
|
|
cidade =null;
|
|
setState((){});
|
|
Navigator.push(context, MaterialPageRoute(builder: (context)=>TelaLogin()));
|
|
|
|
}else{
|
|
carregando = false;
|
|
setState((){});
|
|
showSnackBar(context, 'Erro ao fazer o registro, entre em contato com a escola', Colors.red);
|
|
}
|
|
}
|
|
|
|
Future<bool>back()async{
|
|
if(smsEnviado){
|
|
smsEnviado = false;
|
|
setState((){});
|
|
return false;
|
|
}else{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
List cidades= [];
|
|
for(int i=0;Fixos().listCidades.length>i;i++){
|
|
cidades.add(Fixos().listCidades[i].cidade);
|
|
}
|
|
|
|
return WillPopScope(
|
|
onWillPop:()async=>await back(),
|
|
child: Scaffold(
|
|
appBar: AppBarCustom().appbar(context, 'CADASTRO'),
|
|
body: Container(
|
|
padding: EdgeInsets.all(Dimensoes.width*0.15),
|
|
width: Dimensoes.width,
|
|
height: Dimensoes.height,
|
|
alignment: Alignment.center,
|
|
child: carregando?Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextCustom(text: 'VALIDANDO DADOS ...\n AGUARDE ALGUNS SEGUNDOS !',maxLines: 2,textAlign: TextAlign.center,),
|
|
SizedBox(height: 30,),
|
|
CircularProgressIndicator(color: PaletaCores.corPrimaria,)
|
|
],
|
|
),
|
|
):ListView(
|
|
children: [
|
|
Container(
|
|
child: Image.asset("assets/images/logoEduca.png",width:Dimensoes.width * 0.4,height: Dimensoes.height * 0.12)
|
|
),
|
|
smsEnviado?Container():Container(
|
|
width: Dimensoes.width>900? Dimensoes.width * 0.18:Dimensoes.width*0.7,
|
|
child: InputText(
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.digitsOnly,
|
|
CpfInputFormatter(),
|
|
],
|
|
textInputType: TextInputType.number,
|
|
controller: inputCPF,
|
|
title: 'CPF',
|
|
width: Dimensoes.width>900? Dimensoes.width * 0.18:Dimensoes.width*0.7,
|
|
colorBorder: PaletaCores.branco,
|
|
),
|
|
),
|
|
smsEnviado?Container():Container(
|
|
width: Dimensoes.width>900? Dimensoes.width * 0.18:Dimensoes.width*0.7,
|
|
child: InputText(
|
|
inputFormatters: [],
|
|
obscure: true,
|
|
controller: inputSenha,
|
|
title: 'Senha',
|
|
width: Dimensoes.width>900? Dimensoes.width * 0.18:Dimensoes.width*0.7,
|
|
colorBorder: PaletaCores.branco,
|
|
),
|
|
),
|
|
smsEnviado?Container():Container(
|
|
width: Dimensoes.width>900? Dimensoes.width * 0.18:Dimensoes.width*0.7,
|
|
child: InputText(
|
|
inputFormatters: [],
|
|
obscure: true,
|
|
controller: inputRepita,
|
|
title: 'Repetir Senha',
|
|
width: Dimensoes.width>900? Dimensoes.width * 0.18:Dimensoes.width*0.7,
|
|
colorBorder: PaletaCores.branco,
|
|
),
|
|
),
|
|
smsEnviado?Container():Container(
|
|
width: Dimensoes.width*0.6,
|
|
child: DropdownButtonHideUnderline(
|
|
child: DropdownButton(
|
|
hint: TextCustom(text: 'Cidade'),
|
|
value: cidade,
|
|
items: Fixos().listCidades.map((value) => DropdownMenuItem(
|
|
value: value.cidade,
|
|
child: TextCustom(
|
|
text: value.cidade,
|
|
color: Colors.black54,
|
|
),
|
|
)
|
|
).toList(),
|
|
onChanged: (value){
|
|
setState(() {
|
|
cidade = value!;
|
|
print(cidades.indexOf(cidade!));
|
|
cidadeQuery = Fixos().listCidades[cidades.indexOf(cidade!)].cidadeQuery;
|
|
print(cidadeQuery);
|
|
});
|
|
},
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 20,),
|
|
smsEnviado?Container():ButtonCustom(
|
|
widthCustom: 0.7,
|
|
heightCustom: 0.07,
|
|
text: "Cadastrar",
|
|
fontsize: 14.0,
|
|
colorText: PaletaCores.branco,
|
|
colorButton: PaletaCores.corPrimaria,
|
|
colorBorder: PaletaCores.corPrimaria,
|
|
onPressed: ()=>verificacao(),
|
|
),
|
|
smsEnviado?Container(
|
|
width: Dimensoes.width>900? Dimensoes.width * 0.18:Dimensoes.width*0.7,
|
|
child: InputText(
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.digitsOnly,
|
|
],
|
|
textInputType: TextInputType.number,
|
|
controller: inputCodigo,
|
|
title: 'Código SMS',
|
|
width: Dimensoes.width>900? Dimensoes.width * 0.18:Dimensoes.width*0.7,
|
|
colorBorder: PaletaCores.branco,
|
|
),
|
|
):Container(),
|
|
smsEnviado?ButtonCustom(
|
|
widthCustom: 0.7,
|
|
heightCustom: 0.07,
|
|
text: "Enviar",
|
|
fontsize: 14.0,
|
|
colorText: PaletaCores.branco,
|
|
colorButton: PaletaCores.corPrimaria,
|
|
colorBorder: PaletaCores.corPrimaria,
|
|
onPressed: ()=>verificacaoSMS(),
|
|
):Container(),
|
|
smsEnviado?SizedBox(height: 10):Container(),
|
|
smsEnviado?GestureDetector(
|
|
onTap: ()=>verificacao(),
|
|
child: TextCustom(text: "Não recebeu o SMS?\n Enviar novamente",maxLines: 2,textAlign: TextAlign.center,))
|
|
:Container()
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|