Skip to content

Commit 5f2addd

Browse files
authored
[widget_preview] Do not leak ListTileTheme into previewed widgets (flutter#194992)
In the widget preview scaffold, `WidgetPreviewGroupWidget` wrapped `ExpansionTile` in a `ListTileTheme`. Because `ListTileTheme` is an `InheritedTheme` (`InheritedWidget`), it enclosed all previewed child widgets. Because `MaterialApp` and `Theme` do not insert a `ListTileTheme` boundary to reset ancestor inherited themes, previewed `ListTile` and `SwitchListTile` widgets inherited the scaffold's brightened `tileColor` (`#201E24`). This removes the `ListTileTheme` and dead `Theme(dividerColor: Colors.transparent)` wrappers around `ExpansionTile`, removes unused `listTileTheme` from the scaffold's `_baseTheme`, and adds integration regression tests in `theming_test.dart`. Fixes flutter#194660 ### Visual Behavior - **Before**: `SwitchListTile` and `ListTile` rendered with an unintended dark tile background (`#201E24`). - **After**: `SwitchListTile` and `ListTile` render with the default transparent background. Previous output: ``` 00:01 +4: Previewed MaterialApp ListTileTheme is not overridden ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following TestFailure was thrown running a test: Expected: MaterialColor:<MaterialColor(primary value: Color(alpha: 1.0000, red: 0.9569, green: 0.2627, blue: 0.2118, colorSpace: ColorSpace.sRGB))> Actual: Color:<Color(alpha: 1.0000, red: 0.1245, green: 0.1171, blue: 0.1394, colorSpace: ColorSpace.sRGB)> ════════════════════════════════════════════════════════════════════════════════════════════════════ 00:02 +4 -1: Previewed MaterialApp ListTileTheme is not overridden [E] 00:02 +4 -1: ListTileTheme does not leak into previewed widgets without MaterialApp ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following TestFailure was thrown running a test: Expected: null Actual: Color:<Color(alpha: 1.0000, red: 0.1245, green: 0.1171, blue: 0.1394, colorSpace: ColorSpace.sRGB)> ════════════════════════════════════════════════════════════════════════════════════════════════════ 00:02 +4 -2: ListTileTheme does not leak into previewed widgets without MaterialApp [E] 00:02 +4 -2: SwitchListTile in preview has transparent background by default ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following TestFailure was thrown running a test: Expected: has `color` with value Color:<Color(alpha: 0.0000, red: 0.0000, green: 0.0000, blue: 0.0000, colorSpace: ColorSpace.extendedSRGB)> Actual: ShapeDecoration:<ShapeDecoration(color: Color(alpha: 1.0000, red: 0.1245, green: 0.1171, blue: 0.1394, colorSpace: ColorSpace.sRGB), shape: RoundedRectangleBorder(BorderSide(none), BorderRadius.circular(12.0)))> Which: has `color` with value Color:<Color(alpha: 1.0000, red: 0.1245, green: 0.1171, blue: 0.1394, colorSpace: ColorSpace.sRGB)> ════════════════════════════════════════════════════════════════════════════════════════════════════ 00:03 +4 -3: SwitchListTile in preview has transparent background by default [E] ``` Current output: ``` 00:00 +0: loading dev/integration_tests/widget_preview_scaffold/test/theming_test.dart 00:00 +0: Theming is correctly propagated down to the previewed widget 00:00 +1: Default theme is used if no preview theme is specified 00:00 +2: BrightnessToggleButton correctly toggles preview brightness 00:01 +3: Updated brightness property doesn't override state set by BrightnessToggleButton 00:01 +4: Previewed MaterialApp ListTileTheme is not overridden 00:01 +5: ListTileTheme does not leak into previewed widgets without MaterialApp 00:01 +6: SwitchListTile in preview has transparent background by default 00:01 +7: All tests passed! ```
1 parent 928815a commit 5f2addd

5 files changed

Lines changed: 147 additions & 106 deletions

File tree

‎dev/integration_tests/widget_preview_scaffold/lib/src/theme/theme.dart‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ ThemeData _baseTheme({
118118
borderRadius: BorderRadius.all(kCardRadius),
119119
),
120120
),
121-
listTileTheme: ListTileThemeData(
122-
dense: true,
123-
tileColor: backgroundColor.brighten(),
124-
shape: RoundedRectangleBorder(
125-
borderRadius: BorderRadius.all(kCardRadius),
126-
),
127-
),
128121
dropdownMenuTheme: DropdownMenuThemeData(textStyle: theme.regularTextStyle),
129122
primaryTextTheme: _devToolsTextTheme(theme, theme.primaryTextTheme),
130123
textTheme: _devToolsTextTheme(theme, theme.textTheme),

‎dev/integration_tests/widget_preview_scaffold/lib/src/widget_preview_rendering.dart‎

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ class WidgetPreviewGroupWidget extends StatefulWidget {
232232
static const _gridSpacing = 8.0;
233233
static const _gridRunSpacing = 8.0;
234234

235-
/// The default radius of a Material 3 `Card`, as per documentation for `Card.shape`.
236-
// TODO(bkonyi): inherit this from the theme.
237-
static const _kCardRadius = Radius.circular(12);
238-
239235
@override
240236
State<WidgetPreviewGroupWidget> createState() =>
241237
_WidgetPreviewGroupWidgetState();
@@ -272,51 +268,36 @@ class _WidgetPreviewGroupWidgetState extends State<WidgetPreviewGroupWidget> {
272268

273269
@override
274270
Widget build(BuildContext context) {
275-
final theme = Theme.of(context);
276271
return Card(
277-
child: ListTileTheme(
278-
data: ListTileTheme.of(context).copyWith(
279-
dense: true,
280-
shape: RoundedRectangleBorder(
281-
borderRadius: BorderRadius.all(
282-
WidgetPreviewGroupWidget._kCardRadius,
272+
child: ExpansionTile(
273+
key: PageStorageKey(widget.group.name),
274+
title: Text(widget.group.name),
275+
dense: true,
276+
initiallyExpanded: true,
277+
children: [
278+
// Wrap children in a PageStorage to create a storage boundary.
279+
// Without this, descendant scrollables (which search ancestor
280+
// elements for a PageStorageKey when restoring scroll offset)
281+
// inherit the PageStorageKey from the ExpansionTile and attempt
282+
// to read the ExpansionTile's boolean expansion state as a double,
283+
// throwing a TypeError (see https://github.com/flutter/flutter/issues/191242).
284+
PageStorage(
285+
bucket: _bucket,
286+
child: ValueListenableBuilder<LayoutType>(
287+
valueListenable: widget.controller.layoutTypeListenable,
288+
builder: (context, selectedLayout, _) {
289+
return switch (selectedLayout) {
290+
LayoutType.gridView => _buildGridViewFlex(
291+
widget.group.previews,
292+
),
293+
LayoutType.listView => _buildVerticalListView(
294+
widget.group.previews,
295+
),
296+
};
297+
},
283298
),
284299
),
285-
),
286-
child: Theme(
287-
// Prevents divider lines appearing at the top and bottom of the
288-
// expanded ExpansionTile.
289-
data: theme.copyWith(dividerColor: Colors.transparent),
290-
child: ExpansionTile(
291-
key: PageStorageKey(widget.group.name),
292-
title: Text(widget.group.name),
293-
initiallyExpanded: true,
294-
children: [
295-
// Wrap children in a PageStorage to create a storage boundary.
296-
// Without this, descendant scrollables (which search ancestor
297-
// elements for a PageStorageKey when restoring scroll offset)
298-
// inherit the PageStorageKey from the ExpansionTile and attempt
299-
// to read the ExpansionTile's boolean expansion state as a double,
300-
// throwing a TypeError (see https://github.com/flutter/flutter/issues/191242).
301-
PageStorage(
302-
bucket: _bucket,
303-
child: ValueListenableBuilder<LayoutType>(
304-
valueListenable: widget.controller.layoutTypeListenable,
305-
builder: (context, selectedLayout, _) {
306-
return switch (selectedLayout) {
307-
LayoutType.gridView => _buildGridViewFlex(
308-
widget.group.previews,
309-
),
310-
LayoutType.listView => _buildVerticalListView(
311-
widget.group.previews,
312-
),
313-
};
314-
},
315-
),
316-
),
317-
],
318-
),
319-
),
300+
],
320301
),
321302
);
322303
}

‎dev/integration_tests/widget_preview_scaffold/test/theming_test.dart‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,97 @@ void main() {
307307
expectDarkMode();
308308
},
309309
);
310+
311+
Future<void> pumpPreview(WidgetTester tester, WidgetBuilder builder) async {
312+
final controller = FakeWidgetPreviewScaffoldController(
313+
previews: [
314+
WidgetPreview.test(
315+
builder: () => Builder(builder: builder),
316+
previewData: Preview(),
317+
),
318+
],
319+
);
320+
321+
await controller.initialize();
322+
if (controller.filterBySelectedFileListenable.value) {
323+
await controller.toggleFilterBySelectedFile();
324+
}
325+
326+
await tester.pumpWidget(TestWidgetPreviewScaffold(controller: controller));
327+
await tester.pumpAndSettle();
328+
}
329+
330+
testWidgets('Previewed MaterialApp ListTileTheme is not overridden', (
331+
tester,
332+
) async {
333+
Color? tileColor;
334+
335+
await pumpPreview(
336+
tester,
337+
(_) => MaterialApp(
338+
theme: ThemeData(
339+
listTileTheme: const ListTileThemeData(tileColor: Colors.red),
340+
),
341+
home: Builder(
342+
builder: (context) {
343+
tileColor = ListTileTheme.of(context).tileColor;
344+
return const SizedBox();
345+
},
346+
),
347+
),
348+
);
349+
350+
expect(tileColor, Colors.red);
351+
});
352+
353+
testWidgets(
354+
'ListTileTheme does not leak into previewed widgets without MaterialApp',
355+
(tester) async {
356+
bool builderRan = false;
357+
Color? inheritedTileColor;
358+
359+
await pumpPreview(tester, (context) {
360+
builderRan = true;
361+
inheritedTileColor = ListTileTheme.of(context).tileColor;
362+
return const SizedBox();
363+
});
364+
365+
expect(builderRan, isTrue);
366+
expect(inheritedTileColor, isNull);
367+
},
368+
);
369+
370+
testWidgets(
371+
'SwitchListTile in preview has transparent background by default',
372+
(tester) async {
373+
await pumpPreview(
374+
tester,
375+
(_) => MaterialApp(
376+
home: Scaffold(
377+
backgroundColor: Colors.green,
378+
body: Center(child: SwitchListTile(value: true, onChanged: (_) {})),
379+
),
380+
),
381+
);
382+
383+
final switchListTileFinder = find.byType(SwitchListTile);
384+
expect(switchListTileFinder, findsOneWidget);
385+
386+
final inkFinder = find.descendant(
387+
of: switchListTileFinder,
388+
matching: find.byType(Ink),
389+
);
390+
expect(inkFinder, findsOneWidget);
391+
392+
final ink = tester.widget<Ink>(inkFinder);
393+
expect(
394+
ink.decoration,
395+
isA<ShapeDecoration>().having(
396+
(decoration) => decoration.color,
397+
'color',
398+
Colors.transparent,
399+
),
400+
);
401+
},
402+
);
310403
}

‎packages/flutter_tools/templates/widget_preview_scaffold/lib/src/theme/theme.dart.tmpl‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ ThemeData _baseTheme({
118118
borderRadius: BorderRadius.all(kCardRadius),
119119
),
120120
),
121-
listTileTheme: ListTileThemeData(
122-
dense: true,
123-
tileColor: backgroundColor.brighten(),
124-
shape: RoundedRectangleBorder(
125-
borderRadius: BorderRadius.all(kCardRadius),
126-
),
127-
),
128121
dropdownMenuTheme: DropdownMenuThemeData(textStyle: theme.regularTextStyle),
129122
primaryTextTheme: _devToolsTextTheme(theme, theme.primaryTextTheme),
130123
textTheme: _devToolsTextTheme(theme, theme.textTheme),

‎packages/flutter_tools/templates/widget_preview_scaffold/lib/src/widget_preview_rendering.dart.tmpl‎

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ class WidgetPreviewGroupWidget extends StatefulWidget {
232232
static const _gridSpacing = 8.0;
233233
static const _gridRunSpacing = 8.0;
234234

235-
/// The default radius of a Material 3 `Card`, as per documentation for `Card.shape`.
236-
// TODO(bkonyi): inherit this from the theme.
237-
static const _kCardRadius = Radius.circular(12);
238-
239235
@override
240236
State<WidgetPreviewGroupWidget> createState() =>
241237
_WidgetPreviewGroupWidgetState();
@@ -272,51 +268,36 @@ class _WidgetPreviewGroupWidgetState extends State<WidgetPreviewGroupWidget> {
272268

273269
@override
274270
Widget build(BuildContext context) {
275-
final theme = Theme.of(context);
276271
return Card(
277-
child: ListTileTheme(
278-
data: ListTileTheme.of(context).copyWith(
279-
dense: true,
280-
shape: RoundedRectangleBorder(
281-
borderRadius: BorderRadius.all(
282-
WidgetPreviewGroupWidget._kCardRadius,
272+
child: ExpansionTile(
273+
key: PageStorageKey(widget.group.name),
274+
title: Text(widget.group.name),
275+
dense: true,
276+
initiallyExpanded: true,
277+
children: [
278+
// Wrap children in a PageStorage to create a storage boundary.
279+
// Without this, descendant scrollables (which search ancestor
280+
// elements for a PageStorageKey when restoring scroll offset)
281+
// inherit the PageStorageKey from the ExpansionTile and attempt
282+
// to read the ExpansionTile's boolean expansion state as a double,
283+
// throwing a TypeError (see https://github.com/flutter/flutter/issues/191242).
284+
PageStorage(
285+
bucket: _bucket,
286+
child: ValueListenableBuilder<LayoutType>(
287+
valueListenable: widget.controller.layoutTypeListenable,
288+
builder: (context, selectedLayout, _) {
289+
return switch (selectedLayout) {
290+
LayoutType.gridView => _buildGridViewFlex(
291+
widget.group.previews,
292+
),
293+
LayoutType.listView => _buildVerticalListView(
294+
widget.group.previews,
295+
),
296+
};
297+
},
283298
),
284299
),
285-
),
286-
child: Theme(
287-
// Prevents divider lines appearing at the top and bottom of the
288-
// expanded ExpansionTile.
289-
data: theme.copyWith(dividerColor: Colors.transparent),
290-
child: ExpansionTile(
291-
key: PageStorageKey(widget.group.name),
292-
title: Text(widget.group.name),
293-
initiallyExpanded: true,
294-
children: [
295-
// Wrap children in a PageStorage to create a storage boundary.
296-
// Without this, descendant scrollables (which search ancestor
297-
// elements for a PageStorageKey when restoring scroll offset)
298-
// inherit the PageStorageKey from the ExpansionTile and attempt
299-
// to read the ExpansionTile's boolean expansion state as a double,
300-
// throwing a TypeError (see https://github.com/flutter/flutter/issues/191242).
301-
PageStorage(
302-
bucket: _bucket,
303-
child: ValueListenableBuilder<LayoutType>(
304-
valueListenable: widget.controller.layoutTypeListenable,
305-
builder: (context, selectedLayout, _) {
306-
return switch (selectedLayout) {
307-
LayoutType.gridView => _buildGridViewFlex(
308-
widget.group.previews,
309-
),
310-
LayoutType.listView => _buildVerticalListView(
311-
widget.group.previews,
312-
),
313-
};
314-
},
315-
),
316-
),
317-
],
318-
),
319-
),
300+
],
320301
),
321302
);
322303
}

0 commit comments

Comments
 (0)