@@ -23,44 +23,54 @@ class SelectModelList<T> extends StatefulWidget {
2323}
2424
2525class _SelectModelListState <T > extends State <SelectModelList <T >> {
26+ static const Duration kAnimationDuration = Duration (milliseconds: 300 );
2627 final GlobalKey <AnimatedListState > _selectedListKey =
2728 GlobalKey <AnimatedListState >();
2829 final GlobalKey <AnimatedListState > _unselectedListKey =
2930 GlobalKey <AnimatedListState >();
3031
31- void _onTapItem (T item) {
32- if (widget.selected.contains (item)) {
32+ void _onTapItem (T item, bool selected) {
33+ if (selected) {
34+ if (widget.unselected.contains (item)) return ;
3335 _onDeselect (item);
3436 } else {
37+ if (widget.selected.contains (item)) return ;
3538 _onSelect (item);
3639 }
3740 }
3841
3942 void _onSelect (T item) async {
4043 int oldIndex = widget.unselected.indexOf (item);
44+ int ? newIndex = widget.onSelect? .call (item);
45+
4146 _unselectedListKey.currentState? .removeItem (
4247 oldIndex,
4348 (_, animation) => _buildItem (item, animation, false ),
44- duration: const Duration (milliseconds : 150 ) ,
49+ duration: kAnimationDuration ,
4550 );
4651
47- int ? newIndex = widget.onSelect? .call (item);
4852 newIndex ?? = widget.selected.indexOf (item);
49- _selectedListKey.currentState? .insertItem (newIndex);
53+ _selectedListKey.currentState? .insertItem (
54+ newIndex,
55+ duration: kAnimationDuration,
56+ );
5057 }
5158
5259 void _onDeselect (T item) async {
5360 int oldIndex = widget.selected.indexOf (item);
61+ int ? newIndex = widget.onDeselect? .call (item);
62+
5463 _selectedListKey.currentState? .removeItem (
5564 oldIndex,
5665 (_, animation) => _buildItem (item, animation, true ),
57- duration: const Duration (milliseconds : 150 ) ,
66+ duration: kAnimationDuration ,
5867 );
59-
60- int ? newIndex = widget.onDeselect? .call (item);
61- await Future .delayed (const Duration (milliseconds: 100 ));
68+ // await Future.delayed(const Duration(milliseconds: 10));
6269 newIndex ?? = widget.unselected.indexOf (item);
63- _unselectedListKey.currentState? .insertItem (newIndex);
70+ _unselectedListKey.currentState? .insertItem (
71+ newIndex,
72+ duration: kAnimationDuration,
73+ );
6474 }
6575
6676 @override
@@ -117,9 +127,14 @@ class _SelectModelListState<T> extends State<SelectModelList<T>> {
117127 );
118128 }
119129
120- _buildItem (T item, Animation <double > animation, bool selected) {
130+ Widget _buildItem (
131+ T item,
132+ Animation <double > animation,
133+ bool selected,
134+ ) {
121135 return SizeTransition (
122- sizeFactor: animation,
136+ sizeFactor: animation.drive (
137+ Tween (begin: .0 , end: 1.0 ).chain (CurveTween (curve: Curves .ease))),
123138 child: ListTile (
124139 key: ObjectKey (item),
125140 visualDensity: VisualDensity .compact,
@@ -130,7 +145,7 @@ class _SelectModelListState<T> extends State<SelectModelList<T>> {
130145 trailing: selected
131146 ? Icon (Icons .remove_circle_outline, color: Colors .red)
132147 : Icon (Icons .add_circle_outline, color: Colors .green),
133- onTap: () => _onTapItem (item),
148+ onTap: () => _onTapItem (item, selected ),
134149 ),
135150 );
136151 }
0 commit comments