|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:navigation_devtools/navigation_devtools.dart'; |
| 4 | +import 'package:navigation_devtools_shared/navigation_devtools_shared.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + Future<Navigator1Adapter> pumpApp(WidgetTester tester) async { |
| 8 | + final adapter = Navigator1Adapter(); |
| 9 | + await tester.pumpWidget(MaterialApp( |
| 10 | + navigatorObservers: [adapter.observer], |
| 11 | + home: Builder( |
| 12 | + builder: (context) => Scaffold( |
| 13 | + body: Column(children: [ |
| 14 | + ElevatedButton( |
| 15 | + onPressed: () => showDialog<void>( |
| 16 | + context: context, |
| 17 | + builder: (_) => const AlertDialog(title: Text('Hi')), |
| 18 | + ), |
| 19 | + child: const Text('dialog'), |
| 20 | + ), |
| 21 | + ElevatedButton( |
| 22 | + onPressed: () => showModalBottomSheet<void>( |
| 23 | + context: context, |
| 24 | + builder: (_) => const SizedBox(height: 100), |
| 25 | + ), |
| 26 | + child: const Text('sheet'), |
| 27 | + ), |
| 28 | + ]), |
| 29 | + ), |
| 30 | + ), |
| 31 | + )); |
| 32 | + await tester.pumpAndSettle(); |
| 33 | + return adapter; |
| 34 | + } |
| 35 | + |
| 36 | + testWidgets('showDialog appears on the stack as a pageless DialogRoute', |
| 37 | + (tester) async { |
| 38 | + final adapter = await pumpApp(tester); |
| 39 | + var fired = 0; |
| 40 | + adapter.changes.listen((_) => fired++); |
| 41 | + |
| 42 | + await tester.tap(find.text('dialog')); |
| 43 | + await tester.pumpAndSettle(); |
| 44 | + |
| 45 | + final top = adapter.currentStack().last; |
| 46 | + expect(top.kind, StackFrameKind.pageless); |
| 47 | + expect(top.name, contains('DialogRoute')); |
| 48 | + expect(top.isActive, isTrue); |
| 49 | + expect(fired, greaterThan(0)); |
| 50 | + |
| 51 | + // Panel-driven pop closes the dialog. |
| 52 | + final result = await adapter.navigate(NavigateRequest( |
| 53 | + routerId: adapter.id, action: NavigateAction.pop)); |
| 54 | + await tester.pumpAndSettle(); |
| 55 | + expect(result.ok, isTrue); |
| 56 | + expect(adapter.currentStack().last.name, isNot(contains('DialogRoute'))); |
| 57 | + expect(find.text('Hi'), findsNothing); |
| 58 | + }); |
| 59 | + |
| 60 | + testWidgets('showModalBottomSheet appears as a pageless route and pops off', |
| 61 | + (tester) async { |
| 62 | + final adapter = await pumpApp(tester); |
| 63 | + |
| 64 | + await tester.tap(find.text('sheet')); |
| 65 | + await tester.pumpAndSettle(); |
| 66 | + |
| 67 | + final top = adapter.currentStack().last; |
| 68 | + expect(top.kind, StackFrameKind.pageless); |
| 69 | + expect(top.name, contains('ModalBottomSheetRoute')); |
| 70 | + |
| 71 | + // Closing the sheet in-app removes it from the shadow stack. |
| 72 | + Navigator.of(tester.element(find.text('sheet'))).pop(); |
| 73 | + await tester.pumpAndSettle(); |
| 74 | + expect( |
| 75 | + adapter.currentStack().map((f) => f.name), |
| 76 | + isNot(contains(contains('ModalBottomSheetRoute'))), |
| 77 | + ); |
| 78 | + }); |
| 79 | +} |
0 commit comments