-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathpopup.dart
168 lines (151 loc) · 5.15 KB
/
popup.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
import 'package:flutter/material.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import '../../sizes.dart';
import 'popup_controller.dart';
/// Popup window displaying the list of possible completions
class Popup extends StatefulWidget {
final PopupController controller;
final Size editingWindowSize;
/// The window coordinates of the top-left corner of the editor widget.
final Offset? editorOffset;
/// The window coordinates of the highest allowed top-left corner
/// of the popup if shown above the caret.
///
/// Since the popup is pushed to the bottom of the allowed rectangle
/// the actual position may be lower.
final Offset flippedOffset;
/// The window coordinates of the top-left corner of the popup
/// if shown below the caret.
final Offset normalOffset;
final FocusNode parentFocusNode;
final TextStyle style;
final Color? backgroundColor;
const Popup({
super.key,
required this.controller,
required this.editingWindowSize,
required this.editorOffset,
required this.flippedOffset,
required this.normalOffset,
required this.parentFocusNode,
required this.style,
this.backgroundColor,
});
@override
PopupState createState() => PopupState();
}
class PopupState extends State<Popup> {
final pageStorageBucket = PageStorageBucket();
@override
void initState() {
widget.controller.reset();
widget.controller.addListener(rebuild);
super.initState();
}
@override
void dispose() {
widget.controller.removeListener(rebuild);
super.dispose();
}
@override
Widget build(BuildContext context) {
final verticalFlipRequired = _isVerticalFlipRequired();
final bool isHorizontalOverflowed = _isHorizontallyOverflowed();
final double leftOffsetLimit =
// TODO(nausharipov): find where 100 comes from
widget.editingWindowSize.width -
Sizes.autocompletePopupMaxWidth +
(widget.editorOffset?.dx ?? 0) -
100;
return PageStorage(
bucket: pageStorageBucket,
child: Positioned(
left: isHorizontalOverflowed ? leftOffsetLimit : widget.normalOffset.dx,
top: verticalFlipRequired
? widget.flippedOffset.dy
: widget.normalOffset.dy,
child: Container(
alignment: verticalFlipRequired
? Alignment.bottomCenter
: Alignment.topCenter,
constraints: const BoxConstraints(
maxHeight: Sizes.autocompletePopupMaxHeight,
maxWidth: Sizes.autocompletePopupMaxWidth,
),
// Container is used because the vertical borders
// in DecoratedBox are hidden under scroll.
// ignore: use_decorated_box
child: Container(
decoration: BoxDecoration(
color: widget.backgroundColor,
border: Border.all(
color: widget.style.color!,
width: 0.5,
),
),
child: ScrollablePositionedList.builder(
shrinkWrap: true,
physics: const ClampingScrollPhysics(),
itemScrollController: widget.controller.itemScrollController,
itemPositionsListener: widget.controller.itemPositionsListener,
itemCount: widget.controller.suggestions.length,
itemBuilder: (context, index) {
return _buildListItem(index);
},
),
),
),
),
);
}
bool _isVerticalFlipRequired() {
final isPopupShorterThanWindow =
Sizes.autocompletePopupMaxHeight < widget.editingWindowSize.height;
final isPopupOverflowingHeight = widget.normalOffset.dy +
Sizes.autocompletePopupMaxHeight -
(widget.editorOffset?.dy ?? 0) >
widget.editingWindowSize.height;
return isPopupOverflowingHeight && isPopupShorterThanWindow;
}
bool _isHorizontallyOverflowed() {
return widget.normalOffset.dx -
(widget.editorOffset?.dx ?? 0) +
Sizes.autocompletePopupMaxWidth >
widget.editingWindowSize.width;
}
Widget _buildListItem(int index) {
return Material(
color: Colors.grey.withOpacity(0.1),
child: InkWell(
onTap: () {
widget.controller.selectedIndex = index;
widget.parentFocusNode.requestFocus();
},
onDoubleTap: () {
widget.controller.selectedIndex = index;
widget.parentFocusNode.requestFocus();
widget.controller.onCompletionSelected();
},
hoverColor: Colors.grey.withOpacity(0.1),
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
child: ColoredBox(
color: widget.controller.selectedIndex == index
? Colors.blueAccent.withOpacity(0.5)
: Colors.transparent,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Text(
widget.controller.suggestions[index],
overflow: TextOverflow.ellipsis,
style: widget.style,
),
),
),
),
);
}
void rebuild() {
setState(() {});
}
}