From c5328650f16ea5643b7701af086a247e56ef698f Mon Sep 17 00:00:00 2001 From: Veli Bacik Date: Wed, 29 Nov 2023 11:29:06 +0300 Subject: [PATCH] implemented leadingWidth and automaticallyImplyLeading options (#136165) Before: ![Screenshot 2023-10-09 at 01 32 33](https://github.com/flutter/flutter/assets/17102578/cb90423c-8d8d-40d5-afd0-5aab23b9930b) After: ![Screenshot 2023-10-09 at 01 33 01](https://github.com/flutter/flutter/assets/17102578/3ba84243-42b8-4e02-b064-70618e21a305) ![Screenshot 2023-10-09 at 01 32 49](https://github.com/flutter/flutter/assets/17102578/4f311d74-f6a8-4f32-9974-109bd7f55812) Fixed #136164 --- .../flutter/lib/src/material/app_bar.dart | 8 ++-- packages/flutter/lib/src/material/search.dart | 8 ++++ .../flutter/test/material/search_test.dart | 45 +++++++++++++++++-- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index f641f114a3..8fcbc5435c 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -277,8 +277,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { /// {@template flutter.material.appbar.automaticallyImplyLeading} /// Controls whether we should try to imply the leading widget if null. /// - /// If true and [leading] is null, automatically try to deduce what the leading - /// widget should be. If false and [leading] is null, leading space is given to [title]. + /// If true and [AppBar.leading] is null, automatically try to deduce what the leading + /// widget should be. If false and [AppBar.leading] is null, leading space is given to [AppBar.title]. /// If leading widget is not null, this parameter has no effect. /// {@endtemplate} final bool automaticallyImplyLeading; @@ -642,9 +642,9 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { final double? toolbarHeight; /// {@template flutter.material.appbar.leadingWidth} - /// Defines the width of [leading] widget. + /// Defines the width of [AppBar.leading] widget. /// - /// By default, the value of [leadingWidth] is 56.0. + /// By default, the value of [AppBar.leadingWidth] is 56.0. /// {@endtemplate} final double? leadingWidth; diff --git a/packages/flutter/lib/src/material/search.dart b/packages/flutter/lib/src/material/search.dart index 507af6df90..bca887c6e8 100644 --- a/packages/flutter/lib/src/material/search.dart +++ b/packages/flutter/lib/src/material/search.dart @@ -188,6 +188,12 @@ abstract class SearchDelegate { /// * [AppBar.leading], the intended use for the return value of this method. Widget? buildLeading(BuildContext context); + /// {@macro flutter.material.appbar.automaticallyImplyLeading} + bool? automaticallyImplyLeading; + + /// {@macro flutter.material.appbar.leadingWidth} + double? leadingWidth; + /// Widgets to display after the search query in the [AppBar]. /// /// If the [query] is not empty, this should typically contain a button to @@ -592,6 +598,8 @@ class _SearchPageState extends State<_SearchPage> { data: theme, child: Scaffold( appBar: AppBar( + leadingWidth: widget.delegate.leadingWidth, + automaticallyImplyLeading: widget.delegate.automaticallyImplyLeading ?? true, leading: widget.delegate.buildLeading(context), title: TextField( controller: widget.delegate._queryTextController, diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index 4d64182135..ab36668fd9 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -1025,7 +1025,31 @@ void main() { expect(selectedResults, ['Result']); }); - testWidgetsWithLeakTracking('showSearch with useRootNavigator', (WidgetTester tester) async { + testWidgets('Leading width size is 16', (WidgetTester tester) async { + final _TestSearchDelegate delegate = _TestSearchDelegate(); + final List selectedResults = []; + delegate.leadingWidth = 16; + + await tester.pumpWidget(TestHomePage( + delegate: delegate, + results: selectedResults, + )); + + // Open the search page with check leading width smaller than 16. + await tester.tap(find.byTooltip('Search')); + await tester.pumpAndSettle(); + await tester.tapAt(const Offset(16, 16)); + expect(find.text('Suggestions'), findsOneWidget); + final Finder appBarFinder = find.byType(AppBar); + final AppBar appBar = tester.widget(appBarFinder); + expect(appBar.leadingWidth, 16); + await tester.tapAt(const Offset(8, 16)); + await tester.pumpAndSettle(); + expect(find.text('Suggestions'), findsNothing); + expect(find.text('HomeBody'), findsOneWidget); + }); + + testWidgetsWithLeakTracking('showSearch with useRootNavigator', (WidgetTester tester) async { final _MyNavigatorObserver rootObserver = _MyNavigatorObserver(); final _MyNavigatorObserver localObserver = _MyNavigatorObserver(); @@ -1066,20 +1090,33 @@ void main() { expect(rootObserver.pushCount, 0); expect(localObserver.pushCount, 0); - // showSearch normal and back + // showSearch normal and back. await tester.tap(find.text('showSearchLocalNavigator')); await tester.pumpAndSettle(); + final Finder backButtonFinder = find.byType(BackButton); + expect(backButtonFinder, findsWidgets); await tester.tap(find.byTooltip('Close')); await tester.pumpAndSettle(); expect(rootObserver.pushCount, 0); expect(localObserver.pushCount, 1); - // showSearch with rootNavigator + // showSearch with rootNavigator. await tester.tap(find.text('showSearchRootNavigator')); await tester.pumpAndSettle(); await tester.tap(find.byTooltip('Close')); await tester.pumpAndSettle(); - expect(rootObserver.pushCount, 1); + + // showSearch without back button. + delegate.automaticallyImplyLeading = false; + await tester.tap(find.text('showSearchRootNavigator')); + await tester.pumpAndSettle(); + final Finder appBarFinder = find.byType(AppBar); + final AppBar appBar = tester.widget(appBarFinder); + expect(appBar.automaticallyImplyLeading, false); + expect(find.byTooltip('Back'), findsNothing); + await tester.tap(find.byTooltip('Close')); + await tester.pumpAndSettle(); + expect(rootObserver.pushCount, 2); expect(localObserver.pushCount, 1); });