Skip to content

Commit

Permalink
implemented leadingWidth and automaticallyImplyLeading options (#136165)
Browse files Browse the repository at this point in the history
  • Loading branch information
VB10 authored Nov 29, 2023
1 parent 178130b commit c532865
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/flutter/lib/src/material/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions packages/flutter/lib/src/material/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ abstract class SearchDelegate<T> {
/// * [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
Expand Down Expand Up @@ -592,6 +598,8 @@ class _SearchPageState<T> extends State<_SearchPage<T>> {
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,
Expand Down
45 changes: 41 additions & 4 deletions packages/flutter/test/material/search_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,31 @@ void main() {
expect(selectedResults, <String>['Result']);
});

testWidgetsWithLeakTracking('showSearch with useRootNavigator', (WidgetTester tester) async {
testWidgets('Leading width size is 16', (WidgetTester tester) async {
final _TestSearchDelegate delegate = _TestSearchDelegate();
final List<String> selectedResults = <String>[];
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<AppBar>(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();

Expand Down Expand Up @@ -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();

// 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<AppBar>(appBarFinder);
expect(appBar.automaticallyImplyLeading, false);
expect(find.byTooltip('Back'), findsNothing);
await tester.tap(find.byTooltip('Close'));
await tester.pumpAndSettle();
expect(rootObserver.pushCount, 1);
expect(rootObserver.pushCount, 2);
expect(localObserver.pushCount, 1);
});

Expand Down

0 comments on commit c532865

Please sign in to comment.