-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.dart
169 lines (162 loc) · 6.33 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:find_dropdown/find_dropdown.dart';
import 'user_model.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var countriesKey = GlobalKey<FindDropdownState>();
var nameKey = GlobalKey<FindDropdownState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("FindDropdown Example")),
body: Padding(
padding: const EdgeInsets.all(25),
child: Column(
children: <Widget>[
FindDropdown(
items: ["Brasil", "Itália", "Estados Unidos", "Canadá"],
label: "País",
onChanged: (item) {
print(item);
countriesKey.currentState?.setSelectedItem(<String>[]);
},
selectedItem: "Brasil",
showSearchBox: false,
labelStyle: TextStyle(color: Colors.blue),
backgroundColor: Colors.redAccent,
titleStyle: TextStyle(color: Colors.greenAccent),
validate: (String? item) {
if (item == null)
return "Required field";
else if (item == "Brasil")
return "Invalid item";
else
return null;
},
),
FindDropdown<String>.multiSelect(
key: countriesKey,
items: ["Brasil", "Itália", "Estados Unidos", "Canadá"],
label: "Países",
selectedItems: ["Brasil"],
onChanged: (selectedItems) => print("countries: $selectedItems"),
showSearchBox: false,
labelStyle: TextStyle(color: Colors.blue),
titleStyle: TextStyle(color: Colors.green),
dropdownItemBuilder: (context, item, isSelected) {
return ListTile(
title: Text(item.toString()),
trailing: isSelected ? Icon(Icons.check) : null,
selected: isSelected,
);
},
okButtonBuilder: (context, onPressed) {
return Align(
alignment: Alignment.centerRight,
child: FloatingActionButton(child: Icon(Icons.check), onPressed: onPressed, mini: true),
);
},
validate: (List<String>? items) {
print("VALIDATION: $items");
String? response;
if (items == null || items.isEmpty) {
response = "Required field";
} else if (items.contains("Brasil")) {
response = "'Brasil' não pode ser selecionado.";
}
print(response);
return response;
},
),
FindDropdown<UserModel>(
key: nameKey,
label: "Nome",
onFind: (String filter) => getData(filter),
searchBoxDecoration: InputDecoration(hintText: "Search", border: OutlineInputBorder()),
onChanged: (UserModel? data) => print(data),
),
SizedBox(height: 25),
FindDropdown<UserModel>(
label: "Personagem",
onFind: (String filter) => getData(filter),
onChanged: (UserModel? data) => print(data),
dropdownBuilder: (BuildContext context, UserModel? item) {
return Container(
decoration: BoxDecoration(
border: Border.all(color: Theme.of(context).dividerColor),
borderRadius: BorderRadius.circular(5),
color: Colors.white,
),
child: (item?.avatar == null)
? ListTile(leading: CircleAvatar(), title: Text("No item selected"))
: ListTile(
leading: CircleAvatar(backgroundImage: NetworkImage(item!.avatar)),
title: Text(item.name),
subtitle: Text(item.createdAt.toString()),
),
);
},
dropdownItemBuilder: (BuildContext context, UserModel item, bool isSelected) {
return Container(
decoration: !isSelected
? null
: BoxDecoration(
border: Border.all(color: Theme.of(context).primaryColor),
borderRadius: BorderRadius.circular(5),
color: Colors.white,
),
child: ListTile(
selected: isSelected,
title: Text(item.name),
subtitle: Text(item.createdAt.toString()),
leading: CircleAvatar(backgroundImage: NetworkImage(item.avatar)),
),
);
},
),
SizedBox(height: 25),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
child: Text('Limpar Países'),
color: Theme.of(context).primaryColor,
textColor: Theme.of(context).primaryIconTheme.color,
onPressed: () => countriesKey.currentState?.clear()),
SizedBox(width: 25),
RaisedButton(
child: Text('Limpar Nome'),
color: Theme.of(context).primaryColor,
textColor: Theme.of(context).primaryIconTheme.color,
onPressed: () => nameKey.currentState?.clear()),
],
),
],
),
),
);
}
Future<List<UserModel>> getData(filter) async {
var response = await Dio().get(
"http://5d85ccfb1e61af001471bf60.mockapi.io/user",
queryParameters: {"filter": filter},
);
var models = UserModel.fromJsonList(response.data);
return models;
}
}