Skip to content

Commit

Permalink
TW-1274: Add key for typeAhead and support auto focus in captions
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev authored and hoangdat committed Jan 15, 2024
1 parent 7d5b42e commit 908d875
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
7 changes: 7 additions & 0 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ class ChatController extends State<Chat>

PinnedEventsController pinnedEventsController = PinnedEventsController();

final ValueKey chatComposerTypeAheadKey =
const ValueKey('chatComposerTypeAheadKey');

final ValueKey _chatMediaPickerTypeAheadKey =
const ValueKey('chatMediaPickerTypeAheadKey');

@override
Room? room;

Expand Down Expand Up @@ -1308,6 +1314,7 @@ class ChatController extends State<Chat>
onCameraPicked: (_) => sendMedia(imagePickerController, room: room),
captionController: _captionsController,
focusSuggestionController: _focusSuggestionController,
typeAheadKey: _chatMediaPickerTypeAheadKey,
);
}

Expand Down
1 change: 1 addition & 0 deletions lib/pages/chat/chat_input_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class ChatInputRow extends StatelessWidget {

InputBar _buildInputBar(BuildContext context) {
return InputBar(
typeAheadKey: controller.chatComposerTypeAheadKey,
room: controller.room!,
minLines: 1,
maxLines: 8,
Expand Down
5 changes: 3 additions & 2 deletions lib/pages/chat/input_bar/input_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class InputBar extends StatelessWidget with PasteImageMixin {
final InputDecoration decoration;
final ValueChanged<String>? onChanged;
final bool autofocus;
final bool enablePasteImage;
final ValueKey? typeAheadKey;

InputBar({
this.room,
Expand All @@ -47,8 +47,8 @@ class InputBar extends StatelessWidget with PasteImageMixin {
this.autofocus = false,
this.textInputAction,
this.suggestionScrollController,
this.enablePasteImage = true,
required this.focusSuggestionController,
this.typeAheadKey,
Key? key,
}) : super(key: key);

Expand Down Expand Up @@ -340,6 +340,7 @@ class InputBar extends StatelessWidget with PasteImageMixin {
room: room,
onEnter: _onEnter,
child: TypeAheadField<Map<String, String?>>(
key: typeAheadKey,
direction: AxisDirection.up,
hideOnEmpty: true,
hideOnLoading: true,
Expand Down
10 changes: 10 additions & 0 deletions lib/pages/chat/send_file_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class SendFileDialogController extends State<SendFileDialog> {

final TextEditingController textEditingController = TextEditingController();

final FocusNode captionsFocusNode = FocusNode();

final ValueKey sendFileDialogTypeAheadKey =
const ValueKey('sendFileDialogTypeAhead');

bool isSendMediaWithCaption = true;

List<MatrixFile> get files => widget.files;
Expand All @@ -41,6 +46,7 @@ class SendFileDialogController extends State<SendFileDialog> {
void initState() {
super.initState();
isSendMediaWithCaption = _isShowSendMediaDialog(widget.files, widget.room);
requestFocusCaptions();
}

@override
Expand All @@ -49,6 +55,10 @@ class SendFileDialogController extends State<SendFileDialog> {
super.dispose();
}

void requestFocusCaptions() {
captionsFocusNode.requestFocus();
}

void sendMediaWithCaption() {
if (widget.room == null) {
Logs().e("sendMediaWithCaption:: room is null");
Expand Down
31 changes: 21 additions & 10 deletions lib/pages/chat/send_file_dialog_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,27 @@ class SendFileDialogView extends StatelessWidget {
room: controller.widget.room,
),
const SizedBox(height: 16.0),
InputBar(
maxLines: 5,
minLines: 1,
focusSuggestionController: controller.focusSuggestionController,
room: controller.widget.room,
controller: controller.textEditingController,
decoration:
SendFileDialogStyle.bottomBarInputDecoration(context),
keyboardType: TextInputType.multiline,
enablePasteImage: false,
InkWell(
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
focusColor: Colors.transparent,
onTap: controller.requestFocusCaptions,
child: InputBar(
typeAheadKey: controller.sendFileDialogTypeAheadKey,
maxLines: 5,
minLines: 1,
focusSuggestionController:
controller.focusSuggestionController,
room: controller.widget.room,
controller: controller.textEditingController,
textInputAction: null,
decoration:
SendFileDialogStyle.bottomBarInputDecoration(context),
keyboardType: TextInputType.multiline,
focusNode: controller.captionsFocusNode,
autofocus: !PlatformInfos.isMobile,
onSubmitted: (_) => controller.send(),
),
),
SendFileDialogStyle.spaceBwInputBarAndButton,
Row(
Expand Down
7 changes: 7 additions & 0 deletions lib/pages/chat_draft/draft_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class DraftChatController extends State<DraftChat>
final FocusSuggestionController focusSuggestionController =
FocusSuggestionController();

final ValueKey draftChatComposerTypeAheadKey =
const ValueKey('draftChatComposerTypeAheadKey');

final ValueKey _draftChatMediaPickerTypeAheadKey =
const ValueKey('draftChatMediaPickerTypeAheadKey');

FocusNode inputFocus = FocusNode();
FocusNode keyboardFocus = FocusNode();

Expand Down Expand Up @@ -296,6 +302,7 @@ class DraftChatController extends State<DraftChat>
),
onSendTap: () => sendMedia(imagePickerController),
onCameraPicked: (_) => sendMedia(imagePickerController),
typeAheadKey: _draftChatMediaPickerTypeAheadKey,
);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/pages/chat_draft/draft_chat_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class DraftChatView extends StatelessWidget {
children: [
Expanded(
child: InputBar(
typeAheadKey: controller
.draftChatComposerTypeAheadKey,
minLines:
DraftChatViewStyle
.minLinesInputBar,
Expand Down
14 changes: 13 additions & 1 deletion lib/presentation/mixins/media_picker_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:fluffychat/pages/chat/input_bar/input_bar.dart';
import 'package:fluffychat/pages/chat/item_actions_bottom_widget.dart';
import 'package:fluffychat/pages/chat/send_file_dialog_style.dart';
import 'package:fluffychat/resource/image_paths.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_svg/flutter_svg.dart';
Expand Down Expand Up @@ -38,6 +39,7 @@ mixin MediaPickerMixin on CommonMediaPickerMixin {
OnCameraPicked? onCameraPicked,
FocusSuggestionController? focusSuggestionController,
TextEditingController? captionController,
ValueKey? typeAheadKey,
}) async {
final currentPermissionPhotos = await getCurrentMediaPermission();
final currentPermissionCamera = await getCurrentCameraPermission();
Expand All @@ -53,6 +55,7 @@ mixin MediaPickerMixin on CommonMediaPickerMixin {
onCameraPicked: onCameraPicked,
focusSuggestionController: focusSuggestionController,
captionController: captionController,
typeAheadKey: typeAheadKey,
);
}
}
Expand All @@ -69,6 +72,7 @@ mixin MediaPickerMixin on CommonMediaPickerMixin {
Widget? inputBar,
FocusSuggestionController? focusSuggestionController,
TextEditingController? captionController,
ValueKey? typeAheadKey,
}) async {
final numberSelectedImagesNotifier = ValueNotifier<int>(0);
imagePickerController.addListener(() {
Expand Down Expand Up @@ -182,8 +186,10 @@ mixin MediaPickerMixin on CommonMediaPickerMixin {
children: [
Expanded(
child: InputBar(
typeAheadKey: typeAheadKey,
maxLines: 5,
minLines: 1,
textInputAction: null,
focusSuggestionController:
focusSuggestionController ??
FocusSuggestionController(),
Expand All @@ -194,7 +200,13 @@ mixin MediaPickerMixin on CommonMediaPickerMixin {
context,
),
keyboardType: TextInputType.multiline,
enablePasteImage: false,
autofocus: !PlatformInfos.isMobile,
onSubmitted: (_) {
if (onSendTap != null) {
onSendTap();
}
Navigator.of(context).pop();
},
),
),
Padding(
Expand Down

0 comments on commit 908d875

Please sign in to comment.