<p>In this example, I'm going to share an example of how to select items in a ListView with a Trailing Icon.</p> <p>Here is how the output of the program is going to look like. You can see the items are turned into blue and a checkbox with a check Icon appears on long press. This Check Icons, actually makes the selected item more recognizable when it's long pressed.</p> <p>);</p> <p>class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); }</p> <p>class _MyAppState extends State { double padValue = 0;</p> <p>List paints = [ Paint(1, 'Red', Colors.red), Paint(2, 'Blue', Colors.blue), Paint(3, 'Green', Colors.green), Paint(4, 'Lime', Colors.lime), Paint(5, 'Indigo', Colors.indigo), Paint(6, 'Yellow', Colors.yellow) ];</p> <p>@override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text("Selectable ListView Example"), ), body: ListView( children: List.generate(paints.length, (index) { return ListTile( onLongPress: () { setState(() { paints[index].selected = !paints[index].selected;</p> <pre> log(paints[index].selected.toString()); }); }, selected: paints[index].selected, leading: GestureDetector( behavior: HitTestBehavior.opaque, onTap: () , child: Container( width: 48, height: 48, padding: EdgeInsets.symmetric(vertical: 4.0), alignment: Alignment.center, child: CircleAvatar( backgroundColor: paints[index].colorpicture, ), ), ), title: Text('ID: ' + paints[index].id.toString()), subtitle: Text(paints[index].title), trailing: (paints[index].selected) ? Icon(Icons.check_box) : Icon(Icons.check_box_outline_blank), ); }), ), ), ); </pre> <p>} }</p> <p>class Paint { final int id; final String title; final Color colorpicture; bool selected = false;</p> <p>Paint(this.id, this.title, this.colorpicture); }</p> <p>Hope this will be useful to you. Thanks,<br /> Srikanth</p>
Select Items in a ListView in Flutter - with a Trailing Icon
This Article is posted by seven.srikanth at 10/29/2019 5:08:40 PM
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter
Tags: Select items in ListView; OnLongPress ListTile; ListView Example; add icon to ListView;
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter