-
Notifications
You must be signed in to change notification settings - Fork 805
/
media_button.dart
452 lines (397 loc) · 13.3 KB
/
media_button.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//import 'dart:io';
import 'dart:math' as math;
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_quill/extensions.dart';
import 'package:flutter_quill/flutter_quill.dart' hide Text;
import 'package:flutter_quill/translations.dart';
import 'package:image_picker/image_picker.dart';
import '../embed_types.dart';
/// Widget which combines [ImageButton] and [VideButton] widgets. This widget
/// has more customization and uses dialog similar to one which is used
/// on [http://quilljs.com].
class MediaButton extends StatelessWidget {
const MediaButton({
required this.controller,
required this.icon,
this.type = QuillMediaType.image,
this.iconSize = kDefaultIconSize,
this.fillColor,
this.mediaFilePicker = _defaultMediaPicker,
this.onMediaPickedCallback,
this.iconTheme,
this.dialogTheme,
this.tooltip,
this.childrenSpacing = 16.0,
this.labelText,
this.hintText,
this.submitButtonText,
this.submitButtonSize,
this.galleryButtonText,
this.linkButtonText,
this.autovalidateMode = AutovalidateMode.disabled,
Key? key,
this.validationMessage,
}) : assert(type == QuillMediaType.image,
'Video selection is not supported yet'),
super(key: key);
final QuillController controller;
final IconData icon;
final double iconSize;
final Color? fillColor;
final QuillMediaType type;
final QuillIconTheme? iconTheme;
final QuillDialogTheme? dialogTheme;
final String? tooltip;
final MediaFilePicker mediaFilePicker;
final MediaPickedCallback? onMediaPickedCallback;
/// The margin between child widgets in the dialog.
final double childrenSpacing;
/// The text of label in link add mode.
final String? labelText;
/// The hint text for link [TextField].
final String? hintText;
/// The text of the submit button.
final String? submitButtonText;
/// The size of dialog buttons.
final Size? submitButtonSize;
/// The text of the gallery button [MediaSourceSelectorDialog].
final String? galleryButtonText;
/// The text of the link button [MediaSourceSelectorDialog].
final String? linkButtonText;
final AutovalidateMode autovalidateMode;
final String? validationMessage;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final iconColor = iconTheme?.iconUnselectedColor ?? theme.iconTheme.color;
final iconFillColor =
iconTheme?.iconUnselectedFillColor ?? fillColor ?? theme.canvasColor;
return QuillIconButton(
icon: Icon(icon, size: iconSize, color: iconColor),
tooltip: tooltip,
highlightElevation: 0,
hoverElevation: 0,
size: iconSize * 1.77,
fillColor: iconFillColor,
borderRadius: iconTheme?.borderRadius ?? 2,
onPressed: () => _onPressedHandler(context),
);
}
Future<void> _onPressedHandler(BuildContext context) async {
if (onMediaPickedCallback != null) {
final mediaSource = await showDialog<MediaPickSetting>(
context: context,
builder: (_) => MediaSourceSelectorDialog(
dialogTheme: dialogTheme,
galleryButtonText: galleryButtonText,
linkButtonText: linkButtonText,
),
);
if (mediaSource != null) {
if (mediaSource == MediaPickSetting.Gallery) {
await _pickImage();
} else {
_inputLink(context);
}
}
} else {
_inputLink(context);
}
}
Future<void> _pickImage() async {
if (!(kIsWeb || isMobile() || isDesktop())) {
throw UnsupportedError(
'Unsupported target platform: ${defaultTargetPlatform.name}');
}
final mediaFileUrl = await _pickMediaFileUrl();
if (mediaFileUrl != null) {
final index = controller.selection.baseOffset;
final length = controller.selection.extentOffset - index;
controller.replaceText(
index, length, BlockEmbed.image(mediaFileUrl), null);
}
}
Future<MediaFileUrl?> _pickMediaFileUrl() async {
final mediaFile = await mediaFilePicker(type);
return mediaFile != null ? onMediaPickedCallback?.call(mediaFile) : null;
}
void _inputLink(BuildContext context) {
showDialog<String>(
context: context,
builder: (_) => MediaLinkDialog(
dialogTheme: dialogTheme,
labelText: labelText,
hintText: hintText,
buttonText: submitButtonText,
buttonSize: submitButtonSize,
childrenSpacing: childrenSpacing,
autovalidateMode: autovalidateMode,
validationMessage: validationMessage,
),
).then(_linkSubmitted);
}
void _linkSubmitted(String? value) {
if (value != null && value.isNotEmpty) {
final index = controller.selection.baseOffset;
final length = controller.selection.extentOffset - index;
final data =
type.isImage ? BlockEmbed.image(value) : BlockEmbed.video(value);
controller.replaceText(index, length, data, null);
}
}
}
/// Provides a dialog for input link to media resource.
class MediaLinkDialog extends StatefulWidget {
const MediaLinkDialog({
Key? key,
this.link,
this.dialogTheme,
this.childrenSpacing = 16.0,
this.labelText,
this.hintText,
this.buttonText,
this.buttonSize,
this.autovalidateMode = AutovalidateMode.disabled,
this.validationMessage,
}) : assert(childrenSpacing > 0),
super(key: key);
final String? link;
final QuillDialogTheme? dialogTheme;
/// The margin between child widgets in the dialog.
final double childrenSpacing;
/// The text of label in link add mode.
final String? labelText;
/// The hint text for link [TextField].
final String? hintText;
/// The text of the submit button.
final String? buttonText;
/// The size of dialog buttons.
final Size? buttonSize;
final AutovalidateMode autovalidateMode;
final String? validationMessage;
@override
State<MediaLinkDialog> createState() => _MediaLinkDialogState();
}
class _MediaLinkDialogState extends State<MediaLinkDialog> {
final _linkFocus = FocusNode();
final _linkController = TextEditingController();
@override
void dispose() {
_linkFocus.dispose();
_linkController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final constraints = widget.dialogTheme?.linkDialogConstraints ??
() {
final mediaQuery = MediaQuery.of(context);
final maxWidth =
kIsWeb ? mediaQuery.size.width / 4 : mediaQuery.size.width - 80;
return BoxConstraints(maxWidth: maxWidth, maxHeight: 80);
}();
final buttonStyle = widget.buttonSize != null
? Theme.of(context)
.elevatedButtonTheme
.style
?.copyWith(fixedSize: MaterialStatePropertyAll(widget.buttonSize))
: widget.dialogTheme?.buttonStyle;
final isWrappable = widget.dialogTheme?.isWrappable ?? false;
final children = [
Text(widget.labelText ?? 'Enter media'.i18n),
UtilityWidgets.maybeWidget(
enabled: !isWrappable,
wrapper: (child) => Expanded(
child: child,
),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: widget.childrenSpacing),
child: TextFormField(
controller: _linkController,
focusNode: _linkFocus,
style: widget.dialogTheme?.inputTextStyle,
keyboardType: TextInputType.url,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
labelStyle: widget.dialogTheme?.labelTextStyle,
hintText: widget.hintText,
),
autofocus: true,
autovalidateMode: widget.autovalidateMode,
validator: _validateLink,
onChanged: _linkChanged,
),
),
),
ElevatedButton(
onPressed: _canPress() ? _submitLink : null,
style: buttonStyle,
child: Text(widget.buttonText ?? 'Ok'.i18n),
),
];
return Dialog(
backgroundColor: widget.dialogTheme?.dialogBackgroundColor,
shape: widget.dialogTheme?.shape ??
DialogTheme.of(context).shape ??
RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
child: ConstrainedBox(
constraints: constraints,
child: Padding(
padding:
widget.dialogTheme?.linkDialogPadding ?? const EdgeInsets.all(16),
child: isWrappable
? Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
runSpacing: widget.dialogTheme?.runSpacing ?? 0.0,
children: children,
)
: Row(
children: children,
),
),
),
);
}
bool _canPress() => _validateLink(_linkController.text) == null;
void _linkChanged(String value) {
setState(() {
_linkController.text = value;
});
}
void _submitLink() => Navigator.pop(context, _linkController.text);
String? _validateLink(String? value) {
if ((value?.isEmpty ?? false) ||
!AutoFormatMultipleLinksRule.linkRegExp.hasMatch(value!)) {
return widget.validationMessage ?? 'That is not a valid URL';
}
return null;
}
}
/// Media souce selector.
class MediaSourceSelectorDialog extends StatelessWidget {
const MediaSourceSelectorDialog({
Key? key,
this.dialogTheme,
this.galleryButtonText,
this.linkButtonText,
}) : super(key: key);
final QuillDialogTheme? dialogTheme;
/// The text of the gallery button [MediaSourceSelectorDialog].
final String? galleryButtonText;
/// The text of the link button [MediaSourceSelectorDialog].
final String? linkButtonText;
@override
Widget build(BuildContext context) {
final constraints = dialogTheme?.mediaSelectorDialogConstraints ??
() {
final mediaQuery = MediaQuery.of(context);
double maxWidth, maxHeight;
if (kIsWeb) {
maxWidth = mediaQuery.size.width / 7;
maxHeight = mediaQuery.size.height / 7;
} else {
maxWidth = mediaQuery.size.width - 80;
maxHeight = maxWidth / 2;
}
return BoxConstraints(maxWidth: maxWidth, maxHeight: maxHeight);
}();
final shape = dialogTheme?.shape ??
DialogTheme.of(context).shape ??
RoundedRectangleBorder(borderRadius: BorderRadius.circular(4));
return Dialog(
backgroundColor: dialogTheme?.dialogBackgroundColor,
shape: shape,
child: ConstrainedBox(
constraints: constraints,
child: Padding(
padding: dialogTheme?.mediaSelectorDialogPadding ??
const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: TextButtonWithIcon(
icon: Icons.collections,
label: galleryButtonText ?? 'Gallery'.i18n,
onPressed: () =>
Navigator.pop(context, MediaPickSetting.Gallery),
),
),
const SizedBox(width: 10),
Expanded(
child: TextButtonWithIcon(
icon: Icons.link,
label: linkButtonText ?? 'Link'.i18n,
onPressed: () =>
Navigator.pop(context, MediaPickSetting.Link),
),
)
],
),
),
),
);
}
}
class TextButtonWithIcon extends StatelessWidget {
const TextButtonWithIcon({
required this.label,
required this.icon,
required this.onPressed,
this.textStyle,
Key? key,
}) : super(key: key);
final String label;
final IconData icon;
final VoidCallback onPressed;
final TextStyle? textStyle;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scale = MediaQuery.maybeOf(context)?.textScaleFactor ?? 1;
final gap = scale <= 1 ? 8.0 : lerpDouble(8, 4, math.min(scale - 1, 1))!;
final buttonStyle = TextButtonTheme.of(context).style;
final shape = buttonStyle?.shape?.resolve({}) ??
const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(4)));
return Material(
shape: shape,
textStyle: textStyle ??
theme.textButtonTheme.style?.textStyle?.resolve({}) ??
theme.textTheme.labelLarge,
elevation: buttonStyle?.elevation?.resolve({}) ?? 0,
child: InkWell(
customBorder: shape,
onTap: onPressed,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(icon),
SizedBox(height: gap),
Flexible(child: Text(label)),
],
),
),
),
);
}
}
/// Default file picker.
Future<QuillFile?> _defaultMediaPicker(QuillMediaType mediaType) async {
final pickedFile = mediaType.isImage
? await ImagePicker().pickImage(source: ImageSource.gallery)
: await ImagePicker().pickVideo(source: ImageSource.gallery);
if (pickedFile != null) {
return QuillFile(
name: pickedFile.name,
path: pickedFile.path,
bytes: await pickedFile.readAsBytes(),
);
}
return null;
}