import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { for (final kind in ['searchable', 'popover', 'context']) { for (final stored in [true, 120.0]) { testWidgets('$kind popup isolates ancestor state $stored', ( tester, ) async { final bucket = PageStorageBucket(); const ancestorKey = PageStorageKey('parent-state'); Widget popup() => switch (kind) { 'searchable' => AppSelect( value: 0, label: 'Select', emptyLabel: 'Empty', searchable: true, searchHint: 'Search', onChanged: (_) {}, options: [ for (var i = 0; i < 40; i++) AppSelectOption(i, 'Choice $i'), ], ), 'popover' => AppPopover( builder: (_, toggle) => TextButton( key: const Key('trigger'), onPressed: toggle, child: const Text('Open'), ), child: SizedBox( height: 150, child: ListView( primary: false, children: [ for (var i = 0; i < 40; i++) SizedBox(height: 32, child: Text('Choice $i')), ], ), ), ), _ => AppContextMenu( itemsBuilder: (_) => [ for (var i = 0; i < 40; i++) AppMenuItem(label: 'Choice $i', onPressed: () {}), ], child: const SizedBox( key: Key('trigger'), width: 200, height: 100, child: ColoredBox(color: Colors.transparent), ), ), }; await tester.pumpWidget( MaterialApp( theme: AppTheme.penDark, home: Scaffold( body: Align( alignment: Alignment.topLeft, child: SizedBox( width: 240, child: PageStorage( bucket: bucket, child: Builder( key: ancestorKey, builder: (context) { bucket.writeState(context, stored); return popup(); }, ), ), ), ), ), ), ); await tester.pumpAndSettle(); await tester.tap( kind == 'searchable' ? find.byType(TextField) : find.byKey(const Key('trigger')), buttons: kind == 'context' ? kSecondaryMouseButton : kPrimaryMouseButton, ); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); final positions = tester .stateList( find.byWidgetPredicate( (widget) => widget is Scrollable && axisDirectionToAxis(widget.axisDirection) == Axis.vertical, ), ) .map((state) => state.position) .toList(); expect(positions, isNotEmpty); for (final position in positions) { expect( position.pixels, 0, reason: 'Popup must not restore its parent offset', ); } positions .firstWhere((position) => position.maxScrollExtent > 50) .jumpTo(25); await tester.pumpAndSettle(); expect( bucket.readState(tester.element(find.byKey(ancestorKey))), stored, ); await tester.tapAt(const Offset(790, 590)); await tester.pumpAndSettle(); expect(find.text('Choice 3'), findsNothing); expect( bucket.readState(tester.element(find.byKey(ancestorKey))), stored, ); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); }); } } }