Skip to content

Commit

Permalink
lista e itens
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrcode committed Oct 15, 2022
1 parent 6fdf5a0 commit b0e1bcb
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 87 deletions.
38 changes: 38 additions & 0 deletions lib/Controller/ItemController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,42 @@ class ItemController extends Base {
return false;
}
}

Future<bool> excluirItem() async {
try {
return await repository.excluirItem(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;
}
}
}
9 changes: 9 additions & 0 deletions lib/Repository/ItemRepository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ class ItemRepository {

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

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

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}
}
175 changes: 92 additions & 83 deletions lib/components/dialogs/listas/listaCadastroDialog.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:organizei/Controller/ItemController.dart';
import 'package:organizei/Model/Item/ItemModel.dart';
import 'package:organizei/Model/Lista/ListaModel.dart';
import 'package:organizei/Repository/ItemRepository.dart';
import 'package:organizei/Repository/ListaRepository.dart';
import 'package:organizei/components/botao.dart';
import 'package:organizei/components/dialog_personalizado.dart';
Expand All @@ -12,6 +14,8 @@ Future<dynamic> criarLista(BuildContext context,
{ListaModel? lista = null, Function? fecharDialog = null}) {
late ListaController listaController;
late List<TextEditingController> _controllers = [new TextEditingController()];
late ItemController itemController;
itemController = ItemController(ItemRepository(), context);

listaController = ListaController(ListaRepository(), context);

Expand Down Expand Up @@ -50,97 +54,102 @@ Future<dynamic> criarLista(BuildContext context,
type: MaterialType.transparency,
child: Form(
key: listaController.formKey,
child: Container(
margin: const EdgeInsets.only(top: 24),
height: itens!.length < 3
? MediaQuery.of(context).size.height
: null,
child: DialogPersonalizado(
nome: 'Lista',
child: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: input(
onSaved: listaController.listaNome,
textController: listaController.controllerNome,
label: 'nome',
child: Padding(
padding: const EdgeInsets.only(top: 24.0),
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: MediaQuery.of(context).size.height,
),
child: DialogPersonalizado(
nome: 'Lista',
child: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: input(
onSaved: listaController.listaNome,
textController: listaController.controllerNome,
label: 'nome',
),
),
),
SelectCor(
cor: listaController.listaCor,
corAtual: lista?.cor ?? ''),
ListView.builder(
primary: false,
shrinkWrap: true,
itemCount: itens.length,
itemBuilder: (context, index) {
//var dialog;

// var controllerNomeItem = TextEditingController();

return Padding(
padding: const EdgeInsets.only(bottom: 16),
child: input(
//onSaved: listaController.listaNome,
textController: _controllers[index],
label: 'item',
excluir: true,
funcao: () {
setState(() {
itens!.remove(itens[index]);
_controllers.remove(_controllers[index]);
});
},
),
);
}),
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Botao(
texto: 'Novo item',
cor: const Color(0xFF6BC8E4),
clicar: () async {
setState(() {
itens!.add(ItemModel());
_controllers.add(new TextEditingController());
});
},
),
),
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Botao(
texto: 'Salvar',
cor: const Color(0xFF6385C3),
clicar: () async {
List<ItemModel?>? listaitens = [];

_controllers.asMap().entries.map((item) {
listaitens.add(
ItemModel(
nome: (item.value.text),
concluido: false,
id: itens![item.key]!.id),
SelectCor(
cor: listaController.listaCor,
corAtual: lista?.cor ?? ''),
ListView.builder(
primary: false,
shrinkWrap: true,
itemCount: itens!.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(bottom: 16),
child: input(
//onSaved: listaController.listaNome,
textController: _controllers[index],
label: 'item',
excluir: true,
funcao: () async {
if (itens![index]!.id != null) {
print('entrei no if');
print(itens[index]!.id);
itemController.itemId(itens[index]!.id);
await itemController.excluirItem();
}
setState(() {
itens!.remove(itens[index]);
_controllers
.remove(_controllers[index]);
});
},
),
);
}).toList();
}),
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Botao(
texto: 'Novo item',
cor: const Color(0xFF6BC8E4),
clicar: () async {
setState(() {
itens!.add(ItemModel());
_controllers.add(new TextEditingController());
});
},
),
),
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Botao(
texto: 'Salvar',
cor: const Color(0xFF6385C3),
clicar: () async {
List<ItemModel?>? listaitens = [];

listaController.listaItens(listaitens);
bool succes = await listaController.saveLista();
_controllers.asMap().entries.map((item) {
listaitens.add(
ItemModel(
nome: (item.value.text),
concluido: false,
id: itens![item.key]!.id),
);
}).toList();

if (succes == true) {
var nav = Navigator.of(context);
nav.pop();
listaController.listaItens(listaitens);
bool succes = await listaController.saveLista();

if (lista?.id != null) {
//nav.pop();
if (succes == true) {
var nav = Navigator.of(context);
nav.pop();

if (lista?.id != null) {
//nav.pop();
nav.pop();
}
fecharDialog!();
}
fecharDialog!();
}
},
},
),
),
),
],
],
),
),
),
),
Expand Down
14 changes: 11 additions & 3 deletions lib/components/dialogs/listas/listaDialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Future<dynamic> visualizarLista(BuildContext context,

listaController.listaId(lista.id);

var _pageSize = MediaQuery.of(context).size.height;
var _notifySize = MediaQuery.of(context).padding.top;
//var _appBarSize = appBar.preferredSize.height;

return showDialog(
barrierDismissible: false,
barrierColor: Colors.white.withOpacity(0),
Expand All @@ -33,15 +37,19 @@ Future<dynamic> visualizarLista(BuildContext context,
child: Form(
key: listaController.formKey,
child: Container(
margin: const EdgeInsets.only(top: 24),
height: lista.itens!.length < 3
? MediaQuery.of(context).size.height
: null,
// height: MediaQuery.of(context).size.height *
// lista.itens!.length,
margin: const EdgeInsets.only(top: 24),

child: DialogPersonalizado(
nome: lista.nome ?? '',
cor: lista.cor ?? '',
child: <Widget>[
ListView.builder(
//primary: false,
primary: false,
shrinkWrap: true,
itemCount: lista.itens!.length,
itemBuilder: (context, index) {
Expand All @@ -56,7 +64,7 @@ Future<dynamic> visualizarLista(BuildContext context,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 1.0),
padding: const EdgeInsets.only(bottom: 4.0),
child: Checkbox(
//tristate: true,
activeColor: Color(
Expand Down
2 changes: 1 addition & 1 deletion lib/listas_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class _HomePageState extends State<ListasPage> {
);
}

if (snapshot.data.length == 0) {
if (snapshot.data['listas'].length == 0) {
return const Center(
child:
Text("Você não tem nada pra fazer hoje!"),
Expand Down

0 comments on commit b0e1bcb

Please sign in to comment.