-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathfuture_example.dart
More file actions
240 lines (227 loc) · 7.9 KB
/
future_example.dart
File metadata and controls
240 lines (227 loc) · 7.9 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import 'package:flutter/material.dart';
import 'package:multi_dropdown/multi_dropdown.dart';
/// Async data loading using [MultiDropdown.future].
///
/// Simulates fetching users from an API with a network delay,
/// showing the built-in loading spinner.
class FutureExample extends StatefulWidget {
const FutureExample({super.key});
@override
State<FutureExample> createState() => _FutureExampleState();
}
class _FutureExampleState extends State<FutureExample> {
List<String> _selected = [];
/// Simulates an API call that returns a list of users.
Future<List<DropdownItem<String>>> _fetchUsers() async {
// Simulate network delay
await Future<void>.delayed(const Duration(seconds: 2));
return [
DropdownItem(label: 'Alice Johnson', value: 'alice'),
DropdownItem(label: 'Bob Martinez', value: 'bob'),
DropdownItem(label: 'Carol Chen', value: 'carol'),
DropdownItem(label: 'Daniel Kim', value: 'daniel'),
DropdownItem(label: 'Emma Wilson', value: 'emma'),
DropdownItem(label: 'Frank Lee', value: 'frank'),
DropdownItem(label: 'Grace Patel', value: 'grace'),
DropdownItem(label: 'Henry Taylor', value: 'henry'),
DropdownItem(label: 'Iris Thompson', value: 'iris'),
DropdownItem(label: 'Jack Brown', value: 'jack'),
];
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colorScheme = theme.colorScheme;
return Scaffold(
appBar: AppBar(
title: const Text('Async Loading'),
),
body: ListView(
padding: const EdgeInsets.all(20),
children: [
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.cyan.withAlpha(40),
Colors.blue.withAlpha(30),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(16),
),
child: Row(
children: [
const Icon(Icons.cloud_download_rounded, color: Colors.cyan),
const SizedBox(width: 12),
Expanded(
child: Text(
'Items are loaded asynchronously using '
'MultiDropdown.future(). '
'Notice the loading spinner during the 2-second delay.',
style: theme.textTheme.bodyMedium?.copyWith(
color: colorScheme.onSurface,
),
),
),
],
),
),
const SizedBox(height: 24),
// Async dropdown
MultiDropdown<String>.future(
future: _fetchUsers,
chipDecoration: ChipDecoration(
backgroundColor: Colors.cyan.shade50,
labelStyle: TextStyle(
color: Colors.cyan.shade800,
fontSize: 13,
),
borderRadius: BorderRadius.circular(20),
deleteIcon: Icon(
Icons.close_rounded,
size: 14,
color: Colors.cyan.shade400,
),
wrap: true,
spacing: 8,
runSpacing: 8,
),
fieldDecoration: FieldDecoration(
hintText: 'Select users',
prefixIcon: Icon(
Icons.people_outline_rounded,
color: Colors.cyan.shade400,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(14),
borderSide: BorderSide(color: Colors.cyan.shade200),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(14),
borderSide: BorderSide(
color: Colors.cyan.shade400,
width: 2,
),
),
),
dropdownDecoration: DropdownDecoration(
backgroundColor: colorScheme.surfaceContainerLow,
maxHeight: 300,
),
onSelectionChange: (values) {
setState(() => _selected = values);
},
),
const SizedBox(height: 24),
// How it works
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: colorScheme.surfaceContainerHighest.withAlpha(80),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: colorScheme.outlineVariant),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'How it works',
style: theme.textTheme.titleSmall?.copyWith(
fontWeight: FontWeight.w600,
color: colorScheme.primary,
),
),
const SizedBox(height: 8),
_InfoRow(
icon: Icons.code_rounded,
label: 'Uses MultiDropdown.future()',
colorScheme: colorScheme,
),
_InfoRow(
icon: Icons.timer_rounded,
label: '2-second simulated API delay',
colorScheme: colorScheme,
),
_InfoRow(
icon: Icons.refresh_rounded,
label: 'Shows loading spinner automatically',
colorScheme: colorScheme,
),
],
),
),
const SizedBox(height: 24),
// Selected values
AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: _selected.isNotEmpty
? Container(
key: ValueKey(_selected.length),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.cyan.shade50.withAlpha(60),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: Colors.cyan.shade200,
),
),
child: Row(
children: [
Icon(
Icons.check_circle_outline,
size: 18,
color: Colors.cyan.shade600,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Selected: ${_selected.join(", ")}',
style: theme.textTheme.bodyMedium?.copyWith(
fontFamily: 'monospace',
color: Colors.cyan.shade800,
),
),
),
],
),
)
: const SizedBox.shrink(),
),
],
),
);
}
}
class _InfoRow extends StatelessWidget {
const _InfoRow({
required this.icon,
required this.label,
required this.colorScheme,
});
final IconData icon;
final String label;
final ColorScheme colorScheme;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
Icon(icon, size: 16, color: colorScheme.onSurfaceVariant),
const SizedBox(width: 10),
Expanded(
child: Text(
label,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: colorScheme.onSurfaceVariant,
),
),
),
],
),
);
}
}