Anchored menus: bound by system insets instead of shifting by them

The anchored choice menu route wrapped its layout in a SafeArea while the
anchor rect is in overlay coordinates, so on Android (edge to edge, a
status bar inset) every top bar menu opened an inset below its control.
The insets and a half-opened hinge now only bound the panel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 21:19:55 +00:00
parent ae5d0190fa
commit 8c224fecf8
3 changed files with 355 additions and 74 deletions
@@ -0,0 +1,208 @@
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_header.dart';
import 'package:pipelines_app/ui/adaptive/app_popups.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_adaptive.dart';
import 'package:pipelines_app/ui/adaptive/segmented_bar_overflow.dart';
/// An unfolded foldable: 884x1104 dp, a status bar and a cutout above the
/// app (Android draws edge to edge), a gesture bar below it, and the fold
/// down the middle.
void _foldable(WidgetTester tester, {bool halfOpened = false}) {
tester.view.physicalSize = const Size(884, 1104);
tester.view.devicePixelRatio = 1;
tester.view.padding = const FakeViewPadding(top: 52, bottom: 24);
tester.view.viewPadding = const FakeViewPadding(top: 52, bottom: 24);
tester.view.displayFeatures = [
DisplayFeature(
bounds: halfOpened
? const Rect.fromLTWH(436, 0, 12, 1104)
: const Rect.fromLTWH(442, 0, 0, 1104),
type: halfOpened ? DisplayFeatureType.hinge : DisplayFeatureType.fold,
state: halfOpened
? DisplayFeatureState.postureHalfOpened
: DisplayFeatureState.postureFlat,
),
];
addTearDown(tester.view.reset);
}
Widget _choices(BuildContext menu) => Column(
mainAxisSize: MainAxisSize.min,
children: [
for (final choice in const ['Opus', 'Sonnet', 'Haiku'])
ListTile(title: Text(choice), onTap: () => Navigator.of(menu).pop()),
],
);
Widget _menuButton(String label) => Builder(
builder: (anchor) => TextButton(
onPressed: () => showAppChoiceMenu<void>(anchor, builder: _choices),
child: Text(label),
),
);
/// A chat pane's top bar the way the app lays it out: the app's own top
/// bar under the status bar, the selectors in the pane header, and the
/// overflow menu at its end.
Widget _app({required double listWidth}) => MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(
body: SafeArea(
child: Row(
children: [
SizedBox(width: listWidth, child: const Placeholder()),
Expanded(
child: Column(
children: [
ChatThreadHeader(
title: 'Chat',
folder: 'demo',
onInfo: () {},
controls: [
_menuButton('Model'),
_menuButton('Permission'),
_menuButton('Thinking'),
],
moreActions: [
ChatBarAction(
label: 'Rename chat',
onPressed: () {},
materialIcon: Icons.edit_outlined,
),
],
),
const Expanded(child: SizedBox.expand()),
],
),
),
],
),
),
),
);
final _menu = find.byKey(const ValueKey('app-choice-menu'));
Future<void> _expectUnder(
WidgetTester tester,
Finder button,
Finder panel, {
double slack = 16,
}) async {
final anchor = tester.getRect(button);
await tester.tap(button);
await tester.pumpAndSettle();
final menu = tester.getRect(panel);
expect(menu.top, greaterThanOrEqualTo(anchor.bottom - 1));
expect(
menu.top - anchor.bottom,
lessThan(slack),
reason: 'the menu opens right under its anchor, not an inset lower',
);
await tester.tapAt(const Offset(200, 900));
await tester.pumpAndSettle();
}
void main() {
testWidgets('unfolded foldable, wide pane: every top bar menu opens '
'under its control', (tester) async {
_foldable(tester);
await tester.pumpWidget(_app(listWidth: 200));
await tester.pumpAndSettle();
for (final label in const ['Model', 'Thinking', 'Permission']) {
final button = find.widgetWithText(TextButton, label);
await _expectUnder(tester, button, _menu);
}
// The overflow menu is a MenuAnchor in the root overlay.
await _expectUnder(
tester,
find.byKey(segmentedMoreKey).first,
find.text('Rename chat').hitTestable(),
// The first item's label, inside the panel's and the item's padding.
slack: 32,
);
});
testWidgets('unfolded foldable, narrow pane: the settings panel opens '
'under the bar and each selector menu under its control', (tester) async {
_foldable(tester);
await tester.pumpWidget(_app(listWidth: 360));
await tester.pumpAndSettle();
final bar = tester.getRect(find.byType(ChatThreadHeader));
await tester.tap(find.byKey(segmentedMoreKey).first);
await tester.pumpAndSettle();
await tester.tap(find.text('Chat settings'));
await tester.pumpAndSettle();
final settings = tester.getRect(_menu);
expect(settings.top - bar.bottom, inInclusiveRange(-1, 16));
for (final label in const ['Model', 'Thinking', 'Permission']) {
final button = find.widgetWithText(TextButton, label);
final anchor = tester.getRect(button);
await tester.tap(button);
await tester.pumpAndSettle();
final menu = tester.getRect(_menu.last);
expect(
menu.top - anchor.bottom,
inInclusiveRange(-1, 16),
reason: '$label menu right under its control',
);
await tester.tap(find.text('Opus').last);
await tester.pumpAndSettle();
}
});
testWidgets('a menu stays inside the safe area and above when needed', (
tester,
) async {
_foldable(tester);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Align(
alignment: Alignment.bottomRight,
child: _menuButton('Low'),
),
),
),
);
final anchor = tester.getRect(find.widgetWithText(TextButton, 'Low'));
await tester.tap(find.text('Low'));
await tester.pumpAndSettle();
final menu = tester.getRect(_menu);
// No room below: above the anchor, close to it, inside the app.
expect(menu.bottom, lessThanOrEqualTo(anchor.top));
expect(anchor.top - menu.bottom, lessThan(16));
expect(menu.right, lessThanOrEqualTo(884 - 8));
});
testWidgets('a half opened hinge keeps the menu on its anchor side', (
tester,
) async {
_foldable(tester, halfOpened: true);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 340),
child: Align(
alignment: Alignment.topLeft,
child: _menuButton('Across'),
),
),
),
),
),
);
final anchor = tester.getRect(find.widgetWithText(TextButton, 'Across'));
await tester.tap(find.text('Across'));
await tester.pumpAndSettle();
final menu = tester.getRect(_menu);
expect(menu.top - anchor.bottom, inInclusiveRange(-1, 16));
// The anchor's centre is left of the hinge: the panel stays left of it.
expect(menu.right, lessThanOrEqualTo(436));
});
}