-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.dart
137 lines (125 loc) · 4.2 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
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_dropdown_plus/dropdown.dart';
import 'package:flutter_dropdown_plus/dropdown_item.dart';
import 'package:flutter_dropdown_plus/dropdown_textfield.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false, home: DropdownExample());
}
}
class DropdownExample extends StatefulWidget {
const DropdownExample({Key? key}) : super(key: key);
@override
State<DropdownExample> createState() => _DropdownExampleState();
}
class _DropdownExampleState extends State<DropdownExample> {
List<DropdownItem> _itemList = [];
String _singleSelectedId = "";
final List<String> _multiSelectedIds = [];
final TextEditingController _conDropdownTextField = TextEditingController();
@override
void initState() {
super.initState();
_generateItems();
}
_generateItems() {
List<DropdownItem> list = [];
for (int i = 1; i <= 3; i++) {
list.add(DropdownItem(
id: "$i",
value: "Item $i",
data: User(
userId: "$i",
userName:
"User $i") /* User class is another data class (use any datatype in data field )*/
));
}
setState(() {
_itemList = list;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("flutter_dropdown_plus"),
),
body: SafeArea(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Dropdown.singleSelection(
title: "Single Selection Dropdown",
labelText: "Single",
hintText: "Single Selection",
list: _itemList,
selectedId: _singleSelectedId,
isAddItem: true,
onTapAddItem: (searchValue) {
log(searchValue);
},
onSingleItemListener: (selectedItem) {
setState(() {
_singleSelectedId = selectedItem.id;
});
String itemId = selectedItem.id;
String itemName = selectedItem.value;
User user = selectedItem.data as User;
log("Item Id: $itemId -- Item Name: $itemName ## Other Details ## User Id: ${user.userId} -- User Name: ${user.userName}");
}),
const SizedBox(
height: 20,
),
Dropdown.multiSelection(
title: "Multi Selection Dropdown",
labelText: "Multi",
hintText: "Multi Selection",
list: _itemList,
selectedIds: _multiSelectedIds,
isAllSelection: true,
isAddItem: true,
onTapAddItem: (searchValue) {
log(searchValue);
},
onMultipleItemListener: (selectedItemList) {
for (DropdownItem selectedItem in selectedItemList) {
String itemId = selectedItem.id;
String itemName = selectedItem.value;
User user = selectedItem.data as User;
log("Item Id: $itemId -- Item Name: $itemName ## Other Details ## User Id: ${user.userId} -- User Name: ${user.userName}");
}
}),
const SizedBox(
height: 20,
),
DropdownTextField(
controller: _conDropdownTextField,
list: _itemList,
hintText: "Item search",
labelText: "Item search",
),
],
),
),
)),
);
}
}
class User {
String userId;
String userName;
User({this.userId = "0", this.userName = ""});
}