From 329d54b37ccf199e7966c05a2bfee05ec1fe94cd Mon Sep 17 00:00:00 2001 From: Navaron Bracke Date: Tue, 3 Feb 2026 09:36:18 +0100 Subject: [PATCH] Reduce reliance on Material in page_transitions_test.dart (#181467) This PR reduces the reliance on Material in page_transitions_test.dart but does not entirely remove it. 1) Moves a test for persistent bottom sheet to the right file 2) Fixes a use of `sync*` (not sure why this is not caught by dev/bots/analyze.dart, that _does_ check for `sync*` cc @Piinks ) 3) Replaces some easy usages of Material widgets with its widget counterparts. The only thing that I did not yet fix is replacing `MaterialApp` in this file. That probably needs @victorsanni 's work on page transitions to be addressed first? Part of https://github.com/flutter/flutter/issues/177415 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../persistent_bottom_sheet_test.dart | 92 ++++++++++++++ .../test/widgets/page_transitions_test.dart | 117 +++--------------- 2 files changed, 106 insertions(+), 103 deletions(-) diff --git a/packages/flutter/test/material/persistent_bottom_sheet_test.dart b/packages/flutter/test/material/persistent_bottom_sheet_test.dart index 58e13f6e9a7..4b1d3b7740e 100644 --- a/packages/flutter/test/material/persistent_bottom_sheet_test.dart +++ b/packages/flutter/test/material/persistent_bottom_sheet_test.dart @@ -689,4 +689,96 @@ void main() { await tester.pump(); expect(find.byType(BottomSheet), findsNothing); }); + + // Regression test for https://github.com/flutter/flutter/issues/6451 + testWidgets( + 'Check back gesture with a persistent bottom sheet showing', + (WidgetTester tester) async { + final GlobalKey containerKey1 = GlobalKey(); + final GlobalKey containerKey2 = GlobalKey(); + final routes = { + '/': (_) => Scaffold(key: containerKey1, body: const Text('Home')), + '/sheet': (_) => PersistentBottomSheetTest(key: containerKey2), + }; + + await tester.pumpWidget(MaterialApp(routes: routes)); + + Navigator.pushNamed(containerKey1.currentContext!, '/sheet'); + + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + + expect(find.text('Home'), findsNothing); + expect(find.text('Sheet'), isOnstage); + + // Drag from left edge to invoke the gesture. We should go back. + TestGesture gesture = await tester.startGesture(const Offset(5.0, 100.0)); + await gesture.moveBy(const Offset(500.0, 0.0)); + await gesture.up(); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + + Navigator.pushNamed(containerKey1.currentContext!, '/sheet'); + + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + + expect(find.text('Home'), findsNothing); + expect(find.text('Sheet'), isOnstage); + + // Show the bottom sheet. + final PersistentBottomSheetTestState sheet = containerKey2.currentState!; + sheet.showBottomSheet(); + + await tester.pump(const Duration(seconds: 1)); + + // Drag from left edge to invoke the gesture. Nothing should happen. + gesture = await tester.startGesture(const Offset(5.0, 100.0)); + await gesture.moveBy(const Offset(500.0, 0.0)); + await gesture.up(); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + + expect(find.text('Home'), findsNothing); + expect(find.text('Sheet'), isOnstage); + + // Sheet did not call setState (since the gesture did nothing). + expect(sheet.setStateCalled, isFalse); + }, + variant: const TargetPlatformVariant({ + TargetPlatform.iOS, + TargetPlatform.macOS, + }), + ); +} + +class PersistentBottomSheetTest extends StatefulWidget { + const PersistentBottomSheetTest({super.key}); + + @override + PersistentBottomSheetTestState createState() => PersistentBottomSheetTestState(); +} + +class PersistentBottomSheetTestState extends State { + final GlobalKey _scaffoldKey = GlobalKey(); + + bool setStateCalled = false; + + void showBottomSheet() { + _scaffoldKey.currentState! + .showBottomSheet((BuildContext context) { + return const Text('bottomSheet'); + }) + .closed + .whenComplete(() { + setState(() { + setStateCalled = true; + }); + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold(key: _scaffoldKey, body: const Text('Sheet')); + } } diff --git a/packages/flutter/test/widgets/page_transitions_test.dart b/packages/flutter/test/widgets/page_transitions_test.dart index 8118ec61bb2..f4010af1d9c 100644 --- a/packages/flutter/test/widgets/page_transitions_test.dart +++ b/packages/flutter/test/widgets/page_transitions_test.dart @@ -8,44 +8,11 @@ import 'package:flutter_test/flutter_test.dart'; class TestOverlayRoute extends OverlayRoute { TestOverlayRoute({super.settings}); @override - Iterable createOverlayEntries() sync* { - yield OverlayEntry(builder: _build); - } + Iterable createOverlayEntries() => [OverlayEntry(builder: _build)]; Widget _build(BuildContext context) => const Text('Overlay'); } -class PersistentBottomSheetTest extends StatefulWidget { - const PersistentBottomSheetTest({super.key}); - - @override - PersistentBottomSheetTestState createState() => PersistentBottomSheetTestState(); -} - -class PersistentBottomSheetTestState extends State { - final GlobalKey _scaffoldKey = GlobalKey(); - - bool setStateCalled = false; - - void showBottomSheet() { - _scaffoldKey.currentState! - .showBottomSheet((BuildContext context) { - return const Text('bottomSheet'); - }) - .closed - .whenComplete(() { - setState(() { - setStateCalled = true; - }); - }); - } - - @override - Widget build(BuildContext context) { - return Scaffold(key: _scaffoldKey, body: const Text('Sheet')); - } -} - void main() { testWidgets('Check onstage/offstage handling around transitions', (WidgetTester tester) async { final GlobalKey containerKey1 = GlobalKey(); @@ -136,16 +103,16 @@ void main() { final GlobalKey containerKey2 = GlobalKey(); const kHeroTag = 'hero'; final routes = { - '/': (_) => Scaffold( + '/': (_) => SizedBox( key: containerKey1, - body: const ColoredBox( + child: const ColoredBox( color: Color(0xff00ffff), child: Hero(tag: kHeroTag, child: Text('Home')), ), ), - '/settings': (_) => Scaffold( + '/settings': (_) => SizedBox( key: containerKey2, - body: Container( + child: Container( padding: const EdgeInsets.all(100.0), color: const Color(0xffff00ff), child: const Hero(tag: kHeroTag, child: Text('Settings')), @@ -205,8 +172,8 @@ void main() { final GlobalKey containerKey1 = GlobalKey(); final GlobalKey containerKey2 = GlobalKey(); final routes = { - '/': (_) => Scaffold(key: containerKey1, body: const Text('Home')), - '/settings': (_) => Scaffold(key: containerKey2, body: const Text('Settings')), + '/': (_) => SizedBox(key: containerKey1, child: const Text('Home')), + '/settings': (_) => SizedBox(key: containerKey2, child: const Text('Settings')), }; await tester.pumpWidget(MaterialApp(routes: routes)); @@ -249,67 +216,6 @@ void main() { }), ); - // Tests bug https://github.com/flutter/flutter/issues/6451 - testWidgets( - 'Check back gesture with a persistent bottom sheet showing', - (WidgetTester tester) async { - final GlobalKey containerKey1 = GlobalKey(); - final GlobalKey containerKey2 = GlobalKey(); - final routes = { - '/': (_) => Scaffold(key: containerKey1, body: const Text('Home')), - '/sheet': (_) => PersistentBottomSheetTest(key: containerKey2), - }; - - await tester.pumpWidget(MaterialApp(routes: routes)); - - Navigator.pushNamed(containerKey1.currentContext!, '/sheet'); - - await tester.pump(); - await tester.pump(const Duration(seconds: 1)); - - expect(find.text('Home'), findsNothing); - expect(find.text('Sheet'), isOnstage); - - // Drag from left edge to invoke the gesture. We should go back. - TestGesture gesture = await tester.startGesture(const Offset(5.0, 100.0)); - await gesture.moveBy(const Offset(500.0, 0.0)); - await gesture.up(); - await tester.pump(); - await tester.pump(const Duration(seconds: 1)); - - Navigator.pushNamed(containerKey1.currentContext!, '/sheet'); - - await tester.pump(); - await tester.pump(const Duration(seconds: 1)); - - expect(find.text('Home'), findsNothing); - expect(find.text('Sheet'), isOnstage); - - // Show the bottom sheet. - final sheet = containerKey2.currentState! as PersistentBottomSheetTestState; - sheet.showBottomSheet(); - - await tester.pump(const Duration(seconds: 1)); - - // Drag from left edge to invoke the gesture. Nothing should happen. - gesture = await tester.startGesture(const Offset(5.0, 100.0)); - await gesture.moveBy(const Offset(500.0, 0.0)); - await gesture.up(); - await tester.pump(); - await tester.pump(const Duration(seconds: 1)); - - expect(find.text('Home'), findsNothing); - expect(find.text('Sheet'), isOnstage); - - // Sheet did not call setState (since the gesture did nothing). - expect(sheet.setStateCalled, isFalse); - }, - variant: const TargetPlatformVariant({ - TargetPlatform.iOS, - TargetPlatform.macOS, - }), - ); - testWidgets('Test completed future', (WidgetTester tester) async { final routes = { '/': (_) => const Center(child: Text('home')), @@ -318,9 +224,14 @@ void main() { await tester.pumpWidget(MaterialApp(routes: routes)); - final PageRoute route = MaterialPageRoute( + final PageRoute route = PageRouteBuilder( settings: const RouteSettings(name: '/page'), - builder: (BuildContext context) => const Center(child: Text('page')), + pageBuilder: + ( + BuildContext context, + Animation animation, + Animation secondaryAnimation, + ) => const Center(child: Text('page')), ); var popCount = 0;