Last active
March 19, 2022 16:15
-
-
Save Hitesh822/1ae15d7846d7ef00a54412683bbe267c to your computer and use it in GitHub Desktop.
Select List Items In Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(SelectListItem()); | |
class SelectListItem extends StatefulWidget { | |
@override | |
_SelectListItemState createState() => _SelectListItemState(); | |
} | |
class _SelectListItemState extends State<SelectListItem> { | |
List<int> _selectedItems = List<int>(); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Select List Items'), | |
), | |
body: ListView.builder( | |
itemCount: 10, | |
itemBuilder: (context, index){ | |
return | |
Container( | |
color: (_selectedItems.contains(index)) ? Colors.blue.withOpacity(0.5) : Colors.transparent, | |
child: ListTile( | |
onTap: (){ | |
if(_selectedItems.contains(index)){ | |
setState(() { | |
_selectedItems.removeWhere((val) => val == index); | |
}); | |
} | |
}, | |
onLongPress: (){ | |
if(! _selectedItems.contains(index)){ | |
setState(() { | |
_selectedItems.add(index); | |
}); | |
} | |
}, | |
title: Text('$index'), | |
), | |
); | |
}, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment