feat: Icons are now disabled according to file type chosen

This commit is contained in:
Deven Joshi
2020-12-03 11:39:38 +01:00
committed by Salvatore Giordano
parent e1c1e053b3
commit c4c0f97f34
+51 -33
View File
@@ -108,7 +108,6 @@ class MessageInput extends StatefulWidget {
this.actionsLocation = ActionsLocation.left, this.actionsLocation = ActionsLocation.left,
this.attachmentThumbnailBuilders, this.attachmentThumbnailBuilders,
this.focusNode, this.focusNode,
this.enableFileAndImageAttachment = false,
}) : super(key: key); }) : super(key: key);
/// Message to edit /// Message to edit
@@ -157,9 +156,6 @@ class MessageInput extends StatefulWidget {
/// The focus node associated to the TextField /// The focus node associated to the TextField
final FocusNode focusNode; final FocusNode focusNode;
/// Enables adding both file+images at the same time
final bool enableFileAndImageAttachment;
@override @override
MessageInputState createState() => MessageInputState(); MessageInputState createState() => MessageInputState();
@@ -662,6 +658,34 @@ class MessageInputState extends State<MessageInput> {
} }
Widget _buildFilePickerSection() { Widget _buildFilePickerSection() {
var _attachmentContainsFile =
_attachments.any((element) => element.attachment.type == 'file');
Color _getIconColor(int index) {
switch (index) {
case 0:
return _attachmentContainsFile && _attachments.isNotEmpty
? Colors.black.withOpacity(0.2)
: Colors.black.withOpacity(0.5);
break;
case 1:
return !_attachmentContainsFile && _attachments.isNotEmpty
? Colors.black.withOpacity(0.2)
: Colors.black.withOpacity(0.5);
break;
case 2:
return _attachmentContainsFile && _attachments.isNotEmpty
? Colors.black.withOpacity(0.2)
: Colors.black.withOpacity(0.5);
break;
case 3:
return _attachmentContainsFile && _attachments.isNotEmpty
? Colors.black.withOpacity(0.2)
: Colors.black.withOpacity(0.5);
break;
}
}
return AnimatedContainer( return AnimatedContainer(
duration: _animateContainer ? Duration(milliseconds: 300) : Duration.zero, duration: _animateContainer ? Duration(milliseconds: 300) : Duration.zero,
height: _openFilePickerSection ? _filePickerSize : 0, height: _openFilePickerSection ? _filePickerSize : 0,
@@ -675,11 +699,11 @@ class MessageInputState extends State<MessageInput> {
IconButton( IconButton(
iconSize: 24, iconSize: 24,
icon: StreamSvgIcon.pictures( icon: StreamSvgIcon.pictures(
color: _filePickerIndex == 0 color: _getIconColor(0),
? StreamChatTheme.of(context).accentColor
: Colors.black.withOpacity(0.5),
), ),
onPressed: () { onPressed: _attachmentContainsFile && _attachments.isNotEmpty
? null
: () {
setState(() { setState(() {
_filePickerIndex = 0; _filePickerIndex = 0;
}); });
@@ -688,22 +712,22 @@ class MessageInputState extends State<MessageInput> {
IconButton( IconButton(
iconSize: 32, iconSize: 32,
icon: StreamSvgIcon.files( icon: StreamSvgIcon.files(
color: _filePickerIndex == 1 color: _getIconColor(1),
? StreamChatTheme.of(context).accentColor
: Colors.black.withOpacity(0.5),
), ),
onPressed: () { onPressed: !_attachmentContainsFile && _attachments.isNotEmpty
? null
: () {
pickFile(DefaultAttachmentTypes.file, false); pickFile(DefaultAttachmentTypes.file, false);
}, },
), ),
IconButton( IconButton(
iconSize: 24, iconSize: 24,
icon: StreamSvgIcon.camera( icon: StreamSvgIcon.camera(
color: _filePickerIndex == 2 color: _getIconColor(2),
? StreamChatTheme.of(context).accentColor
: Colors.black.withOpacity(0.5),
), ),
onPressed: () { onPressed: _attachmentContainsFile && _attachments.isNotEmpty
? null
: () {
pickFile(DefaultAttachmentTypes.image, true); pickFile(DefaultAttachmentTypes.image, true);
}, },
), ),
@@ -711,11 +735,11 @@ class MessageInputState extends State<MessageInput> {
padding: const EdgeInsets.all(0), padding: const EdgeInsets.all(0),
iconSize: 24, iconSize: 24,
icon: StreamSvgIcon.record( icon: StreamSvgIcon.record(
color: _filePickerIndex == 3 color: _getIconColor(3),
? StreamChatTheme.of(context).accentColor
: Colors.black.withOpacity(0.5),
), ),
onPressed: () { onPressed: _attachmentContainsFile && _attachments.isNotEmpty
? null
: () {
pickFile(DefaultAttachmentTypes.video, true); pickFile(DefaultAttachmentTypes.video, true);
}, },
), ),
@@ -774,6 +798,9 @@ class MessageInputState extends State<MessageInput> {
} }
Widget _buildPickerSection() { Widget _buildPickerSection() {
var _attachmentContainsFile =
_attachments.any((element) => element.attachment.type == 'file');
switch (_filePickerIndex) { switch (_filePickerIndex) {
case 0: case 0:
return FutureBuilder<bool>( return FutureBuilder<bool>(
@@ -786,19 +813,14 @@ class MessageInputState extends State<MessageInput> {
} }
if (snapshot.data) { if (snapshot.data) {
return MediaListView( return IgnorePointer(
ignoring: _attachmentContainsFile,
child: MediaListView(
selectedIds: _attachments.map((e) => e.id).toList(), selectedIds: _attachments.map((e) => e.id).toList(),
onSelect: (media) async { onSelect: (media) async {
if (!_attachments if (!_attachments
.any((element) => element.id == media.id)) { .any((element) => element.id == media.id)) {
if (widget.enableFileAndImageAttachment) {
_addAttachment(media); _addAttachment(media);
} else {
if (!_attachments.any(
(element) => element.attachment?.type == 'file')) {
_addAttachment(media);
}
}
} else { } else {
setState(() { setState(() {
_attachments _attachments
@@ -806,6 +828,7 @@ class MessageInputState extends State<MessageInput> {
}); });
} }
}, },
),
); );
} }
@@ -1581,11 +1604,6 @@ class MessageInputState extends State<MessageInput> {
bytes: bytes, bytes: bytes,
); );
} else { } else {
if (_attachments.any((element) => element.attachment.type != 'file') &&
!widget.enableFileAndImageAttachment) {
return;
}
FileType type; FileType type;
if (fileType == DefaultAttachmentTypes.image) { if (fileType == DefaultAttachmentTypes.image) {
type = FileType.image; type = FileType.image;