Skip to content

Commit 32d14ab

Browse files
Add image picker config and fixes (#96)
1 parent 5a6f6ec commit 32d14ab

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

lib/src/models/send_message_configuration.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'package:audio_waveforms/audio_waveforms.dart';
2323
import 'package:chatview/src/values/enumaration.dart';
2424
import 'package:flutter/material.dart';
2525
import 'package:flutter/services.dart';
26+
import 'package:image_picker/image_picker.dart';
2627

2728
class SendMessageConfiguration {
2829
/// Used to give background color to text field.
@@ -49,6 +50,9 @@ class SendMessageConfiguration {
4950
/// Provides configuration of image picker functionality.
5051
final ImagePickerIconsConfiguration? imagePickerIconsConfig;
5152

53+
/// Provides configuration of image picker plugin.
54+
final ImagePickerConfiguration? imagePickerConfiguration;
55+
5256
/// Provides configuration of text field.
5357
final TextFieldConfiguration? textFieldConfig;
5458

@@ -65,6 +69,7 @@ class SendMessageConfiguration {
6569
this.textFieldConfig,
6670
this.textFieldBackgroundColor,
6771
this.imagePickerIconsConfig,
72+
this.imagePickerConfiguration,
6873
this.defaultSendButtonColor,
6974
this.sendButtonIcon,
7075
this.replyDialogColor,
@@ -161,6 +166,32 @@ class TextFieldConfiguration {
161166
});
162167
}
163168

169+
class ImagePickerConfiguration {
170+
/// Used to give max width of image.
171+
final double? maxWidth;
172+
173+
/// Used to give max height of image.
174+
final double? maxHeight;
175+
176+
/// Used to give image quality.
177+
final int? imageQuality;
178+
179+
/// Preferred camera device to pick image from.
180+
final CameraDevice? preferredCameraDevice;
181+
182+
/// Callback when image is picked from camera or gallery,
183+
/// we can perform our task on image like adding crop options and return new image path
184+
final Future<String?> Function(String? path)? onImagePicked;
185+
186+
const ImagePickerConfiguration({
187+
this.maxWidth,
188+
this.maxHeight,
189+
this.imageQuality,
190+
this.preferredCameraDevice,
191+
this.onImagePicked,
192+
});
193+
}
194+
164195
/// Styling configuration for recorder widget.
165196
class VoiceRecordingConfiguration {
166197
const VoiceRecordingConfiguration({

lib/src/widgets/chat_list_widget.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ChatListWidget extends StatefulWidget {
5050
this.replyPopupConfig,
5151
this.loadMoreData,
5252
this.isLastPage,
53+
this.onChatListTap,
5354
}) : super(key: key);
5455

5556
/// Provides controller for accessing few function for running chat.
@@ -104,6 +105,9 @@ class ChatListWidget extends StatefulWidget {
104105
/// bubble.
105106
final MessageCallBack assignReplyMessage;
106107

108+
/// Provides callback when user tap anywhere on whole chat.
109+
final VoidCallBack? onChatListTap;
110+
107111
@override
108112
State<ChatListWidget> createState() => _ChatListWidgetState();
109113
}
@@ -297,7 +301,10 @@ class _ChatListWidgetState extends State<ChatListWidget>
297301
}
298302

299303
void _onChatListTap() {
300-
if (!kIsWeb && Platform.isIOS) FocusScope.of(context).unfocus();
304+
widget.onChatListTap?.call();
305+
if (!kIsWeb && (Platform.isIOS || Platform.isAndroid)) {
306+
FocusScope.of(context).unfocus();
307+
}
301308
showPopUp.value = false;
302309
ScaffoldMessenger.of(context).hideCurrentSnackBar();
303310
}

lib/src/widgets/chat_view.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ChatView extends StatefulWidget {
5050
this.sendMessageBuilder,
5151
this.showTypingIndicator = false,
5252
this.sendMessageConfig,
53+
this.onChatListTap,
5354
required this.chatViewState,
5455
ChatViewStateConfiguration? chatViewStateConfig,
5556
this.featureActiveConfig = const FeatureActiveConfig(),
@@ -133,6 +134,9 @@ class ChatView extends StatefulWidget {
133134
/// Provides parameter so user can assign ChatViewAppbar.
134135
final Widget? appBar;
135136

137+
/// Provides callback when user tap on chat list.
138+
final VoidCallBack? onChatListTap;
139+
136140
@override
137141
State<ChatView> createState() => _ChatViewState();
138142
}
@@ -242,6 +246,7 @@ class _ChatViewState extends State<ChatView>
242246
profileCircleConfig: widget.profileCircleConfig,
243247
repliedMessageConfig: widget.repliedMessageConfig,
244248
swipeToReplyConfig: widget.swipeToReplyConfig,
249+
onChatListTap: widget.onChatListTap,
245250
assignReplyMessage: (message) => _sendMessageKey
246251
.currentState
247252
?.assignReplyMessage(message),

lib/src/widgets/chatui_textfield.dart

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
221221
if (!isRecordingValue) ...[
222222
IconButton(
223223
constraints: const BoxConstraints(),
224-
onPressed: () => _onIconPressed(ImageSource.camera),
224+
onPressed: () => _onIconPressed(
225+
ImageSource.camera,
226+
config: widget
227+
.sendMessageConfig?.imagePickerConfiguration,
228+
),
225229
icon:
226230
imagePickerIconsConfig?.cameraImagePickerIcon ??
227231
Icon(
@@ -232,8 +236,11 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
232236
),
233237
IconButton(
234238
constraints: const BoxConstraints(),
235-
onPressed: () =>
236-
_onIconPressed(ImageSource.gallery),
239+
onPressed: () => _onIconPressed(
240+
ImageSource.gallery,
241+
config: widget
242+
.sendMessageConfig?.imagePickerConfiguration,
243+
),
237244
icon: imagePickerIconsConfig
238245
?.galleryImagePickerIcon ??
239246
Icon(
@@ -284,10 +291,25 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
284291
}
285292
}
286293

287-
void _onIconPressed(ImageSource imageSource) async {
294+
void _onIconPressed(
295+
ImageSource imageSource, {
296+
ImagePickerConfiguration? config,
297+
}) async {
288298
try {
289-
final XFile? image = await _imagePicker.pickImage(source: imageSource);
290-
widget.onImageSelected(image?.path ?? '', '');
299+
final XFile? image = await _imagePicker.pickImage(
300+
source: imageSource,
301+
maxHeight: config?.maxHeight,
302+
maxWidth: config?.maxWidth,
303+
imageQuality: config?.imageQuality,
304+
preferredCameraDevice:
305+
config?.preferredCameraDevice ?? CameraDevice.rear,
306+
);
307+
String? imagePath = image?.path;
308+
if (config?.onImagePicked != null) {
309+
String? updatedImagePath = await config?.onImagePicked!(imagePath);
310+
if (updatedImagePath != null) imagePath = updatedImagePath;
311+
}
312+
widget.onImageSelected(imagePath ?? '', '');
291313
} catch (e) {
292314
widget.onImageSelected('', e.toString());
293315
}

0 commit comments

Comments
 (0)