Skip to content

Commit

Permalink
Lembrete
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrcode committed Oct 15, 2022
1 parent 9b69492 commit 6c0fd28
Show file tree
Hide file tree
Showing 7 changed files with 516 additions and 10 deletions.
151 changes: 151 additions & 0 deletions lib/Controller/LembreteController.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:organizei/Model/Lembrete/LembreteModel.dart';
import 'package:organizei/Controller/Base/Base.dart';
import 'package:organizei/Repository/LembreteRepository.dart';
import 'package:organizei/services/persistencia/login.configuracoes.dart';

class LembreteController extends Base {
LembreteController(this.repository, this.context);

final BuildContext context;
final LembreteRepository repository;
final formKey = GlobalKey<FormState>();
var model = LembreteModel();
//var loginConfiguracoes = LoginConfiguracoes();

lembreteId(int? value) => model.id = value;
lembreteNome(String? value) => model.nome = value.toString();
lembreteDataehora(String? value) => model.data = value.toString();
lembreteAniversario(bool? value) => model.eAniversario = value;
lembreteCor(String? value) => model.cor = value.toString();

var controllerNome = TextEditingController();
var controllerCor = TextEditingController();
var controllerDataehora = TextEditingController();

Future<bool> saveLembrete() async {
if (!formKey.currentState!.validate()) {
return false;
}

formKey.currentState!.save();

try {
if (model.id == null) {
return await repository.addLembrete(model).then((value) async {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
elevation: 6.0,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
side: const BorderSide(color: Colors.black, width: 3),
borderRadius: BorderRadius.circular(16),
),
content: Text(
value.msg!,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
),
),
backgroundColor: value.valido!
? const Color(0xFF74C198)
: const Color(0xFFEF7E69),
),
);

await Future.delayed(const Duration(milliseconds: 500));

if (value.valido!) {
return value.valido!;
} else {
return false;
}
});
} else {
return await repository.updateLembrete(model).then((value) async {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
elevation: 6.0,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
side: const BorderSide(color: Colors.black, width: 3),
borderRadius: BorderRadius.circular(16),
),
content: Text(
value.msg!,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
),
),
backgroundColor: value.valido!
? const Color(0xFF74C198)
: const Color(0xFFEF7E69),
),
);

await Future.delayed(const Duration(milliseconds: 500));

if (value.valido!) {
return value.valido!;
} else {
return false;
}
});
}
} catch (e) {
print(e);
return false;
}
}

Future<List<LembreteModel>?> getLembretes() async {
return await repository.getLembretes();
}

Future<Map<String, dynamic>> get() async {
return await repository.get();
}

Future<bool> excluirLembrete() async {
try {
return await repository.excluirLembrete(model).then((value) async {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
elevation: 6.0,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
side: const BorderSide(color: Colors.black, width: 3),
borderRadius: BorderRadius.circular(16),
),
content: Text(
value.msg!,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
),
),
backgroundColor: value.valido!
? const Color(0xFF74C198)
: const Color(0xFFEF7E69),
),
);

await Future.delayed(const Duration(milliseconds: 500));

if (value.valido!) {
return value.valido!;
} else {
return false;
}
});
} catch (e) {
print(e);
return false;
}
}
}
4 changes: 2 additions & 2 deletions lib/Model/API/APIModel.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ApiModel {
//static const ApiUrl = "http://10.0.0.91:4444";
//static const ApiUrl = "http://192.168.100.74:4444";
static const ApiUrl = "http://192.168.100.82:4444";
static const ApiUrl = "http://192.168.100.74:4444";
//static const ApiUrl = "http://192.168.100.82:4444";
static const String _token = "fd7bc88b7c0149adbc134d5d0a919814";
static const Map<String, String> headers = {
"Content-Type": "application/json",
Expand Down
31 changes: 31 additions & 0 deletions lib/Model/Lembrete/LembreteModel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:organizei/Model/API/ResponseAPIModel.dart';

class LembreteModel {
int? id;
String? nome;
String? data;
bool? eAniversario;
String? cor;
ResponseAPIModel? responseAPIModel;

LembreteModel({this.id, this.nome, this.data, this.eAniversario, this.cor});

LembreteModel.fromJson(Map<String, dynamic> json) {
id = int.parse(json['id']);
nome = json['nome'];
data = json['data'];
eAniversario = json['eAniversario'];
cor = json['cor'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data['id'] = id;
data['nome'] = nome;
data['data'] = data;
data['eAniversario'] = eAniversario;
data['cor'] = cor;

return data;
}
}
72 changes: 72 additions & 0 deletions lib/Repository/LembreteRepository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'dart:convert';

import 'package:organizei/Model/API/APIModel.dart';

import 'package:http/http.dart' as http;
import 'package:organizei/Model/API/ResponseAPIModel.dart';
import 'package:organizei/Model/Lembrete/LembreteModel.dart';

class LembreteRepository {
Future<ResponseAPIModel> addLembrete(LembreteModel model) async {
var json = {
"nome": model.nome,
"data": model.data,
"eAniversario": model.eAniversario,
"cor": model.cor,
};

final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/lembretes'),
headers: ApiModel.headers, body: jsonEncode(json));

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}

Future<ResponseAPIModel> updateLembrete(LembreteModel model) async {
var json = {
"nome": model.nome,
"data": model.data,
"eAniversario": model.eAniversario,
"cor": model.cor,
};

final response = await http.put(
Uri.parse(ApiModel.ApiUrl + '/lembretes/' + model.id.toString()),
headers: ApiModel.headers,
body: jsonEncode(json));

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}

Future<ResponseAPIModel> excluirLembrete(LembreteModel model) async {
final response = await http.delete(
Uri.parse(ApiModel.ApiUrl + '/lembretes/' + model.id.toString()),
headers: ApiModel.headers);

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}

Future<List<LembreteModel>> getLembretes() async {
Uri _uriSearchProduto = Uri.parse(ApiModel.ApiUrl + '/lembretes');

var _url = Uri.parse(_uriSearchProduto.toString());
final response = await http.get(_url, headers: ApiModel.headers);

Map<String, dynamic> jsonMap = jsonDecode(response.body);
List<LembreteModel> listaLembretes = (jsonMap['lembretes'] as List)
.map((item) => LembreteModel.fromJson(item))
.toList();

return listaLembretes;
}

Future<Map<String, dynamic>> get() async {
Uri _uriSearchProduto = Uri.parse(ApiModel.ApiUrl + '/lembretes');

var _url = Uri.parse(_uriSearchProduto.toString());
final response = await http.get(_url, headers: ApiModel.headers);

Map<String, dynamic> jsonMap = jsonDecode(response.body);

return jsonMap;
}
}
124 changes: 124 additions & 0 deletions lib/components/dialogs/lembretes/lembreteCadastroDialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import 'package:flutter/material.dart';
import 'package:organizei/Model/Lembrete/LembreteModel.dart';
import 'package:organizei/Repository/LembreteRepository.dart';
import 'package:organizei/components/botao.dart';
import 'package:organizei/components/dialog_personalizado.dart';
import 'package:organizei/components/input.dart';
import 'package:organizei/components/selectCor.dart';
import 'package:organizei/components/selectData.dart';
import 'package:organizei/components/selectPrioridade.dart';
import '../../../Controller/LembreteController.dart';

Future<dynamic> criarLembrete(BuildContext context,
{LembreteModel? lembrete = null, Function? fecharDialog = null}) {
late LembreteController lembreteController;

lembreteController = LembreteController(LembreteRepository(), context);

late bool? eAniversario = false;

if (lembrete != null) {
lembreteController.controllerNome.text = lembrete.nome ?? '';
lembreteController.controllerDataehora.text = lembrete.data ?? '';
lembreteController.lembreteAniversario(lembrete.eAniversario);
eAniversario = lembrete.eAniversario;
lembreteController.lembreteCor(lembrete.cor);
lembreteController.lembreteId(lembrete.id);
}

return showDialog(
barrierDismissible: false,
barrierColor: Colors.white.withOpacity(0),
context: context,
builder: (context) {
return StatefulBuilder(builder: (context, StateSetter setState) {
return Scaffold(
backgroundColor: Colors.transparent,
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Material(
type: MaterialType.transparency,
child: Form(
key: lembreteController.formKey,
child: Padding(
padding: const EdgeInsets.only(top: 24.0),
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: MediaQuery.of(context).size.height,
),
child: DialogPersonalizado(
nome: 'Lembrete',
child: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: input(
onSaved: lembreteController.lembreteNome,
textController: lembreteController.controllerNome,
label: 'nome',
),
),
SelectData(
controller: lembreteController.controllerDataehora,
onSaved: lembreteController.lembreteDataehora,
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 4.0),
child: Checkbox(
//tristate: true,
activeColor: const Color(0xFF6385C3),
side: const BorderSide(
width: 2, color: Colors.black),
value: eAniversario,
onChanged: (bool? value) async {
setState(() {
eAniversario = value;
});

lembreteController
.lembreteAniversario(eAniversario);
},
),
),
const Padding(
padding: EdgeInsets.only(bottom: 16),
child: Text('é aniversário?'),
),
],
),
SelectCor(
cor: lembreteController.lembreteCor,
corAtual: lembrete?.cor ?? ''),
Botao(
texto: 'Salvar',
cor: const Color(0xFF6385C3),
clicar: () async {
bool succes =
await lembreteController.saveLembrete();

if (succes == true) {
var nav = Navigator.of(context);
nav.pop();

if (lembrete?.id != null) {
//nav.pop();
nav.pop();
}
fecharDialog!();
}
},
),
],
),
),
),
),
),
),
);
});
});
}
Loading

0 comments on commit 6c0fd28

Please sign in to comment.