fix(chat): Return breaks lines on phones and align the activity line
On Android and iOS the keyboard's Return now inserts a newline and the Send button sends; desktop keeps Enter to send. The Material activity line drops TextButton's inset so it starts at the answer's edge now that replies have no bubble.
This commit is contained in:
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'chat_stop_icon.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -30,6 +31,16 @@ String chatComposerPlaceholderFor(String directory) {
|
||||
return name.isEmpty ? chatComposerPlaceholder : 'Work on $name';
|
||||
}
|
||||
|
||||
/// Whether Enter sends on [platform] (the running host by default).
|
||||
///
|
||||
/// On a phone the on-screen keyboard's Return breaks the line and the Send
|
||||
/// button sends, as in other chat apps; a desktop keyboard's Enter sends and
|
||||
/// Shift+Enter breaks the line. Ctrl/Cmd+Enter sends everywhere.
|
||||
bool chatEnterSends([TargetPlatform? platform]) {
|
||||
final target = platform ?? defaultTargetPlatform;
|
||||
return target != TargetPlatform.android && target != TargetPlatform.iOS;
|
||||
}
|
||||
|
||||
/// The single primary control's words while a turn is and is not running.
|
||||
const String chatSendLabel = 'Send';
|
||||
const String chatStopLabel = 'Stop';
|
||||
@@ -82,8 +93,12 @@ class ChatComposerBar extends StatefulWidget {
|
||||
this.paste,
|
||||
this.backgroundTasks,
|
||||
this.placeholder = chatComposerPlaceholder,
|
||||
this.enterSends,
|
||||
});
|
||||
|
||||
/// Overrides [chatEnterSends] for this composer; null follows the host.
|
||||
final bool? enterSends;
|
||||
|
||||
final ChatComposerController composer;
|
||||
|
||||
/// The empty field's hint; see [chatComposerPlaceholderFor].
|
||||
@@ -270,6 +285,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
if (_text.value.composing.isValid && !_text.value.composing.isCollapsed) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
// A phone keyboard's Return is the field's own newline; only the
|
||||
// modifier shortcut sends from the keyboard there.
|
||||
if (!sendShortcut && !_enterSends) return KeyEventResult.ignored;
|
||||
if (!sendShortcut && keyboard.isShiftPressed) {
|
||||
if (event is KeyDownEvent) {
|
||||
final selection = _text.selection;
|
||||
@@ -292,6 +310,8 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
|
||||
bool get _enterSends => widget.enterSends ?? chatEnterSends();
|
||||
|
||||
/// Hardware Enter, the keyboard's Send action and modifier shortcuts share
|
||||
/// the same command/prompt path. An active IME candidate is never submitted.
|
||||
void _submitDraft() {
|
||||
@@ -939,7 +959,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
// strips Shift+Enter's newline on the next text edit.
|
||||
maxLines: empty && lines > 1 ? lines : 6,
|
||||
keyboardType: TextInputType.multiline,
|
||||
textInputAction: TextInputAction.send,
|
||||
textInputAction: _enterSends
|
||||
? TextInputAction.send
|
||||
: TextInputAction.newline,
|
||||
onEditingComplete: () {},
|
||||
onSubmitted: (_) => _submitDraft(),
|
||||
onChanged: _onDraftEdited,
|
||||
@@ -954,7 +976,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
minLines: 1,
|
||||
maxLines: 6,
|
||||
keyboardType: TextInputType.multiline,
|
||||
textInputAction: TextInputAction.send,
|
||||
textInputAction: _enterSends
|
||||
? TextInputAction.send
|
||||
: TextInputAction.newline,
|
||||
onEditingComplete: () {},
|
||||
onSubmitted: (_) => _submitDraft(),
|
||||
onChanged: _onDraftEdited,
|
||||
|
||||
@@ -186,6 +186,12 @@ class _ChatTurnActivityViewState extends State<ChatTurnActivityView> {
|
||||
)
|
||||
: TextButton(
|
||||
key: const Key('chat-turn-activity'),
|
||||
// No inset of its own: the line starts at the answer's edge,
|
||||
// which no longer sits inside a padded bubble.
|
||||
style: TextButton.styleFrom(
|
||||
padding: EdgeInsets.zero,
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
),
|
||||
onPressed: _open,
|
||||
child: content,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user