-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomDropDown.dart
172 lines (161 loc) · 4.58 KB
/
customDropDown.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
import 'package:flutter/material.dart';
import 'package:uiHomeCraft/constants.dart';
import 'package:uiHomeCraft/dimensions.dart';
import 'dart:math' as math;
class CustomDropDown extends StatefulWidget {
final List<String> dropDownItems;
final Function(String) getSelected;
final String startingItem;
CustomDropDown({this.dropDownItems, this.getSelected, this.startingItem});
@override
_CustomDropDownState createState() => _CustomDropDownState();
}
class _CustomDropDownState extends State<CustomDropDown>
with SingleTickerProviderStateMixin {
GlobalKey key = LabeledGlobalKey("container");
Offset offset;
Size buttonSize;
Offset buttonPosition;
OverlayEntry overlayEntry;
bool isOpen = false;
AnimationController animationController;
List<OverlayEntry> overlayList = [];
int heightManager = 0;
String selectedItem;
OverlayEntry overlayEntryBuilder(String label) {
return OverlayEntry(
builder: (context) {
heightManager++;
return Positioned(
left: buttonPosition.dx,
top: buttonPosition.dy + buttonSize.height * heightManager,
child: DropDownItems(
label: label,
getLabel: (val) {
closeMenu();
selectedItem = val;
widget.getSelected(val);
setState(() {});
},
),
);
},
);
}
findButton() {
RenderBox renderBox = key.currentContext.findRenderObject();
buttonSize = renderBox.size;
buttonPosition = renderBox.localToGlobal(Offset.zero);
}
openMenu() {
for (var item in widget.dropDownItems) {
if (item != selectedItem) overlayList.add(overlayEntryBuilder(item));
}
findButton();
// overlayList.remove(selectedItem);
Overlay.of(context).insertAll(overlayList);
isOpen = true;
}
closeMenu() {
animationController.reverse();
overlayList.forEach((element) {
element.remove();
});
overlayList.clear();
setState(() {});
heightManager = 0;
isOpen = false;
}
@override
void initState() {
selectedItem = widget.startingItem;
animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 200),
lowerBound: 0,
upperBound: math.pi / 2);
super.initState();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
if (isOpen) {
animationController.reverse();
closeMenu();
} else {
animationController.forward();
openMenu();
}
},
child: Stack(
alignment: Alignment(1, 0),
children: [
DropDownItems(
isAbsorbing: true,
key: key,
label: selectedItem,
),
AnimatedBuilder(
child: Icon(
Icons.keyboard_arrow_down,
size: Dimensions.boxHeight * 4,
),
animation: animationController,
builder: (context, child) {
return Transform.rotate(
angle: -animationController.value,
child: child,
);
},
)
],
),
);
}
}
class DropDownItems extends StatelessWidget {
final bool isAbsorbing;
final String label;
final Function(String) getLabel;
const DropDownItems({
this.isAbsorbing = false,
this.getLabel,
this.label,
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return AbsorbPointer(
absorbing: isAbsorbing,
child: GestureDetector(
onTap: () {
return getLabel(label);
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 1),
child: Material(
borderRadius: BorderRadius.circular(100),
color: Colors.white,
child: Container(
alignment: Alignment(-0.7, 0),
width: Dimensions.boxWidth * 35,
height: Dimensions.boxHeight * 3.2,
decoration: BoxDecoration(
color: Colors.black12,
borderRadius: BorderRadius.circular(100),
),
child: Text(
label,
style: TextStyle(
fontSize: Dimensions.boxHeight * 2.2,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
);
}
}