Unverified Commit 9cb7ac13 authored by Ayush Bherwani's avatar Ayush Bherwani Committed by GitHub

[ListTile] adds new properties to customize the tile color (#61347)

parent 5c9d09f0
...@@ -658,6 +658,8 @@ class ListTile extends StatelessWidget { ...@@ -658,6 +658,8 @@ class ListTile extends StatelessWidget {
this.hoverColor, this.hoverColor,
this.focusNode, this.focusNode,
this.autofocus = false, this.autofocus = false,
this.tileColor,
this.selectedTileColor,
}) : assert(isThreeLine != null), }) : assert(isThreeLine != null),
assert(enabled != null), assert(enabled != null),
assert(selected != null), assert(selected != null),
...@@ -808,6 +810,16 @@ class ListTile extends StatelessWidget { ...@@ -808,6 +810,16 @@ class ListTile extends StatelessWidget {
/// {@macro flutter.widgets.Focus.autofocus} /// {@macro flutter.widgets.Focus.autofocus}
final bool 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 /// Add a one pixel border in between each tile. If color isn't specified the
/// [ThemeData.dividerColor] of the context's [Theme] is used. /// [ThemeData.dividerColor] of the context's [Theme] is used.
/// ///
...@@ -913,6 +925,16 @@ class ListTile extends StatelessWidget { ...@@ -913,6 +925,16 @@ class ListTile extends StatelessWidget {
: style.copyWith(color: color); : style.copyWith(color: color);
} }
Color _tileBackgroundColor() {
if (!selected && tileColor != null)
return tileColor;
if (selected && selectedTileColor != null)
return selectedTileColor;
return Colors.transparent;
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context)); assert(debugCheckHasMaterial(context));
...@@ -984,6 +1006,8 @@ class ListTile extends StatelessWidget { ...@@ -984,6 +1006,8 @@ class ListTile extends StatelessWidget {
child: Semantics( child: Semantics(
selected: selected, selected: selected,
enabled: enabled, enabled: enabled,
child: ColoredBox(
color: _tileBackgroundColor(),
child: SafeArea( child: SafeArea(
top: false, top: false,
bottom: false, bottom: false,
...@@ -1002,6 +1026,7 @@ class ListTile extends StatelessWidget { ...@@ -1002,6 +1026,7 @@ class ListTile extends StatelessWidget {
), ),
), ),
), ),
),
); );
} }
} }
......
...@@ -1534,4 +1534,80 @@ void main() { ...@@ -1534,4 +1534,80 @@ void main() {
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); 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);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment