forked from alura-cursos/alura_flutter_client_control
/
clients_page.dart
149 lines (142 loc) · 5.14 KB
/
clients_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import 'package:client_control/models/client_type.dart';
import 'package:client_control/models/clients.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../components/hamburger_menu.dart';
import '../models/client.dart';
import '../models/types.dart';
class ClientsPage extends StatefulWidget {
const ClientsPage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<ClientsPage> createState() => _ClientsPageState();
}
class _ClientsPageState extends State<ClientsPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
drawer: const HamburgerMenu(),
body: Consumer<Clients>(
builder: (BuildContext context, Clients list, Widget? widget) {
return ListView.builder(
itemCount: list.clients.length,
itemBuilder: (context, index) {
return Dismissible(
key: UniqueKey(),
background: Container(color: Colors.red),
child: ListTile(
leading: Icon(list.clients[index].type.icon),
title: Text(list.clients[index].name +
' (' +
list.clients[index].type.name +
')'),
iconColor: Colors.indigo,
),
onDismissed: (direction) {
setState(() {
list.clients.removeAt(index);
});
},
);
},
);
},
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.indigo,
onPressed: () {
createType(context);
},
tooltip: 'Add Tipo',
child: const Icon(Icons.add),
),
);
}
void createType(context) {
TextEditingController nomeInput = TextEditingController();
TextEditingController emailInput = TextEditingController();
Types listTypes = Provider.of<Types>(context, listen: false);
ClientType dropdownValue = listTypes.types[0];
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
scrollable: true,
title: const Text('Cadastrar cliente'),
content: Padding(
padding: const EdgeInsets.all(8.0),
child: Form(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
controller: nomeInput,
decoration: const InputDecoration(
labelText: 'Nome',
icon: Icon(Icons.account_box),
),
),
const Padding(padding: EdgeInsets.all(5)),
TextFormField(
controller: emailInput,
decoration: const InputDecoration(
labelText: 'Email',
icon: Icon(Icons.email),
),
),
const Padding(padding: EdgeInsets.all(5)),
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return DropdownButton(
isExpanded: true,
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.indigo,
),
onChanged: (newValue) {
setState(() {
dropdownValue = newValue as ClientType;
});
},
items: listTypes.types.map((ClientType type) {
return DropdownMenuItem<ClientType>(
value: type,
child: Text(type.name),
);
}).toList(),
);
}),
],
),
),
),
actions: [
Consumer<Clients>(
builder: (BuildContext context, Clients list, Widget? widget) {
return TextButton(
child: const Text("Salvar"),
onPressed: () async {
list.addClients(Client(
name: nomeInput.text,
email: emailInput.text,
type: dropdownValue));
Navigator.pop(context);
});
},
),
TextButton(
child: const Text("Cancelar"),
onPressed: () {
Navigator.pop(context);
}),
],
);
});
}
}