parent
e3bb1030b2
commit
853aea3820
5 changed files with 220 additions and 123 deletions
@ -0,0 +1,80 @@ |
|||||||
|
import 'package:flutter/foundation.dart'; |
||||||
|
import 'package:flutter/material.dart'; |
||||||
|
import 'package:image_picker/image_picker.dart'; |
||||||
|
|
||||||
|
import '../controller.dart'; |
||||||
|
import '../toolbar.dart'; |
||||||
|
import 'image_video_utils.dart'; |
||||||
|
import 'quill_icon_button.dart'; |
||||||
|
|
||||||
|
class CameraButton extends StatelessWidget { |
||||||
|
const CameraButton({ |
||||||
|
required this.icon, |
||||||
|
required this.controller, |
||||||
|
this.iconSize = kDefaultIconSize, |
||||||
|
this.fillColor, |
||||||
|
this.onImagePickCallback, |
||||||
|
this.onVideoPickCallback, |
||||||
|
this.filePickImpl, |
||||||
|
this.webImagePickImpl, |
||||||
|
this.webVideoPickImpl, |
||||||
|
Key? key, |
||||||
|
}) : super(key: key); |
||||||
|
|
||||||
|
final IconData icon; |
||||||
|
final double iconSize; |
||||||
|
|
||||||
|
final Color? fillColor; |
||||||
|
|
||||||
|
final QuillController controller; |
||||||
|
|
||||||
|
final OnImagePickCallback? onImagePickCallback; |
||||||
|
|
||||||
|
final OnVideoPickCallback? onVideoPickCallback; |
||||||
|
|
||||||
|
final WebImagePickImpl? webImagePickImpl; |
||||||
|
|
||||||
|
final WebVideoPickImpl? webVideoPickImpl; |
||||||
|
|
||||||
|
final FilePickImpl? filePickImpl; |
||||||
|
|
||||||
|
@override |
||||||
|
Widget build(BuildContext context) { |
||||||
|
final theme = Theme.of(context); |
||||||
|
|
||||||
|
return QuillIconButton( |
||||||
|
icon: Icon(icon, size: iconSize, color: theme.iconTheme.color), |
||||||
|
highlightElevation: 0, |
||||||
|
hoverElevation: 0, |
||||||
|
size: iconSize * 1.77, |
||||||
|
fillColor: fillColor ?? theme.canvasColor, |
||||||
|
onPressed: () => _handleCameraButtonTap(context, controller, |
||||||
|
onImagePickCallback: onImagePickCallback, |
||||||
|
onVideoPickCallback: onVideoPickCallback, |
||||||
|
filePickImpl: filePickImpl, |
||||||
|
webImagePickImpl: webImagePickImpl), |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
Future<void> _handleCameraButtonTap( |
||||||
|
BuildContext context, QuillController controller, |
||||||
|
{OnImagePickCallback? onImagePickCallback, |
||||||
|
OnVideoPickCallback? onVideoPickCallback, |
||||||
|
FilePickImpl? filePickImpl, |
||||||
|
WebImagePickImpl? webImagePickImpl}) async { |
||||||
|
if (onImagePickCallback != null && onVideoPickCallback != null) { |
||||||
|
// TODO: show dialog to choose Image or Video |
||||||
|
} |
||||||
|
|
||||||
|
if (onImagePickCallback != null) { |
||||||
|
return ImageVideoUtils.handleImageButtonTap( |
||||||
|
context, controller, ImageSource.camera, onImagePickCallback, |
||||||
|
filePickImpl: filePickImpl, webImagePickImpl: webImagePickImpl); |
||||||
|
} |
||||||
|
|
||||||
|
assert(onVideoPickCallback != null, 'onVideoPickCallback must not be null'); |
||||||
|
return ImageVideoUtils.handleVideoButtonTap( |
||||||
|
context, controller, ImageSource.camera, onVideoPickCallback!, |
||||||
|
filePickImpl: filePickImpl, webVideoPickImpl: webVideoPickImpl); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,115 @@ |
|||||||
|
import 'dart:io'; |
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart'; |
||||||
|
import 'package:flutter/material.dart'; |
||||||
|
import 'package:image_picker/image_picker.dart'; |
||||||
|
|
||||||
|
import '../../models/documents/nodes/embed.dart'; |
||||||
|
import '../controller.dart'; |
||||||
|
import '../toolbar.dart'; |
||||||
|
|
||||||
|
class ImageVideoUtils { |
||||||
|
/// For image picking logic |
||||||
|
static Future<void> handleImageButtonTap( |
||||||
|
BuildContext context, |
||||||
|
QuillController controller, |
||||||
|
ImageSource imageSource, |
||||||
|
OnImagePickCallback onImagePickCallback, |
||||||
|
{FilePickImpl? filePickImpl, |
||||||
|
WebImagePickImpl? webImagePickImpl}) async { |
||||||
|
final index = controller.selection.baseOffset; |
||||||
|
final length = controller.selection.extentOffset - index; |
||||||
|
|
||||||
|
String? imageUrl; |
||||||
|
if (kIsWeb) { |
||||||
|
assert( |
||||||
|
webImagePickImpl != null, |
||||||
|
'Please provide webImagePickImpl for Web ' |
||||||
|
'(check out example directory for how to do it)'); |
||||||
|
imageUrl = await webImagePickImpl!(onImagePickCallback); |
||||||
|
} else if (Platform.isAndroid || Platform.isIOS) { |
||||||
|
imageUrl = await _pickImage(imageSource, onImagePickCallback); |
||||||
|
} else { |
||||||
|
assert(filePickImpl != null, 'Desktop must provide filePickImpl'); |
||||||
|
imageUrl = |
||||||
|
await _pickImageDesktop(context, filePickImpl!, onImagePickCallback); |
||||||
|
} |
||||||
|
|
||||||
|
if (imageUrl != null) { |
||||||
|
controller.replaceText(index, length, BlockEmbed.image(imageUrl), null); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static Future<String?> _pickImage( |
||||||
|
ImageSource source, OnImagePickCallback onImagePickCallback) async { |
||||||
|
final pickedFile = await ImagePicker().pickImage(source: source); |
||||||
|
if (pickedFile == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
return onImagePickCallback(File(pickedFile.path)); |
||||||
|
} |
||||||
|
|
||||||
|
static Future<String?> _pickImageDesktop( |
||||||
|
BuildContext context, |
||||||
|
FilePickImpl filePickImpl, |
||||||
|
OnImagePickCallback onImagePickCallback) async { |
||||||
|
final filePath = await filePickImpl(context); |
||||||
|
if (filePath == null || filePath.isEmpty) return null; |
||||||
|
|
||||||
|
final file = File(filePath); |
||||||
|
return onImagePickCallback(file); |
||||||
|
} |
||||||
|
|
||||||
|
/// For video picking logic |
||||||
|
static Future<void> handleVideoButtonTap( |
||||||
|
BuildContext context, |
||||||
|
QuillController controller, |
||||||
|
ImageSource videoSource, |
||||||
|
OnVideoPickCallback onVideoPickCallback, |
||||||
|
{FilePickImpl? filePickImpl, |
||||||
|
WebVideoPickImpl? webVideoPickImpl}) async { |
||||||
|
final index = controller.selection.baseOffset; |
||||||
|
final length = controller.selection.extentOffset - index; |
||||||
|
|
||||||
|
String? videoUrl; |
||||||
|
if (kIsWeb) { |
||||||
|
assert( |
||||||
|
webVideoPickImpl != null, |
||||||
|
'Please provide webVideoPickImpl for Web ' |
||||||
|
'(check out example directory for how to do it)'); |
||||||
|
videoUrl = await webVideoPickImpl!(onVideoPickCallback); |
||||||
|
} else if (Platform.isAndroid || Platform.isIOS) { |
||||||
|
videoUrl = await _pickVideo(ImageSource.gallery, onVideoPickCallback); |
||||||
|
} else { |
||||||
|
assert(filePickImpl != null, 'Desktop must provide filePickImpl'); |
||||||
|
videoUrl = |
||||||
|
await _pickVideoDesktop(context, filePickImpl!, onVideoPickCallback); |
||||||
|
} |
||||||
|
|
||||||
|
if (videoUrl != null) { |
||||||
|
controller.replaceText(index, length, BlockEmbed.video(videoUrl), null); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static Future<String?> _pickVideo( |
||||||
|
ImageSource source, OnVideoPickCallback onVideoPickCallback) async { |
||||||
|
final pickedFile = await ImagePicker().pickVideo(source: source); |
||||||
|
if (pickedFile == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
return onVideoPickCallback(File(pickedFile.path)); |
||||||
|
} |
||||||
|
|
||||||
|
static Future<String?> _pickVideoDesktop( |
||||||
|
BuildContext context, |
||||||
|
FilePickImpl filePickImpl, |
||||||
|
OnVideoPickCallback onVideoPickCallback) async { |
||||||
|
final filePath = await filePickImpl(context); |
||||||
|
if (filePath == null || filePath.isEmpty) return null; |
||||||
|
|
||||||
|
final file = File(filePath); |
||||||
|
return onVideoPickCallback(file); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue