From 6e1d4f9fc6dce82549c237ead562cb3a283c1e73 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Tue, 25 Apr 2023 11:27:24 +0300 Subject: [PATCH] Fix `SliverAppBar.medium` & `SliverAppBar.large` text scale (#125038) fixes https://github.com/flutter/flutter/issues/114340
code sample ```dart import 'package:flutter/material.dart'; /// Flutter code sample for [SliverAppBar.medium]. void main() { runApp(const AppBarMediumApp()); } class AppBarMediumApp extends StatelessWidget { const AppBarMediumApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( useMaterial3: true, ), home: MediaQuery( data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0), child: Material( child: CustomScrollView( slivers: [ SliverAppBar.medium( leading: IconButton(icon: const Icon(Icons.menu), onPressed: () {}), title: const Text('Medium App Bar'), actions: [ IconButton(icon: const Icon(Icons.more_vert), onPressed: () {}), ], ), // SliverAppBar.large( // leading: // IconButton(icon: const Icon(Icons.menu), onPressed: () {}), // title: const Text('Large App Bar'), // actions: [ // IconButton( // icon: const Icon(Icons.more_vert), onPressed: () {}), // ], // ), // Just some content big enough to have something to scroll. SliverToBoxAdapter( child: Card( child: SizedBox( height: 1200, child: Padding( padding: const EdgeInsets.fromLTRB(8, 100, 8, 100), child: Text( 'Here be scrolling content...', style: Theme.of(context).textTheme.headlineSmall, ), ), ), ), ), ], ), ), ), ); } } ```
### Before | Medium App Bar - `textScaleFactor: 3.0` | Large App Bar - `textScaleFactor.30` | | --------------- | --------------- | | | | ### After | Medium App Bar - `textScaleFactor: 3.0` | Large App Bar - `textScaleFactor.30` | | --------------- | --------------- | | | | --- .../flutter/lib/src/material/app_bar.dart | 48 +++-- .../flutter/test/material/app_bar_test.dart | 185 ++++++++++++++++++ 2 files changed, 214 insertions(+), 19 deletions(-) diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index dbaf036bcf..ce3b9c4769 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -2101,10 +2101,11 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget { late final ThemeData theme = Theme.of(context); late final AppBarTheme appBarTheme = AppBarTheme.of(context); final AppBarTheme defaults = theme.useMaterial3 ? _AppBarDefaultsM3(context) : _AppBarDefaultsM2(context); + final double textScaleFactor = math.min(MediaQuery.textScaleFactorOf(context), _kMaxTitleTextScaleFactor); // TODO(tahatesser): Add link to Material spec when available, https://github.com/flutter/flutter/issues/58769. final FlexibleSpaceBarSettings settings = context.dependOnInheritedWidgetOfExactType()!; final double topPadding = primary ? MediaQuery.viewPaddingOf(context).top : 0; final double collapsedHeight = settings.minExtent - topPadding - bottomHeight; - final double scrollUnderHeight = settings.maxExtent - settings.minExtent + bottomHeight; + final double scrollUnderHeight = settings.maxExtent - (settings.minExtent / textScaleFactor) + bottomHeight; final _ScrollUnderFlexibleConfig config; switch (variant) { case _ScrollUnderFlexibleVariant.medium: @@ -2144,31 +2145,40 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget { padding.left, 0, padding.right, - adjustBottomPadding, + adjustBottomPadding / textScaleFactor, ); } return padding; } - return Column( - children: [ - Padding( - padding: EdgeInsets.only(top: collapsedHeight + topPadding), - ), - Flexible( - child: ClipRect( - child: OverflowBox( - minHeight: scrollUnderHeight, - maxHeight: scrollUnderHeight, - alignment: Alignment.bottomLeft, - child: Container( - alignment: AlignmentDirectional.bottomStart, - padding: expandedTitlePadding(), - child: expandedTitle, + + // Set maximum text scale factor to [_kMaxTitleTextScaleFactor] for the + // title to keep the visual hierarchy the same even with larger font + // sizes. To opt out, wrap the [title] widget in a [MediaQuery] widget + // with [MediaQueryData.textScaleFactor] set to + // `MediaQuery.textScaleFactorOf(context)`. + return MediaQuery( + data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor), + child: Column( + children: [ + Padding( + padding: EdgeInsets.only(top: collapsedHeight + topPadding), + ), + Flexible( + child: ClipRect( + child: OverflowBox( + minHeight: scrollUnderHeight, + maxHeight: scrollUnderHeight, + alignment: Alignment.bottomLeft, + child: Container( + alignment: AlignmentDirectional.bottomStart, + padding: expandedTitlePadding(), + child: expandedTitle, + ), ), ), ), - ), - ], + ], + ), ); } } diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index 0d87fc0041..387ba51980 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_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/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; @@ -4649,6 +4650,190 @@ void main() { expect(appBarHeight(tester), collapsedAppBarHeight + bottomHeight); }); + testWidgets('SliverAppBar.medium expanded title has upper limit on text scaling', (WidgetTester tester) async { + const String title = 'Medium AppBar'; + Widget buildAppBar({double textScaleFactor = 1.0}) { + return MaterialApp( + theme: ThemeData(useMaterial3: true), + home: MediaQuery( + data: MediaQueryData(textScaleFactor: textScaleFactor), + child: Material( + child: CustomScrollView( + slivers: [ + const SliverAppBar.medium( + title: Text(title), + ), + SliverToBoxAdapter( + child: Container( + height: 1200, + color: Colors.orange[400], + ), + ), + ], + ), + ), + ), + ); + } + + await tester.pumpWidget(buildAppBar()); + + final Finder expandedTitle = find.text(title).first; + expect(tester.getRect(expandedTitle).height, closeTo(31.9, 0.1)); + + await tester.pumpWidget(buildAppBar(textScaleFactor: 2.0)); + expect(tester.getRect(expandedTitle).height, closeTo(43.0, 0.1)); + + await tester.pumpWidget(buildAppBar(textScaleFactor: 3.0)); + expect(tester.getRect(expandedTitle).height, closeTo(43.0, 0.1)); + }); + + testWidgets('SliverAppBar.large expanded title has upper limit on text scaling', (WidgetTester tester) async { + const String title = 'Large AppBar'; + Widget buildAppBar({double textScaleFactor = 1.0}) { + return MaterialApp( + theme: ThemeData(useMaterial3: true), + home: MediaQuery( + data: MediaQueryData(textScaleFactor: textScaleFactor), + child: Material( + child: CustomScrollView( + slivers: [ + const SliverAppBar.large( + title: Text(title), + ), + SliverToBoxAdapter( + child: Container( + height: 1200, + color: Colors.orange[400], + ), + ), + ], + ), + ), + ), + ); + } + + await tester.pumpWidget(buildAppBar()); + + // TODO(tahatesser): https://github.com/flutter/flutter/issues/99933 + // A bug in the HTML renderer and/or Chrome 96+ causes a + // discrepancy in the paragraph height. + const bool hasIssue99933 = kIsWeb && !bool.fromEnvironment('FLUTTER_WEB_USE_SKIA'); + final Finder expandedTitle = find.text(title).first; + expect( + tester.getRect(expandedTitle).height, + closeTo( hasIssue99933 ? 37.0 : 36.0, 0.1), + ); + + await tester.pumpWidget(buildAppBar(textScaleFactor: 2.0)); + expect(tester.getRect(expandedTitle).height, closeTo(48.0, 0.1)); + + await tester.pumpWidget(buildAppBar(textScaleFactor: 3.0)); + expect(tester.getRect(expandedTitle).height, closeTo(48.0, 0.1)); + }); + + testWidgets('SliverAppBar.medium expanded title position is adjusted with textScaleFactor', (WidgetTester tester) async { + const String title = 'Medium AppBar'; + Widget buildAppBar({double textScaleFactor = 1.0}) { + return MaterialApp( + theme: ThemeData(useMaterial3: true), + home: MediaQuery( + data: MediaQueryData(textScaleFactor: textScaleFactor), + child: Material( + child: CustomScrollView( + slivers: [ + const SliverAppBar.medium( + title: Text(title), + ), + SliverToBoxAdapter( + child: Container( + height: 1200, + color: Colors.orange[400], + ), + ), + ], + ), + ), + ), + ); + } + + await tester.pumpWidget(buildAppBar()); + + final Finder expandedTitle = find.text(title).first; + Offset titleTop = tester.getTopLeft(expandedTitle); + expect(titleTop.dy, 64.0); + Offset titleBottom = tester.getBottomLeft(expandedTitle); + expect(titleBottom.dy, closeTo(96.0, 0.1)); + + await tester.pumpWidget(buildAppBar(textScaleFactor: 2.0)); + + titleTop = tester.getTopLeft(expandedTitle); + expect(titleTop.dy, closeTo(57.0, 0.1)); + titleBottom = tester.getBottomLeft(expandedTitle); + expect(titleBottom.dy, closeTo(100.0, 0.1)); + + await tester.pumpWidget(buildAppBar(textScaleFactor: 3.0)); + + titleTop = tester.getTopLeft(expandedTitle); + expect(titleTop.dy, closeTo(57.0, 0.1)); + titleBottom = tester.getBottomLeft(expandedTitle); + expect(titleBottom.dy, closeTo(100.0, 0.1)); + }); + + testWidgets('SliverAppBar.large expanded title position is adjusted with textScaleFactor', (WidgetTester tester) async { + const String title = 'Large AppBar'; + Widget buildAppBar({double textScaleFactor = 1.0}) { + return MaterialApp( + theme: ThemeData(useMaterial3: true), + home: MediaQuery( + data: MediaQueryData(textScaleFactor: textScaleFactor), + child: Material( + child: CustomScrollView( + slivers: [ + const SliverAppBar.large( + title: Text(title), + ), + SliverToBoxAdapter( + child: Container( + height: 1200, + color: Colors.orange[400], + ), + ), + ], + ), + ), + ), + ); + } + + await tester.pumpWidget(buildAppBar()); + // TODO(tahatesser): https://github.com/flutter/flutter/issues/99933 + // A bug in the HTML renderer and/or Chrome 96+ causes a + // discrepancy in the paragraph height. + const bool hasIssue99933 = kIsWeb && !bool.fromEnvironment('FLUTTER_WEB_USE_SKIA'); + final Finder expandedTitle = find.text(title).first; + Offset titleTop = tester.getTopLeft(expandedTitle); + expect(titleTop.dy, closeTo(hasIssue99933 ? 91.0 : 92.0, 0.1)); + Offset titleBottom = tester.getBottomLeft(expandedTitle); + expect(titleBottom.dy, closeTo(128.0, 0.1)); + + await tester.pumpWidget(buildAppBar(textScaleFactor: 2.0)); + + titleTop = tester.getTopLeft(expandedTitle); + expect(titleTop.dy, closeTo(86.1, 0.1)); + titleBottom = tester.getBottomLeft(expandedTitle); + expect(titleBottom.dy, closeTo(134.1, 0.1)); + + await tester.pumpWidget(buildAppBar(textScaleFactor: 3.0)); + + titleTop = tester.getTopLeft(expandedTitle); + expect(titleTop.dy, closeTo(86.1, 0.1)); + titleBottom = tester.getBottomLeft(expandedTitle); + expect(titleBottom.dy, closeTo(134.1, 0.1)); + }); + group('AppBar.forceMaterialTransparency', () { Material getAppBarMaterial(WidgetTester tester) { return tester.widget(find