From 46f96a44e45bb312da837df3d547811440e84332 Mon Sep 17 00:00:00 2001 From: Gagan Yadav Date: Wed, 29 Jan 2025 11:02:53 +0530 Subject: [PATCH] fix: appbar leading width is not square for custom toolbar height (#161880) Fixes #161301 This change allows setting custom `leadingWidth` in the `AppBarTheme` along with `toolbarHeight`. --- .../flutter/lib/src/material/app_bar.dart | 8 ++- .../lib/src/material/app_bar_theme.dart | 11 +++ .../test/material/app_bar_theme_test.dart | 70 +++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 89cd4ed1e4..59d5f039d2 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -1002,12 +1002,16 @@ class _AppBarState extends State { // a size of 48x48, and a highlight size of 40x40. Users can also put other // type of widgets on leading with the original config. leading = ConstrainedBox( - constraints: BoxConstraints.tightFor(width: widget.leadingWidth ?? _kLeadingWidth), + constraints: BoxConstraints.tightFor( + width: widget.leadingWidth ?? appBarTheme.leadingWidth ?? _kLeadingWidth, + ), child: leading, ); } else { leading = ConstrainedBox( - constraints: BoxConstraints.tightFor(width: widget.leadingWidth ?? _kLeadingWidth), + constraints: BoxConstraints.tightFor( + width: widget.leadingWidth ?? appBarTheme.leadingWidth ?? _kLeadingWidth, + ), child: leading, ); } diff --git a/packages/flutter/lib/src/material/app_bar_theme.dart b/packages/flutter/lib/src/material/app_bar_theme.dart index 0d332f7964..07242f5a5f 100644 --- a/packages/flutter/lib/src/material/app_bar_theme.dart +++ b/packages/flutter/lib/src/material/app_bar_theme.dart @@ -42,6 +42,7 @@ class AppBarTheme with Diagnosticable { this.actionsIconTheme, this.centerTitle, this.titleSpacing, + this.leadingWidth, this.toolbarHeight, this.toolbarTextStyle, this.titleTextStyle, @@ -123,6 +124,10 @@ class AppBarTheme with Diagnosticable { /// If null, [AppBar] uses default value of [NavigationToolbar.kMiddleSpacing]. final double? titleSpacing; + /// Overrides the default value of the [AppBar.leadingWidth] + /// property in all descendant [AppBar] widgets. + final double? leadingWidth; + /// Overrides the default value of the [AppBar.toolbarHeight] /// property in all descendant [AppBar] widgets. /// @@ -173,6 +178,7 @@ class AppBarTheme with Diagnosticable { IconThemeData? iconTheme, bool? centerTitle, double? titleSpacing, + double? leadingWidth, double? toolbarHeight, TextStyle? toolbarTextStyle, TextStyle? titleTextStyle, @@ -195,6 +201,7 @@ class AppBarTheme with Diagnosticable { actionsIconTheme: actionsIconTheme ?? this.actionsIconTheme, centerTitle: centerTitle ?? this.centerTitle, titleSpacing: titleSpacing ?? this.titleSpacing, + leadingWidth: leadingWidth ?? this.leadingWidth, toolbarHeight: toolbarHeight ?? this.toolbarHeight, toolbarTextStyle: toolbarTextStyle ?? this.toolbarTextStyle, titleTextStyle: titleTextStyle ?? this.titleTextStyle, @@ -227,6 +234,7 @@ class AppBarTheme with Diagnosticable { actionsIconTheme: IconThemeData.lerp(a?.actionsIconTheme, b?.actionsIconTheme, t), centerTitle: t < 0.5 ? a?.centerTitle : b?.centerTitle, titleSpacing: lerpDouble(a?.titleSpacing, b?.titleSpacing, t), + leadingWidth: lerpDouble(a?.leadingWidth, b?.leadingWidth, t), toolbarHeight: lerpDouble(a?.toolbarHeight, b?.toolbarHeight, t), toolbarTextStyle: TextStyle.lerp(a?.toolbarTextStyle, b?.toolbarTextStyle, t), titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t), @@ -248,6 +256,7 @@ class AppBarTheme with Diagnosticable { actionsIconTheme, centerTitle, titleSpacing, + leadingWidth, toolbarHeight, toolbarTextStyle, titleTextStyle, @@ -275,6 +284,7 @@ class AppBarTheme with Diagnosticable { other.actionsIconTheme == actionsIconTheme && other.centerTitle == centerTitle && other.titleSpacing == titleSpacing && + other.leadingWidth == leadingWidth && other.toolbarHeight == toolbarHeight && other.toolbarTextStyle == toolbarTextStyle && other.titleTextStyle == titleTextStyle && @@ -304,6 +314,7 @@ class AppBarTheme with Diagnosticable { ); properties.add(DiagnosticsProperty('centerTitle', centerTitle, defaultValue: null)); properties.add(DiagnosticsProperty('titleSpacing', titleSpacing, defaultValue: null)); + properties.add(DiagnosticsProperty('leadingWidth', leadingWidth, defaultValue: null)); properties.add(DiagnosticsProperty('toolbarHeight', toolbarHeight, defaultValue: null)); properties.add( DiagnosticsProperty('toolbarTextStyle', toolbarTextStyle, defaultValue: null), diff --git a/packages/flutter/test/material/app_bar_theme_test.dart b/packages/flutter/test/material/app_bar_theme_test.dart index 2ef51a5682..75a94120db 100644 --- a/packages/flutter/test/material/app_bar_theme_test.dart +++ b/packages/flutter/test/material/app_bar_theme_test.dart @@ -761,6 +761,38 @@ void main() { expect(navToolBar.middleSpacing, 40); }); + testWidgets('AppBar uses AppBarTheme.leadingWidth', (WidgetTester tester) async { + const double kLeadingWidth = 80; + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(appBarTheme: const AppBarTheme(leadingWidth: kLeadingWidth)), + home: Scaffold(appBar: AppBar(leading: const Icon(Icons.chevron_left))), + ), + ); + + final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar)); + final BoxConstraints leadingConstraints = (navToolBar.leading! as ConstrainedBox).constraints; + expect(leadingConstraints.maxWidth, kLeadingWidth); + expect(leadingConstraints.minWidth, kLeadingWidth); + }); + + testWidgets('AppBar.leadingWidth takes priority over AppBarTheme.leadingWidth', ( + WidgetTester tester, + ) async { + const double kLeadingWidth = 80; + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(appBarTheme: const AppBarTheme(leadingWidth: kLeadingWidth)), + home: Scaffold(appBar: AppBar(leading: const Icon(Icons.chevron_left), leadingWidth: 40)), + ), + ); + + final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar)); + final BoxConstraints leadingConstraints = (navToolBar.leading! as ConstrainedBox).constraints; + expect(leadingConstraints.maxWidth, 40); + expect(leadingConstraints.minWidth, 40); + }); + testWidgets('SliverAppBar uses AppBarTheme.titleSpacing', (WidgetTester tester) async { const double kTitleSpacing = 10; await tester.pumpWidget( @@ -791,6 +823,42 @@ void main() { expect(navToolbar.middleSpacing, 40); }); + testWidgets('SliverAppBar uses AppBarTheme.leadingWidth', (WidgetTester tester) async { + const double kLeadingWidth = 80; + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(appBarTheme: const AppBarTheme(leadingWidth: kLeadingWidth)), + home: const CustomScrollView( + slivers: [SliverAppBar(leading: Icon(Icons.chevron_left))], + ), + ), + ); + + final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar)); + final BoxConstraints leadingConstraints = (navToolBar.leading! as ConstrainedBox).constraints; + expect(leadingConstraints.maxWidth, kLeadingWidth); + expect(leadingConstraints.minWidth, kLeadingWidth); + }); + + testWidgets('SliverAppBar.leadingWidth takes priority over AppBarTheme.leadingWidth ', ( + WidgetTester tester, + ) async { + const double kLeadingWidth = 80; + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(appBarTheme: const AppBarTheme(leadingWidth: kLeadingWidth)), + home: const CustomScrollView( + slivers: [SliverAppBar(leading: Icon(Icons.chevron_left), leadingWidth: 40)], + ), + ), + ); + + final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar)); + final BoxConstraints leadingConstraints = (navToolBar.leading! as ConstrainedBox).constraints; + expect(leadingConstraints.maxWidth, 40); + expect(leadingConstraints.minWidth, 40); + }); + testWidgets('SliverAppBar.medium uses AppBarTheme properties', (WidgetTester tester) async { const String title = 'Medium App Bar'; @@ -1100,6 +1168,7 @@ void main() { iconTheme: IconThemeData(color: Color(0xff000004)), centerTitle: true, titleSpacing: 40.0, + leadingWidth: 96, toolbarHeight: 96, toolbarTextStyle: TextStyle(color: Color(0xff000005)), titleTextStyle: TextStyle(color: Color(0xff000006)), @@ -1125,6 +1194,7 @@ void main() { 'iconTheme: IconThemeData#00000(color: ${const Color(0xff000004)})', 'centerTitle: true', 'titleSpacing: 40.0', + 'leadingWidth: 96.0', 'toolbarHeight: 96.0', 'toolbarTextStyle: TextStyle(inherit: true, color: ${const Color(0xff000005)})', 'titleTextStyle: TextStyle(inherit: true, color: ${const Color(0xff000006)})',