Skip to content

Commit

Permalink
[ListTile] adds new properties to customize the tile color (flutter#6…
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushBherwani1998 authored and Pragya007 committed Aug 5, 2020
1 parent 54a9796 commit 9d66126
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 15 deletions.
55 changes: 40 additions & 15 deletions packages/flutter/lib/src/material/list_tile.dart
Expand Up @@ -658,6 +658,8 @@ class ListTile extends StatelessWidget {
this.hoverColor,
this.focusNode,
this.autofocus = false,
this.tileColor,
this.selectedTileColor,
}) : assert(isThreeLine != null),
assert(enabled != null),
assert(selected != null),
Expand Down Expand Up @@ -808,6 +810,16 @@ class ListTile extends StatelessWidget {
/// {@macro flutter.widgets.Focus.autofocus}
final bool autofocus;

/// Defines the background color of `ListTile when [selected] is false.
///
/// By default, the value of `tileColor` is [Colors.transparent].
final Color tileColor;

/// Defines the background color of `ListTile` when [selected] is true.
///
/// By default, the value of `selectedListColor` is [Colors.transparent].
final Color selectedTileColor;

/// Add a one pixel border in between each tile. If color isn't specified the
/// [ThemeData.dividerColor] of the context's [Theme] is used.
///
Expand Down Expand Up @@ -913,6 +925,16 @@ class ListTile extends StatelessWidget {
: style.copyWith(color: color);
}

Color _tileBackgroundColor() {
if (!selected && tileColor != null)
return tileColor;

if (selected && selectedTileColor != null)
return selectedTileColor;

return Colors.transparent;
}

@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
Expand Down Expand Up @@ -984,21 +1006,24 @@ class ListTile extends StatelessWidget {
child: Semantics(
selected: selected,
enabled: enabled,
child: SafeArea(
top: false,
bottom: false,
minimum: resolvedContentPadding,
child: _ListTile(
leading: leadingIcon,
title: titleText,
subtitle: subtitleText,
trailing: trailingIcon,
isDense: _isDenseLayout(tileTheme),
visualDensity: visualDensity ?? theme.visualDensity,
isThreeLine: isThreeLine,
textDirection: textDirection,
titleBaselineType: titleStyle.textBaseline,
subtitleBaselineType: subtitleStyle?.textBaseline,
child: ColoredBox(
color: _tileBackgroundColor(),
child: SafeArea(
top: false,
bottom: false,
minimum: resolvedContentPadding,
child: _ListTile(
leading: leadingIcon,
title: titleText,
subtitle: subtitleText,
trailing: trailingIcon,
isDense: _isDenseLayout(tileTheme),
visualDensity: visualDensity ?? theme.visualDensity,
isThreeLine: isThreeLine,
textDirection: textDirection,
titleBaselineType: titleStyle.textBaseline,
subtitleBaselineType: subtitleStyle?.textBaseline,
),
),
),
),
Expand Down
76 changes: 76 additions & 0 deletions packages/flutter/test/material/list_tile_test.dart
Expand Up @@ -1534,4 +1534,80 @@ void main() {

expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
});

testWidgets('ListTile respects tileColor & selectedTileColor', (WidgetTester tester) async {
bool isSelected = false;
const Color selectedTileColor = Colors.red;
const Color tileColor = Colors.green;

await tester.pumpWidget(
MaterialApp(
home: Material(
child: Center(
child: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return ListTile(
selected: isSelected,
selectedTileColor: selectedTileColor,
tileColor: tileColor,
onTap: () {
setState(()=> isSelected = !isSelected);
},
title: const Text('Title'),
);
},
),
),
),
),
);

// Initially, when isSelected is false, the ListTile should respect tileColor.
ColoredBox coloredBox = tester.widget(find.byType(ColoredBox));
expect(coloredBox.color, tileColor);

// Tap on tile to change isSelected.
await tester.tap(find.byType(ListTile));
await tester.pumpAndSettle();

// When isSelected is true, the ListTile should respect selectedTileColor.
coloredBox = tester.widget(find.byType(ColoredBox));
expect(coloredBox.color, selectedTileColor);
});

testWidgets('ListTile default tile color', (WidgetTester tester) async {
bool isSelected = false;
const Color defaultColor = Colors.transparent;

await tester.pumpWidget(
MaterialApp(
home: Material(
child: Center(
child: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return ListTile(
selected: isSelected,
onTap: () {
setState(()=> isSelected = !isSelected);
},
title: const Text('Title'),
);
},
),
),
),
),
);

ColoredBox coloredBox = tester.widget(find.byType(ColoredBox));
expect(coloredBox.color, defaultColor);

// Tap on tile to change isSelected.
await tester.tap(find.byType(ListTile));
await tester.pumpAndSettle();

coloredBox = tester.widget(find.byType(ColoredBox));
expect(isSelected, isTrue);
expect(coloredBox.color, defaultColor);
});
}

0 comments on commit 9d66126

Please sign in to comment.