From 2c4e7ebfd9fd86bf5c63553cd4031c7211829d32 Mon Sep 17 00:00:00 2001 From: Pacific <39942340+prash4931@users.noreply.github.com> Date: Thu, 11 Dec 2025 02:23:33 +0530 Subject: [PATCH] Fixed RenderFlex overflow in RouteObserver Example (#170980) Fixed RenderOverflow in RouteObserver Example: https://api.flutter.dev/flutter/widgets/RouteObserver-class.html This PR is about solving render overflow issue in Route Observer Example in Live Website. I've wrapped the body with a Scrollable View to solve this issue | | | | --- | --- | | B | ![image](https://github.com/user-attachments/assets/c8fa78e7-62ca-4bd1-9798-588c636cd61a) | | A | ![image](https://github.com/user-attachments/assets/3a2b4697-121a-43e3-9c85-b1379a575af1) | ## 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]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] 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]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. [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 --------- Co-authored-by: Victor Sanni --- .../lib/widgets/routes/route_observer.0.dart | 62 ++++++++++--------- .../widgets/routes/route_observer.0_test.dart | 20 ++++++ 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/examples/api/lib/widgets/routes/route_observer.0.dart b/examples/api/lib/widgets/routes/route_observer.0.dart index fdb44fdce45..e1ce6adc72e 100644 --- a/examples/api/lib/widgets/routes/route_observer.0.dart +++ b/examples/api/lib/widgets/routes/route_observer.0.dart @@ -63,37 +63,39 @@ class _RouteObserverExampleState extends State @override Widget build(BuildContext context) { return Scaffold( - body: Center( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - Text( - 'RouteObserver log:', - style: Theme.of(context).textTheme.headlineSmall, - ), - ConstrainedBox( - constraints: const BoxConstraints(maxHeight: 300.0), - child: ListView.builder( - itemCount: log.length, - itemBuilder: (BuildContext context, int index) { - if (log.isEmpty) { - return const SizedBox.shrink(); - } - return Text(log[index], textAlign: TextAlign.center); - }, + body: SingleChildScrollView( + child: Center( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text( + 'RouteObserver log:', + style: Theme.of(context).textTheme.headlineSmall, ), - ), - OutlinedButton( - onPressed: () { - Navigator.of(context).push( - MaterialPageRoute( - builder: (BuildContext context) => const NextPage(), - ), - ); - }, - child: const Text('Go to next page'), - ), - ], + ConstrainedBox( + constraints: const BoxConstraints(maxHeight: 300.0), + child: ListView.builder( + itemCount: log.length, + itemBuilder: (BuildContext context, int index) { + if (log.isEmpty) { + return const SizedBox.shrink(); + } + return Text(log[index], textAlign: TextAlign.center); + }, + ), + ), + OutlinedButton( + onPressed: () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (BuildContext context) => const NextPage(), + ), + ); + }, + child: const Text('Go to next page'), + ), + ], + ), ), ), ); diff --git a/examples/api/test/widgets/routes/route_observer.0_test.dart b/examples/api/test/widgets/routes/route_observer.0_test.dart index 3c79c476398..91e60952e3a 100644 --- a/examples/api/test/widgets/routes/route_observer.0_test.dart +++ b/examples/api/test/widgets/routes/route_observer.0_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/rendering.dart'; import 'package:flutter_api_samples/widgets/routes/route_observer.0.dart' as example; import 'package:flutter_test/flutter_test.dart'; @@ -38,5 +39,24 @@ void main() { // Check the RouteObserver logs after the route is popped again. expect(find.text('didPush'), findsOneWidget); expect(find.text('didPopNext'), findsNWidgets(2)); + + // Check if any overflow or layout exceptions occurred. + expect(tester.takeException(), isNull); }); + + testWidgets( + 'RouteObserver example renders without overflow on small screens', + (WidgetTester tester) async { + // Set the screen size to a smaller value. + tester.view.physicalSize = const Size(200, 200); + addTearDown(tester.view.reset); + + // Build the RouteObserver example widget. + await tester.pumpWidget(const example.RouteObserverApp()); + await tester.pumpAndSettle(); + + // Verify there are no layout exceptions. + expect(tester.takeException(), isNull); + }, + ); }