diff --git a/packages/flutter/lib/src/material/flexible_space_bar.dart b/packages/flutter/lib/src/material/flexible_space_bar.dart index cd79d8c396..82a484c1dc 100644 --- a/packages/flutter/lib/src/material/flexible_space_bar.dart +++ b/packages/flutter/lib/src/material/flexible_space_bar.dart @@ -394,7 +394,15 @@ class _FlexibleSpaceBarState extends State { alignment: titleAlignment, child: DefaultTextStyle( style: titleStyle, - child: title, + child: LayoutBuilder( + builder: (BuildContext context, BoxConstraints constraints) { + return Container( + width: constraints.maxWidth / scaleValue, + alignment: titleAlignment, + child: title, + ); + } + ), ), ), ), diff --git a/packages/flutter/test/material/flexible_space_bar_test.dart b/packages/flutter/test/material/flexible_space_bar_test.dart index c70a08db81..431f7a4758 100644 --- a/packages/flutter/test/material/flexible_space_bar_test.dart +++ b/packages/flutter/test/material/flexible_space_bar_test.dart @@ -117,6 +117,57 @@ void main() { expect(clipRect.size.height, minExtent); }); + // This is a regression test for https://github.com/flutter/flutter/issues/14227 + testWidgets('FlexibleSpaceBar sets width constraints for the title', (WidgetTester tester) async { + const double titleFontSize = 20.0; + const double height = 300.0; + double width; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Builder( + builder: (BuildContext context) { + width = MediaQuery.of(context).size.width; + return CustomScrollView( + slivers: [ + SliverAppBar( + expandedHeight: height, + pinned: true, + stretch: true, + flexibleSpace: FlexibleSpaceBar( + titlePadding: EdgeInsets.zero, + title: Text( + 'X' * 2000, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: const TextStyle(fontSize: titleFontSize), + ), + centerTitle: false, + ), + ), + ], + ); + } + ), + ), + ), + ); + + // The title is scaled and transformed to be 1.5 times bigger, when the + // FlexibleSpaceBar is fully expanded, thus we expect the width to be + // 1.5 times smaller than the full width. The height of the text is the same + // as the font size, with 10 dps bottom margin. + expect( + tester.getRect(find.byType(Text)), + Rect.fromLTRB( + 0, + height - titleFontSize - 10, + (width / 1.5).floorToDouble(), + height - 10, + ), + ); + }); + testWidgets('FlexibleSpaceBar test titlePadding defaults', (WidgetTester tester) async { Widget buildFrame(TargetPlatform platform, bool centerTitle) { return MaterialApp(