/
main.dart
210 lines (201 loc) · 7.08 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:select_dialog/select_dialog.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> {
String? ex1;
String? ex2;
UserModel? ex3;
UserModel? ex4;
List<String> ex5 = [];
String? ex6;
final ex6Controller = TextEditingController(text: "20");
final modelItems = List.generate(
50,
(index) => UserModel(
avatar: "https://i.imgur.com/lTy4hiN.jpg",
name: "Deivão $index",
id: "$index",
createdAt: DateTime.now(),
),
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Select Dialog Example")),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(25),
width: double.infinity,
child: Column(
children: <Widget>[
ElevatedButton(
child: Text(ex1 ?? "Simple Example"),
onPressed: () {
SelectDialog.showModal<String>(
context,
label: "Simple Example",
titleStyle: TextStyle(color: Colors.brown),
showSearchBox: false,
selectedValue: ex1,
backgroundColor: Colors.amber,
items: List.generate(50, (index) => "Item $index"),
onChange: (String selected) {
setState(() {
ex1 = selected;
});
},
);
},
),
ElevatedButton(
child: Text(ex2 ?? "Model Example"),
onPressed: () {
SelectDialog.showModal<UserModel>(
context,
alwaysShowScrollBar: true,
label: "Model Example",
searchBoxDecoration: InputDecoration(hintText: "Example Hint"),
items: modelItems,
onChange: (UserModel selected) {
setState(() {
ex2 = selected.name;
});
},
);
},
),
ElevatedButton(
child: Text(ex3?.name ?? "Item Builder Example"),
onPressed: () {
SelectDialog.showModal<UserModel>(
context,
label: "Item Builder Example",
items: modelItems,
selectedValue: ex3,
itemBuilder: (BuildContext context, UserModel item, bool isSelected) {
return Container(
decoration: !isSelected
? null
: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Colors.white,
border: Border.all(color: Theme.of(context).primaryColor),
),
child: ListTile(
leading: CircleAvatar(backgroundImage: item.avatar == null ? null : NetworkImage(item.avatar!)),
selected: isSelected,
title: Text(item.name),
subtitle: Text(item.createdAt.toString()),
),
);
},
onChange: (selected) {
setState(() {
ex3 = selected;
});
},
);
},
),
ElevatedButton(
child: Text(ex4?.name ?? "Online Example"),
onPressed: () {
SelectDialog.showModal<UserModel>(
context,
label: "Online Example",
selectedValue: ex4,
onFind: (String filter) => getData(filter),
onChange: (UserModel selected) {
setState(() {
ex4 = selected;
});
},
);
},
),
ElevatedButton(
child: Text(
ex5.isEmpty ? "Multiple Items Example" : ex5.join(", "),
),
onPressed: () {
SelectDialog.showModal<String>(
context,
label: "Multiple Items Example",
multipleSelectedValues: ex5,
items: List.generate(50, (index) => "Item $index"),
itemBuilder: (context, item, isSelected) {
return ListTile(
trailing: isSelected ? Icon(Icons.check) : null,
title: Text(item),
selected: isSelected,
);
},
onMultipleItemsChange: (List<String> selected) {
setState(() {
ex5 = selected;
});
},
okButtonBuilder: (context, onPressed) {
return Align(
alignment: Alignment.centerRight,
child: FloatingActionButton(
onPressed: onPressed,
child: Icon(Icons.check),
mini: true,
),
);
},
);
},
),
ElevatedButton(
child: Text(ex6 ?? "Find Controller Example"),
onPressed: () {
SelectDialog.showModal<UserModel>(
context,
findController: ex6Controller,
alwaysShowScrollBar: true,
label: "Scroll Controller Example",
searchBoxDecoration: InputDecoration(hintText: "Example Hint"),
items: modelItems,
onChange: (UserModel selected) {
setState(() {
ex6 = selected.name;
});
},
);
},
),
],
),
),
),
);
}
Future<List<UserModel>> getData(String filter) async {
var response = await Dio().get(
"http://5d85ccfb1e61af001471bf60.mockapi.io/user",
queryParameters: {
"filter": filter
},
);
var models = UserModel.fromJsonList(response.data);
return models;
}
}