I\'ve made a ListView
in Flutter, but now I have some ListTiles
in this ListView
that can be selected. Upon selection, I want the back
I was able to change the background color of the ListTile using a BoxDecoration inside Container:
ListView (
children: <Widget>[
new Container (
decoration: new BoxDecoration (
color: Colors.red
),
child: new ListTile (
leading: const Icon(Icons.euro_symbol),
title: Text('250,00')
)
)
]
)
I know that the original question has been answered, but I wanted to add how to set the color of ListTile
while the tile is being pressed. The property you are looking for is called highlight color
and it can be set by wrapping the ListTile
in a Theme
widget, like this:
Theme(
data: ThemeData(
highlightColor: Colors.red,
),
child: ListTile(...),
)
);
Note: if the Theme
widget resets the font of text elements inside the ListTile
, just set its fontFamily
property to the same value You used in other places in your app.
An easy way would be to store the initial index in a variable and then change the state of that variable whenever tapped.
ListView.builder(
shrinkWrap: true,
itemCount: 4,
itemBuilder: (context, index) {
return Container( //I have used container for this example. [not mandatory]
color: tappedIndex == index ? Colors.blue : Colors.grey,
child: ListTile(
title: Center(
child: Text('${index + 1}'),
),onTap:(){
setState((){
tappedIndex=index;
});
}));
})
full code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyWidget(),
);
}
}
class MyWidget extends StatefulWidget {
@override
MyWidgetState createState() => MyWidgetState();
}
class MyWidgetState extends State<MyWidget> {
int tappedIndex;
@override
void initState() {
super.initState();
tappedIndex = 0;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ListView.builder(
shrinkWrap: true,
itemCount: 4,
itemBuilder: (context, index) {
return Container(
color: tappedIndex == index ? Colors.blue : Colors.grey,
child: ListTile(
title: Center(
child: Text('${index + 1}'),
),onTap:(){
setState((){
tappedIndex=index;
});
}));
})
]));
}
}
Dartpad link: https://dartpad.dev/250ff453b97cc79225e8a9c657dffc8a
I have used as
ListTile(
title: Text('Receipts'),
leading: Icon(Icons.point_of_sale),
tileColor: Colors.blue,
),
Your answer has been answered in Github.
Card(
color: Colors.white,
shape: ContinuousRectangleBorder(
borderRadius: BorderRadius.zero,
),
borderOnForeground: true,
elevation: 0,
margin: EdgeInsets.fromLTRB(0,0,0,0),
child: ListTile(
// ...
),
)
Screenshot:
Solution:
// you can do that by using `Map` but for simplicity I used 2 separate `List`.
List<int> _list = List.generate(20, (i) => i);
List<bool> _selected = List.generate(20, (i) => false); // initially fill it up with false
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: ListView.builder(
itemBuilder: (_, i) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 2),
color: _selected[i] ? Colors.blue : null, // if current item is selected show blue color
child: ListTile(
title: Text("Item ${_list[i]}"),
onTap: () => setState(() => _selected[i] = !_selected[i]), // reverse bool value
),
);
},
),
);
}