-
Notifications
You must be signed in to change notification settings - Fork 5
/
custom_menu_page.dart
205 lines (186 loc) · 6.46 KB
/
custom_menu_page.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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'app_scaffold.dart';
class _TextSelectionToolbarItemData {
const _TextSelectionToolbarItemData({
this.label,
this.onPressed,
});
final String label;
final VoidCallback onPressed;
}
class CustomMenuPage extends StatefulWidget {
const CustomMenuPage({ Key key }) : super(key: key);
static const String routeName = '/custom-menu';
@override _CustomMenuPageState createState() => _CustomMenuPageState();
}
class _CustomMenuPageState extends State<CustomMenuPage> {
@override
Widget build(BuildContext context) {
return AppScaffold(
title: 'Custom Menu Page',
/*
child: TextField(
maxLines: 5,
selectionControls: MyMaterialTextSelectionControls(),
),
*/
child: SelectableText(
'Select me custom menu',
selectionControls: MyMaterialTextSelectionControls(),
),
);
}
}
class MyMaterialTextSelectionControls extends MaterialTextSelectionControls {
// Padding between the toolbar and the anchor.
static const double _kToolbarContentDistanceBelow = 20.0;
static const double _kToolbarContentDistance = 8.0;
/// Builder for material-style copy/paste text selection toolbar.
@override
Widget buildToolbar(
BuildContext context,
Rect globalEditableRegion,
double textLineHeight,
Offset selectionMidpoint,
List<TextSelectionPoint> endpoints,
TextSelectionDelegate delegate,
ClipboardStatusNotifier clipboardStatus,
Offset lastSecondaryTapDownPosition,
) {
final TextSelectionPoint startTextSelectionPoint = endpoints[0];
final TextSelectionPoint endTextSelectionPoint = endpoints.length > 1
? endpoints[1]
: endpoints[0];
final Offset anchorAbove = Offset(
globalEditableRegion.left + selectionMidpoint.dx,
globalEditableRegion.top + startTextSelectionPoint.point.dy - textLineHeight - _kToolbarContentDistance
);
final Offset anchorBelow = Offset(
globalEditableRegion.left + selectionMidpoint.dx,
globalEditableRegion.top + endTextSelectionPoint.point.dy + _kToolbarContentDistanceBelow,
);
return MyTextSelectionToolbar(
anchorAbove: anchorAbove,
anchorBelow: anchorBelow,
clipboardStatus: clipboardStatus,
handleCopy: canCopy(delegate) && handleCopy != null ? () => handleCopy(delegate, clipboardStatus) : null,
handleCustomButton: () {
delegate.textEditingValue = delegate.textEditingValue.copyWith(
selection: TextSelection.collapsed(
offset: delegate.textEditingValue.selection.baseOffset,
),
);
delegate.hideToolbar();
},
handleCut: canCut(delegate) && handleCut != null ? () => handleCut(delegate) : null,
handlePaste: canPaste(delegate) && handlePaste != null ? () => handlePaste(delegate) : null,
handleSelectAll: canSelectAll(delegate) && handleSelectAll != null ? () => handleSelectAll(delegate) : null,
);
}
}
class MyTextSelectionToolbar extends StatefulWidget {
const MyTextSelectionToolbar({
Key key,
this.anchorAbove,
this.anchorBelow,
this.clipboardStatus,
this.handleCopy,
this.handleCustomButton,
this.handleCut,
this.handlePaste,
this.handleSelectAll,
}) : super(key: key);
final Offset anchorAbove;
final Offset anchorBelow;
final ClipboardStatusNotifier clipboardStatus;
final VoidCallback handleCopy;
final VoidCallback handleCustomButton;
final VoidCallback handleCut;
final VoidCallback handlePaste;
final VoidCallback handleSelectAll;
@override
MyTextSelectionToolbarState createState() => MyTextSelectionToolbarState();
}
class MyTextSelectionToolbarState extends State<MyTextSelectionToolbar> {
void _onChangedClipboardStatus() {
setState(() {
// Inform the widget that the value of clipboardStatus has changed.
});
}
@override
void initState() {
super.initState();
widget.clipboardStatus.addListener(_onChangedClipboardStatus);
widget.clipboardStatus.update();
}
@override
void didUpdateWidget(MyTextSelectionToolbar oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.clipboardStatus != oldWidget.clipboardStatus) {
widget.clipboardStatus.addListener(_onChangedClipboardStatus);
oldWidget.clipboardStatus.removeListener(_onChangedClipboardStatus);
}
widget.clipboardStatus.update();
}
@override
void dispose() {
super.dispose();
if (!widget.clipboardStatus.disposed) {
widget.clipboardStatus.removeListener(_onChangedClipboardStatus);
}
}
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterialLocalizations(context));
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
final List<_TextSelectionToolbarItemData> itemDatas = <_TextSelectionToolbarItemData>[
if (widget.handleCut != null)
_TextSelectionToolbarItemData(
label: localizations.cutButtonLabel,
onPressed: widget.handleCut,
),
if (widget.handleCopy != null)
_TextSelectionToolbarItemData(
label: localizations.copyButtonLabel,
onPressed: widget.handleCopy,
),
if (widget.handlePaste != null
&& widget.clipboardStatus.value == ClipboardStatus.pasteable)
_TextSelectionToolbarItemData(
label: localizations.pasteButtonLabel,
onPressed: widget.handlePaste,
),
if (widget.handleSelectAll != null)
_TextSelectionToolbarItemData(
label: localizations.selectAllButtonLabel,
onPressed: widget.handleSelectAll,
),
_TextSelectionToolbarItemData(
onPressed: widget.handleCustomButton,
label: 'Custom button',
),
];
int childIndex = 0;
return TextSelectionToolbar(
anchorAbove: widget.anchorAbove,
anchorBelow: widget.anchorBelow,
toolbarBuilder: (BuildContext context, Widget child) {
return Container(
color: Colors.pink,
child: child,
);
},
children: itemDatas.map((_TextSelectionToolbarItemData itemData) {
return TextSelectionToolbarTextButton(
padding: TextSelectionToolbarTextButton.getPadding(childIndex++, itemDatas.length),
onPressed: itemData.onPressed,
child: Text(itemData.label),
);
}).toList(),
);
}
}