From 78a0d3d4e2f600d754179f199aaa5681634e3937 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Wed, 25 May 2022 17:28:12 +0300 Subject: [PATCH] [reland] Migrate `ListTile` TextTheme TextStyle references to Material 3 (#102167) --- .../flutter/lib/src/material/list_tile.dart | 14 +- .../flutter/test/material/list_tile_test.dart | 155 +++++++++++++++++- 2 files changed, 156 insertions(+), 13 deletions(-) diff --git a/packages/flutter/lib/src/material/list_tile.dart b/packages/flutter/lib/src/material/list_tile.dart index 972559f474..7fd627bc20 100644 --- a/packages/flutter/lib/src/material/list_tile.dart +++ b/packages/flutter/lib/src/material/list_tile.dart @@ -657,10 +657,10 @@ class ListTile extends StatelessWidget { final TextStyle textStyle; switch(style ?? tileTheme.style ?? theme.listTileTheme.style ?? ListTileStyle.list) { case ListTileStyle.drawer: - textStyle = theme.textTheme.bodyText1!; + textStyle = theme.useMaterial3 ? theme.textTheme.bodyMedium! : theme.textTheme.bodyText1!; break; case ListTileStyle.list: - textStyle = theme.textTheme.subtitle1!; + textStyle = theme.useMaterial3 ? theme.textTheme.titleMedium! : theme.textTheme.subtitle1!; break; } final Color? color = _textColor(theme, tileTheme, textStyle.color); @@ -670,15 +670,19 @@ class ListTile extends StatelessWidget { } TextStyle _subtitleTextStyle(ThemeData theme, ListTileThemeData tileTheme) { - final TextStyle textStyle = theme.textTheme.bodyText2!; - final Color? color = _textColor(theme, tileTheme, theme.textTheme.caption!.color); + final TextStyle textStyle = theme.useMaterial3 ? theme.textTheme.bodyMedium! : theme.textTheme.bodyText2!; + final Color? color = _textColor( + theme, + tileTheme, + theme.useMaterial3 ? theme.textTheme.bodySmall!.color : theme.textTheme.caption!.color, + ); return _isDenseLayout(theme, tileTheme) ? textStyle.copyWith(color: color, fontSize: 12.0) : textStyle.copyWith(color: color); } TextStyle _trailingAndLeadingTextStyle(ThemeData theme, ListTileThemeData tileTheme) { - final TextStyle textStyle = theme.textTheme.bodyText2!; + final TextStyle textStyle = theme.useMaterial3 ? theme.textTheme.bodyMedium! : theme.textTheme.bodyText2!; final Color? color = _textColor(theme, tileTheme, textStyle.color); return textStyle.copyWith(color: color); } diff --git a/packages/flutter/test/material/list_tile_test.dart b/packages/flutter/test/material/list_tile_test.dart index c5a91e0220..e543527362 100644 --- a/packages/flutter/test/material/list_tile_test.dart +++ b/packages/flutter/test/material/list_tile_test.dart @@ -2104,6 +2104,7 @@ void main() { ListTileStyle? style, }) { return MaterialApp( + theme: ThemeData(useMaterial3: true), home: Material( child: Center( child: Builder( @@ -2181,6 +2182,7 @@ void main() { ListTileStyle? style, }) { return MaterialApp( + theme: ThemeData(useMaterial3: true), home: Material( child: Center( child: Builder( @@ -2207,25 +2209,25 @@ void main() { // ListTile - ListTileStyle.list (default). await tester.pumpWidget(buildFrame()); RenderParagraph leading = _getTextRenderObject(tester, 'leading'); - expect(leading.text.style!.color, theme.textTheme.bodyText2!.color); + expect(leading.text.style!.color, theme.textTheme.bodyMedium!.color); RenderParagraph title = _getTextRenderObject(tester, 'title'); - expect(title.text.style!.color, theme.textTheme.subtitle1!.color); + expect(title.text.style!.color, theme.textTheme.titleMedium!.color); RenderParagraph subtitle = _getTextRenderObject(tester, 'subtitle'); - expect(subtitle.text.style!.color, theme.textTheme.caption!.color); + expect(subtitle.text.style!.color, theme.textTheme.bodySmall!.color); RenderParagraph trailing = _getTextRenderObject(tester, 'trailing'); - expect(trailing.text.style!.color, theme.textTheme.bodyText2!.color); + expect(trailing.text.style!.color, theme.textTheme.bodyMedium!.color); // ListTile - ListTileStyle.drawer. await tester.pumpWidget(buildFrame(style: ListTileStyle.drawer)); await tester.pumpAndSettle(); leading = _getTextRenderObject(tester, 'leading'); - expect(leading.text.style!.color, theme.textTheme.bodyText2!.color); + expect(leading.text.style!.color, theme.textTheme.bodyMedium!.color); title = _getTextRenderObject(tester, 'title'); - expect(title.text.style!.color, theme.textTheme.bodyText1!.color); + expect(title.text.style!.color, theme.textTheme.bodyLarge!.color); subtitle = _getTextRenderObject(tester, 'subtitle'); - expect(subtitle.text.style!.color, theme.textTheme.caption!.color); + expect(subtitle.text.style!.color, theme.textTheme.bodySmall!.color); trailing = _getTextRenderObject(tester, 'trailing'); - expect(trailing.text.style!.color, theme.textTheme.bodyText2!.color); + expect(trailing.text.style!.color, theme.textTheme.bodyMedium!.color); }); testWidgets('Default ListTile debugFillProperties', (WidgetTester tester) async { @@ -2299,6 +2301,143 @@ void main() { expect(description[22], 'minVerticalPadding: 2.0'); expect(description[23], 'minLeadingWidth: 6.0'); }); + + group('Material 2', () { + // Tests that are only relevant for Material 2. Once ThemeData.useMaterial3 + // is turned on by default, these tests can be removed. + + testWidgets('ListTile font size', (WidgetTester tester) async { + Widget buildFrame({ + bool dense = false, + bool enabled = true, + bool selected = false, + ListTileStyle? style, + }) { + return MaterialApp( + home: Material( + child: Center( + child: Builder( + builder: (BuildContext context) { + return ListTile( + dense: dense, + enabled: enabled, + selected: selected, + style: style, + leading: const TestText('leading'), + title: const TestText('title'), + subtitle: const TestText('subtitle') , + trailing: const TestText('trailing'), + ); + }, + ), + ), + ), + ); + } + + // ListTile - ListTileStyle.list (default). + await tester.pumpWidget(buildFrame()); + RenderParagraph leading = _getTextRenderObject(tester, 'leading'); + expect(leading.text.style!.fontSize, 14.0); + RenderParagraph title = _getTextRenderObject(tester, 'title'); + expect(title.text.style!.fontSize, 16.0); + RenderParagraph subtitle = _getTextRenderObject(tester, 'subtitle'); + expect(subtitle.text.style!.fontSize, 14.0); + RenderParagraph trailing = _getTextRenderObject(tester, 'trailing'); + expect(trailing.text.style!.fontSize, 14.0); + + // ListTile - Densed - ListTileStyle.list (default). + await tester.pumpWidget(buildFrame(dense: true)); + await tester.pumpAndSettle(); + leading = _getTextRenderObject(tester, 'leading'); + expect(leading.text.style!.fontSize, 14.0); + title = _getTextRenderObject(tester, 'title'); + expect(title.text.style!.fontSize, 13.0); + subtitle = _getTextRenderObject(tester, 'subtitle'); + expect(subtitle.text.style!.fontSize, 12.0); + trailing = _getTextRenderObject(tester, 'trailing'); + expect(trailing.text.style!.fontSize, 14.0); + + // ListTile - ListTileStyle.drawer. + await tester.pumpWidget(buildFrame(style: ListTileStyle.drawer)); + await tester.pumpAndSettle(); + leading = _getTextRenderObject(tester, 'leading'); + expect(leading.text.style!.fontSize, 14.0); + title = _getTextRenderObject(tester, 'title'); + expect(title.text.style!.fontSize, 14.0); + subtitle = _getTextRenderObject(tester, 'subtitle'); + expect(subtitle.text.style!.fontSize, 14.0); + trailing = _getTextRenderObject(tester, 'trailing'); + expect(trailing.text.style!.fontSize, 14.0); + + // ListTile - Densed - ListTileStyle.drawer. + await tester.pumpWidget(buildFrame(dense: true, style: ListTileStyle.drawer)); + await tester.pumpAndSettle(); + leading = _getTextRenderObject(tester, 'leading'); + expect(leading.text.style!.fontSize, 14.0); + title = _getTextRenderObject(tester, 'title'); + expect(title.text.style!.fontSize, 13.0); + subtitle = _getTextRenderObject(tester, 'subtitle'); + expect(subtitle.text.style!.fontSize, 12.0); + trailing = _getTextRenderObject(tester, 'trailing'); + expect(trailing.text.style!.fontSize, 14.0); + }); + + testWidgets('ListTile text color', (WidgetTester tester) async { + Widget buildFrame({ + bool dense = false, + bool enabled = true, + bool selected = false, + ListTileStyle? style, + }) { + return MaterialApp( + home: Material( + child: Center( + child: Builder( + builder: (BuildContext context) { + return ListTile( + dense: dense, + enabled: enabled, + selected: selected, + style: style, + leading: const TestText('leading'), + title: const TestText('title'), + subtitle: const TestText('subtitle') , + trailing: const TestText('trailing'), + ); + }, + ), + ), + ), + ); + } + + final ThemeData theme = ThemeData(); + + // ListTile - ListTileStyle.list (default). + await tester.pumpWidget(buildFrame()); + RenderParagraph leading = _getTextRenderObject(tester, 'leading'); + expect(leading.text.style!.color, theme.textTheme.bodyText2!.color); + RenderParagraph title = _getTextRenderObject(tester, 'title'); + expect(title.text.style!.color, theme.textTheme.subtitle1!.color); + RenderParagraph subtitle = _getTextRenderObject(tester, 'subtitle'); + expect(subtitle.text.style!.color, theme.textTheme.caption!.color); + RenderParagraph trailing = _getTextRenderObject(tester, 'trailing'); + expect(trailing.text.style!.color, theme.textTheme.bodyText2!.color); + + // ListTile - ListTileStyle.drawer. + await tester.pumpWidget(buildFrame(style: ListTileStyle.drawer)); + await tester.pumpAndSettle(); + leading = _getTextRenderObject(tester, 'leading'); + expect(leading.text.style!.color, theme.textTheme.bodyText2!.color); + title = _getTextRenderObject(tester, 'title'); + expect(title.text.style!.color, theme.textTheme.subtitle1!.color); + subtitle = _getTextRenderObject(tester, 'subtitle'); + expect(subtitle.text.style!.color, theme.textTheme.caption!.color); + trailing = _getTextRenderObject(tester, 'trailing'); + expect(trailing.text.style!.color, theme.textTheme.bodyText2!.color); + }); + }); } RenderParagraph _getTextRenderObject(WidgetTester tester, String text) {