Expand composer + menu upward from the button
This commit is contained in:
1 parent
9ac915bdf2
commit
390a4a80fc
2 files changed
+331
-35
No files matched your search
+193
-28
@@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:math' as math;
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
@@ -132,6 +134,9 @@ class ChatComposer extends StatelessWidget {
|
|||||||
/// Marks the compose bubble (and the offline bar's bubble).
|
/// Marks the compose bubble (and the offline bar's bubble).
|
||||||
static const bubbleKey = ValueKey('chat-composer-bubble');
|
static const bubbleKey = ValueKey('chat-composer-bubble');
|
||||||
|
|
||||||
|
/// Marks the surface of the open "+" menu.
|
||||||
|
static const actionMenuKey = ValueKey('chat-composer-action-menu');
|
||||||
|
|
||||||
const ChatComposer({
|
const ChatComposer({
|
||||||
super.key,
|
super.key,
|
||||||
required this.controller,
|
required this.controller,
|
||||||
@@ -152,34 +157,25 @@ class ChatComposer extends StatelessWidget {
|
|||||||
final actions = actionsBuilder();
|
final actions = actionsBuilder();
|
||||||
if (actions.isEmpty) return;
|
if (actions.isEmpty) return;
|
||||||
final button = buttonContext.findRenderObject() as RenderBox;
|
final button = buttonContext.findRenderObject() as RenderBox;
|
||||||
final overlay =
|
final navigator = Navigator.of(buttonContext);
|
||||||
Overlay.of(buttonContext).context.findRenderObject() as RenderBox;
|
final overlay = navigator.overlay!.context.findRenderObject() as RenderBox;
|
||||||
final buttonOffset = button.localToGlobal(Offset.zero, ancestor: overlay);
|
final anchor =
|
||||||
final menuHeight = actions.length * kMinInteractiveDimension + 16;
|
button.localToGlobal(Offset.zero, ancestor: overlay) & button.size;
|
||||||
final menuTop = (buttonOffset.dy - menuHeight).clamp(
|
// The menu route never requests focus: if it took focus from the text
|
||||||
8.0,
|
// field the keyboard would close, the composer would drop down and the
|
||||||
overlay.size.height,
|
// menu would be left floating where the keyboard used to be.
|
||||||
);
|
final selected = await navigator.push<String>(
|
||||||
final position = RelativeRect.fromLTRB(
|
_ComposerActionMenuRoute(
|
||||||
buttonOffset.dx,
|
actions: actions,
|
||||||
menuTop,
|
anchor: anchor,
|
||||||
overlay.size.width - buttonOffset.dx - button.size.width,
|
overlaySize: overlay.size,
|
||||||
overlay.size.height - buttonOffset.dy,
|
capturedThemes: InheritedTheme.capture(
|
||||||
);
|
from: buttonContext,
|
||||||
final selected = await showMenu<String>(
|
to: navigator.context,
|
||||||
context: buttonContext,
|
),
|
||||||
position: position,
|
barrierLabel: MaterialLocalizations.of(buttonContext).menuDismissLabel,
|
||||||
// Keep focus on the text field: if the menu route took focus, the
|
animate: !MediaQuery.disableAnimationsOf(buttonContext),
|
||||||
// keyboard would close, the composer would drop down and the menu would
|
),
|
||||||
// be left floating where the keyboard used to be.
|
|
||||||
requestFocus: false,
|
|
||||||
items: [
|
|
||||||
for (final action in actions)
|
|
||||||
PopupMenuItem(
|
|
||||||
value: action.id,
|
|
||||||
child: MeshMenuItemRow(icon: action.icon, label: action.label),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
if (selected == null || !buttonContext.mounted) return;
|
if (selected == null || !buttonContext.mounted) return;
|
||||||
onAction(selected);
|
onAction(selected);
|
||||||
@@ -585,3 +581,172 @@ class OfflineComposerBar extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The "+" menu: a popup that grows upward from the bottom-trailing corner,
|
||||||
|
/// just above the "+" button, so it opens out of the button instead of
|
||||||
|
/// sliding down toward it.
|
||||||
|
class _ComposerActionMenuRoute extends PopupRoute<String> {
|
||||||
|
final List<ChatComposerAction> actions;
|
||||||
|
final Rect anchor;
|
||||||
|
final Size overlaySize;
|
||||||
|
final CapturedThemes capturedThemes;
|
||||||
|
final bool animate;
|
||||||
|
|
||||||
|
@override
|
||||||
|
final String barrierLabel;
|
||||||
|
|
||||||
|
_ComposerActionMenuRoute({
|
||||||
|
required this.actions,
|
||||||
|
required this.anchor,
|
||||||
|
required this.overlaySize,
|
||||||
|
required this.capturedThemes,
|
||||||
|
required this.barrierLabel,
|
||||||
|
required this.animate,
|
||||||
|
}) : super(requestFocus: false);
|
||||||
|
|
||||||
|
static const double _gap = 4;
|
||||||
|
static const double _margin = 8;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Color? get barrierColor => null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get barrierDismissible => true;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Duration get transitionDuration =>
|
||||||
|
animate ? const Duration(milliseconds: 250) : Duration.zero;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Duration get reverseTransitionDuration =>
|
||||||
|
animate ? const Duration(milliseconds: 150) : Duration.zero;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget buildPage(
|
||||||
|
BuildContext context,
|
||||||
|
Animation<double> animation,
|
||||||
|
Animation<double> secondaryAnimation,
|
||||||
|
) {
|
||||||
|
final direction = Directionality.of(context);
|
||||||
|
final padding = MediaQuery.paddingOf(context);
|
||||||
|
final end = direction == TextDirection.ltr
|
||||||
|
? overlaySize.width - anchor.right
|
||||||
|
: anchor.left;
|
||||||
|
final maxHeight = math.max(
|
||||||
|
kMinInteractiveDimension,
|
||||||
|
anchor.top - _gap - padding.top - _margin,
|
||||||
|
);
|
||||||
|
return capturedThemes.wrap(
|
||||||
|
Stack(
|
||||||
|
children: [
|
||||||
|
Positioned.directional(
|
||||||
|
textDirection: direction,
|
||||||
|
end: math.max(_margin, end),
|
||||||
|
bottom: overlaySize.height - anchor.top + _gap,
|
||||||
|
child: ConstrainedBox(
|
||||||
|
constraints: BoxConstraints(
|
||||||
|
minWidth: 112,
|
||||||
|
maxWidth: math.max(112, math.min(280, overlaySize.width - 16)),
|
||||||
|
maxHeight: maxHeight,
|
||||||
|
),
|
||||||
|
child: _ComposerActionMenu(
|
||||||
|
actions: actions,
|
||||||
|
animation: animation,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ComposerActionMenu extends StatelessWidget {
|
||||||
|
final List<ChatComposerAction> actions;
|
||||||
|
final Animation<double> animation;
|
||||||
|
|
||||||
|
const _ComposerActionMenu({required this.actions, required this.animation});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final theme = Theme.of(context);
|
||||||
|
final scheme = theme.colorScheme;
|
||||||
|
final menuTheme = PopupMenuTheme.of(context);
|
||||||
|
final items = IntrinsicWidth(
|
||||||
|
stepWidth: 56,
|
||||||
|
child: Semantics(
|
||||||
|
scopesRoute: true,
|
||||||
|
namesRoute: true,
|
||||||
|
explicitChildNodes: true,
|
||||||
|
label: MaterialLocalizations.of(context).popupMenuLabel,
|
||||||
|
child: SingleChildScrollView(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||||
|
child: DefaultTextStyle(
|
||||||
|
style: (menuTheme.textStyle ?? theme.textTheme.labelLarge!)
|
||||||
|
.copyWith(color: scheme.onSurface),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
for (final action in actions)
|
||||||
|
InkWell(
|
||||||
|
onTap: () => Navigator.of(context).pop(action.id),
|
||||||
|
child: ConstrainedBox(
|
||||||
|
constraints: const BoxConstraints(
|
||||||
|
minHeight: kMinInteractiveDimension,
|
||||||
|
),
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||||
|
child: Align(
|
||||||
|
alignment: AlignmentDirectional.centerStart,
|
||||||
|
child: MeshMenuItemRow(
|
||||||
|
icon: action.icon,
|
||||||
|
label: action.label,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return AnimatedBuilder(
|
||||||
|
animation: animation,
|
||||||
|
builder: (context, child) {
|
||||||
|
final t = animation.value;
|
||||||
|
final heightFactor =
|
||||||
|
(animation.status == AnimationStatus.reverse
|
||||||
|
? Curves.easeIn
|
||||||
|
: Curves.easeOutCubic)
|
||||||
|
.transform(t);
|
||||||
|
return Opacity(
|
||||||
|
opacity: const Interval(0, 0.4).transform(t),
|
||||||
|
child: Material(
|
||||||
|
key: ChatComposer.actionMenuKey,
|
||||||
|
type: MaterialType.card,
|
||||||
|
color: menuTheme.color ?? scheme.surfaceContainerHigh,
|
||||||
|
elevation: menuTheme.elevation ?? 3,
|
||||||
|
shadowColor: menuTheme.shadowColor,
|
||||||
|
surfaceTintColor: Colors.transparent,
|
||||||
|
shape:
|
||||||
|
menuTheme.shape ??
|
||||||
|
RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(MeshRadii.md),
|
||||||
|
),
|
||||||
|
clipBehavior: Clip.antiAlias,
|
||||||
|
child: Align(
|
||||||
|
alignment: AlignmentDirectional.bottomEnd,
|
||||||
|
widthFactor: 1,
|
||||||
|
heightFactor: heightFactor,
|
||||||
|
child: child,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: items,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -65,6 +65,7 @@ Widget _app(
|
|||||||
Widget? replyPreview,
|
Widget? replyPreview,
|
||||||
bool disableAnimations = false,
|
bool disableAnimations = false,
|
||||||
Brightness brightness = Brightness.light,
|
Brightness brightness = Brightness.light,
|
||||||
|
List<ChatComposerAction>? actions,
|
||||||
}) {
|
}) {
|
||||||
final composer = ChatComposer(
|
final composer = ChatComposer(
|
||||||
controller: h.controller,
|
controller: h.controller,
|
||||||
@@ -72,13 +73,15 @@ Widget _app(
|
|||||||
maxBytes: maxBytes,
|
maxBytes: maxBytes,
|
||||||
onSend: () => h.sends++,
|
onSend: () => h.sends++,
|
||||||
sendTooltip: _l10n.chat_sendMessage,
|
sendTooltip: _l10n.chat_sendMessage,
|
||||||
actionsBuilder: () => [
|
actionsBuilder: () =>
|
||||||
ChatComposerAction(
|
actions ??
|
||||||
id: 'gif',
|
[
|
||||||
icon: Icons.gif_box,
|
ChatComposerAction(
|
||||||
label: _l10n.chat_sendGif,
|
id: 'gif',
|
||||||
),
|
icon: Icons.gif_box,
|
||||||
],
|
label: _l10n.chat_sendGif,
|
||||||
|
),
|
||||||
|
],
|
||||||
onAction: h.actions.add,
|
onAction: h.actions.add,
|
||||||
replyPreview: replyPreview,
|
replyPreview: replyPreview,
|
||||||
);
|
);
|
||||||
@@ -119,6 +122,7 @@ Widget _app(
|
|||||||
|
|
||||||
Finder get _plus => find.byTooltip(_l10n.chat_selectSendAction);
|
Finder get _plus => find.byTooltip(_l10n.chat_selectSendAction);
|
||||||
Finder get _send => find.byTooltip(_l10n.chat_sendMessage);
|
Finder get _send => find.byTooltip(_l10n.chat_sendMessage);
|
||||||
|
Finder get _menu => find.byKey(ChatComposer.actionMenuKey);
|
||||||
Finder get _bubble => find.byKey(ChatComposer.bubbleKey);
|
Finder get _bubble => find.byKey(ChatComposer.bubbleKey);
|
||||||
|
|
||||||
Future<void> _type(WidgetTester tester, String text) async {
|
Future<void> _type(WidgetTester tester, String text) async {
|
||||||
@@ -380,6 +384,133 @@ void main() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('+ menu opens above the button and grows upward from it', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final h = await _harness(tester);
|
||||||
|
await tester.pumpWidget(_app(h));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final plusTop = tester.getTopLeft(_plus).dy;
|
||||||
|
final plusRight = tester.getTopRight(_plus).dx;
|
||||||
|
|
||||||
|
await tester.tap(_plus);
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump(const Duration(milliseconds: 50));
|
||||||
|
final early = tester.getRect(_menu);
|
||||||
|
await tester.pump(const Duration(milliseconds: 50));
|
||||||
|
final later = tester.getRect(_menu);
|
||||||
|
expect(later.top, lessThan(early.top));
|
||||||
|
expect(later.bottom, closeTo(early.bottom, 0.01));
|
||||||
|
expect(later.height, greaterThan(early.height));
|
||||||
|
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final open = tester.getRect(_menu);
|
||||||
|
expect(open.bottom, closeTo(early.bottom, 0.01));
|
||||||
|
expect(open.bottom, lessThanOrEqualTo(plusTop));
|
||||||
|
// Trailing edges line up (the tap target is a little wider than the
|
||||||
|
// visible button).
|
||||||
|
expect(open.right, closeTo(plusRight, 3));
|
||||||
|
expect(open.height, greaterThanOrEqualTo(kMinInteractiveDimension));
|
||||||
|
// The card hugs a short label instead of filling the width cap.
|
||||||
|
expect(open.width, lessThan(280));
|
||||||
|
|
||||||
|
await tester.tapAt(const Offset(5, 5));
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump(const Duration(milliseconds: 60));
|
||||||
|
final closing = tester.getRect(_menu);
|
||||||
|
expect(closing.bottom, closeTo(open.bottom, 0.01));
|
||||||
|
expect(closing.height, lessThan(open.height));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(_menu, findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('+ menu opens at once with animations disabled', (tester) async {
|
||||||
|
final h = await _harness(tester);
|
||||||
|
await tester.pumpWidget(_app(h, disableAnimations: true));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(_plus);
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.text(_l10n.chat_sendGif), findsOneWidget);
|
||||||
|
expect(
|
||||||
|
tester.getRect(_menu).height,
|
||||||
|
greaterThanOrEqualTo(kMinInteractiveDimension),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
tester.getRect(_menu).bottom,
|
||||||
|
lessThanOrEqualTo(tester.getTopLeft(_plus).dy),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('+ menu keeps focus on the text field', (tester) async {
|
||||||
|
final h = await _harness(tester);
|
||||||
|
await tester.pumpWidget(_app(h));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
h.focusNode.requestFocus();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(h.focusNode.hasFocus, isTrue);
|
||||||
|
|
||||||
|
await tester.tap(_plus);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text(_l10n.chat_sendGif), findsOneWidget);
|
||||||
|
expect(h.focusNode.hasFocus, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('tapping outside the + menu closes it without an action', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final h = await _harness(tester);
|
||||||
|
await tester.pumpWidget(_app(h));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(_plus);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(_menu, findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tapAt(const Offset(5, 5));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(_menu, findsNothing);
|
||||||
|
expect(h.actions, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (final scale in [1.3, 2.0]) {
|
||||||
|
testWidgets('+ menu stays above the button at 320dp, ${scale}x text', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
tester.view.physicalSize = const Size(320, 568);
|
||||||
|
tester.view.devicePixelRatio = 1;
|
||||||
|
tester.platformDispatcher.textScaleFactorTestValue = scale;
|
||||||
|
addTearDown(tester.view.reset);
|
||||||
|
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
|
||||||
|
final h = await _harness(tester);
|
||||||
|
await tester.pumpWidget(
|
||||||
|
_app(
|
||||||
|
h,
|
||||||
|
actions: [
|
||||||
|
for (var i = 0; i < 4; i++)
|
||||||
|
ChatComposerAction(
|
||||||
|
id: 'a$i',
|
||||||
|
icon: Icons.gif_box,
|
||||||
|
label: 'A rather long action label number $i for the menu',
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(_plus);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(tester.takeException(), isNull);
|
||||||
|
final menu = tester.getRect(_menu);
|
||||||
|
expect(menu.bottom, lessThanOrEqualTo(tester.getTopLeft(_plus).dy));
|
||||||
|
expect(menu.left, greaterThanOrEqualTo(0));
|
||||||
|
expect(menu.right, lessThanOrEqualTo(320));
|
||||||
|
expect(menu.top, greaterThanOrEqualTo(0));
|
||||||
|
|
||||||
|
await tester.tap(find.textContaining('number 3'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(h.actions, ['a3']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
testWidgets('panel is solid without a chat background and transparent '
|
testWidgets('panel is solid without a chat background and transparent '
|
||||||
'over one', (tester) async {
|
'over one', (tester) async {
|
||||||
final h = await _harness(tester);
|
final h = await _harness(tester);
|
||||||
|
|||||||
Reference in new issue
Block a user