|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:lantern/core/common/app_dialog.dart'; |
| 5 | +import 'package:lantern/core/common/app_theme.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + testWidgets('dialog callback does not pop the route beneath the dialog', ( |
| 9 | + tester, |
| 10 | + ) async { |
| 11 | + await tester.pumpWidget( |
| 12 | + ScreenUtilInit( |
| 13 | + designSize: const Size(390, 844), |
| 14 | + child: MaterialApp(theme: AppTheme.appTheme(), home: const _HomePage()), |
| 15 | + ), |
| 16 | + ); |
| 17 | + await tester.pumpAndSettle(); |
| 18 | + |
| 19 | + await tester.tap(find.byKey(const Key('home.open_page'))); |
| 20 | + await tester.pumpAndSettle(); |
| 21 | + expect(find.byKey(const Key('dialog_page')), findsOneWidget); |
| 22 | + |
| 23 | + await tester.tap(find.byKey(const Key('dialog_page.open_dialog'))); |
| 24 | + await tester.pumpAndSettle(); |
| 25 | + expect(find.text('Dialog title'), findsOneWidget); |
| 26 | + |
| 27 | + await tester.tap(find.text('Close')); |
| 28 | + await tester.pump(); |
| 29 | + await tester.pump(const Duration(milliseconds: 500)); |
| 30 | + await tester.pumpAndSettle(); |
| 31 | + |
| 32 | + expect(find.text('Dialog title'), findsNothing); |
| 33 | + expect(find.byKey(const Key('dialog_page')), findsOneWidget); |
| 34 | + expect(find.byKey(const Key('home')), findsNothing); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +class _HomePage extends StatelessWidget { |
| 39 | + const _HomePage(); |
| 40 | + |
| 41 | + @override |
| 42 | + Widget build(BuildContext context) { |
| 43 | + return Scaffold( |
| 44 | + key: const Key('home'), |
| 45 | + body: Center( |
| 46 | + child: ElevatedButton( |
| 47 | + key: const Key('home.open_page'), |
| 48 | + onPressed: () { |
| 49 | + Navigator.of(context).push<void>( |
| 50 | + MaterialPageRoute<void>(builder: (_) => const _DialogPage()), |
| 51 | + ); |
| 52 | + }, |
| 53 | + child: const Text('Open page'), |
| 54 | + ), |
| 55 | + ), |
| 56 | + ); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +class _DialogPage extends StatelessWidget { |
| 61 | + const _DialogPage(); |
| 62 | + |
| 63 | + @override |
| 64 | + Widget build(BuildContext context) { |
| 65 | + return Scaffold( |
| 66 | + key: const Key('dialog_page'), |
| 67 | + body: Center( |
| 68 | + child: ElevatedButton( |
| 69 | + key: const Key('dialog_page.open_dialog'), |
| 70 | + onPressed: () { |
| 71 | + AppDialog.dialog( |
| 72 | + context: context, |
| 73 | + title: 'Dialog title', |
| 74 | + content: 'Dialog body', |
| 75 | + action: 'Close', |
| 76 | + onPressed: () => Navigator.of(context).pop(), |
| 77 | + ); |
| 78 | + }, |
| 79 | + child: const Text('Open dialog'), |
| 80 | + ), |
| 81 | + ), |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments