Add width constraints for FlexibleSpaceBar.title in its expanded state, so that overflow of long titles can be handled (#51335)

This commit is contained in:
Per Classon 2020-03-09 20:06:01 +01:00 committed by GitHub
parent 31df1d459e
commit 1546f41ec1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 1 deletions

View File

@ -394,7 +394,15 @@ class _FlexibleSpaceBarState extends State<FlexibleSpaceBar> {
alignment: titleAlignment, alignment: titleAlignment,
child: DefaultTextStyle( child: DefaultTextStyle(
style: titleStyle, style: titleStyle,
child: title, child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
width: constraints.maxWidth / scaleValue,
alignment: titleAlignment,
child: title,
);
}
),
), ),
), ),
), ),

View File

@ -117,6 +117,57 @@ void main() {
expect(clipRect.size.height, minExtent); 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: <Widget>[
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 { testWidgets('FlexibleSpaceBar test titlePadding defaults', (WidgetTester tester) async {
Widget buildFrame(TargetPlatform platform, bool centerTitle) { Widget buildFrame(TargetPlatform platform, bool centerTitle) {
return MaterialApp( return MaterialApp(