Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matifdeveloper/77ac2ef9bb47c2c2a8415283c0a5675e to your computer and use it in GitHub Desktop.
Save matifdeveloper/77ac2ef9bb47c2c2a8415283c0a5675e to your computer and use it in GitHub Desktop.
Gmail-like Multiple Selection ListView
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SelectionListView(),
);
}
}
class SelectionListView extends StatefulWidget {
@override
_SelectionListViewState createState() => _SelectionListViewState();
}
class _SelectionListViewState extends State<SelectionListView>
with SingleTickerProviderStateMixin {
List<String> emails = List.generate(20, (index) => "Email $index");
Set<int> selectedIndexes = {};
late AnimationController _controller;
late Animation<double> _animation;
int? currentSelectedIndex;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(milliseconds: 300),
);
_animation = Tween<double>(begin: 0.0, end: 1.0).animate(_controller);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _startAnimation() {
_controller.reset();
_controller.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Gmail-like Multiple Selection ListView'),
actions: [
IconButton(
icon: Icon(Icons.delete),
onPressed: () {
// Perform action on selected items
print('Deleting selected emails: $selectedIndexes');
},
),
],
),
body: ListView.builder(
itemCount: emails.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
setState(() {
if (selectedIndexes.contains(index)) {
selectedIndexes.remove(index);
currentSelectedIndex = null; // Reset current selected index
} else {
selectedIndexes.add(index);
currentSelectedIndex = index; // Update current selected index
}
});
_startAnimation();
},
child: ListTile(
title: Text(emails[index]),
leading: AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return CircleAvatar(
child: currentSelectedIndex == index
? Transform(
alignment: Alignment.center,
transform: Matrix4.identity()
..rotateY(3.14)
..rotateY(_animation.value * 3.14),
child: Icon(
Icons.check,
),
)
: selectedIndexes.contains(index)
? Icon(
Icons.check,
)
: Text("E"),
);
},
),
selected: selectedIndexes.contains(index),
selectedTileColor: Colors.blue.withOpacity(0.5),
),
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment