|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:piwigo_ng/components/sections/form_section.dart'; |
| 3 | +import 'package:piwigo_ng/utils/localizations.dart'; |
| 4 | + |
| 5 | +class SelectModelList<T> extends StatefulWidget { |
| 6 | + const SelectModelList({ |
| 7 | + Key? key, |
| 8 | + this.selected = const [], |
| 9 | + this.unselected = const [], |
| 10 | + required this.itemBuilder, |
| 11 | + this.onSelect, |
| 12 | + this.onDeselect, |
| 13 | + }) : super(key: key); |
| 14 | + |
| 15 | + final List<T> selected; |
| 16 | + final List<T> unselected; |
| 17 | + final Widget Function(T) itemBuilder; |
| 18 | + final int Function(T)? onSelect; |
| 19 | + final int Function(T)? onDeselect; |
| 20 | + |
| 21 | + @override |
| 22 | + State<SelectModelList<T>> createState() => _SelectModelListState<T>(); |
| 23 | +} |
| 24 | + |
| 25 | +class _SelectModelListState<T> extends State<SelectModelList<T>> { |
| 26 | + final GlobalKey<AnimatedListState> _selectedListKey = |
| 27 | + GlobalKey<AnimatedListState>(); |
| 28 | + final GlobalKey<AnimatedListState> _unselectedListKey = |
| 29 | + GlobalKey<AnimatedListState>(); |
| 30 | + |
| 31 | + void _onTapItem(T item) { |
| 32 | + if (widget.selected.contains(item)) { |
| 33 | + _onDeselect(item); |
| 34 | + } else { |
| 35 | + _onSelect(item); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + void _onSelect(T item) async { |
| 40 | + int oldIndex = widget.unselected.indexOf(item); |
| 41 | + _unselectedListKey.currentState?.removeItem( |
| 42 | + oldIndex, |
| 43 | + (_, animation) => _buildItem(item, animation, false), |
| 44 | + duration: const Duration(milliseconds: 150), |
| 45 | + ); |
| 46 | + |
| 47 | + int? newIndex = widget.onSelect?.call(item); |
| 48 | + newIndex ??= widget.selected.indexOf(item); |
| 49 | + _selectedListKey.currentState?.insertItem(newIndex); |
| 50 | + } |
| 51 | + |
| 52 | + void _onDeselect(T item) async { |
| 53 | + int oldIndex = widget.selected.indexOf(item); |
| 54 | + _selectedListKey.currentState?.removeItem( |
| 55 | + oldIndex, |
| 56 | + (_, animation) => _buildItem(item, animation, true), |
| 57 | + duration: const Duration(milliseconds: 150), |
| 58 | + ); |
| 59 | + |
| 60 | + int? newIndex = widget.onDeselect?.call(item); |
| 61 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 62 | + newIndex ??= widget.unselected.indexOf(item); |
| 63 | + _unselectedListKey.currentState?.insertItem(newIndex); |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + Widget build(BuildContext context) { |
| 68 | + return Column( |
| 69 | + children: [ |
| 70 | + _buildSelectedList, |
| 71 | + _buildUnselectedList, |
| 72 | + ], |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + Widget get _buildSelectedList { |
| 77 | + return FormSection( |
| 78 | + title: appStrings.tagsHeader_selected, |
| 79 | + child: ClipRRect( |
| 80 | + borderRadius: BorderRadius.circular(15.0), |
| 81 | + child: Material( |
| 82 | + color: Theme.of(context).cardColor, |
| 83 | + child: AnimatedList( |
| 84 | + key: _selectedListKey, |
| 85 | + shrinkWrap: true, |
| 86 | + physics: const NeverScrollableScrollPhysics(), |
| 87 | + initialItemCount: widget.selected.length, |
| 88 | + itemBuilder: (context, index, animation) { |
| 89 | + T item = widget.selected[index]; |
| 90 | + return _buildItem(item, animation, true); |
| 91 | + }, |
| 92 | + ), |
| 93 | + ), |
| 94 | + ), |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + Widget get _buildUnselectedList { |
| 99 | + return FormSection( |
| 100 | + title: appStrings.tagsHeader_notSelected, |
| 101 | + child: ClipRRect( |
| 102 | + borderRadius: BorderRadius.circular(15.0), |
| 103 | + child: Material( |
| 104 | + color: Theme.of(context).cardColor, |
| 105 | + child: AnimatedList( |
| 106 | + key: _unselectedListKey, |
| 107 | + shrinkWrap: true, |
| 108 | + physics: const NeverScrollableScrollPhysics(), |
| 109 | + initialItemCount: widget.unselected.length, |
| 110 | + itemBuilder: (context, index, animation) { |
| 111 | + T item = widget.unselected[index]; |
| 112 | + return _buildItem(item, animation, false); |
| 113 | + }, |
| 114 | + ), |
| 115 | + ), |
| 116 | + ), |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + _buildItem(T item, Animation<double> animation, bool selected) { |
| 121 | + return SizeTransition( |
| 122 | + sizeFactor: animation, |
| 123 | + child: ListTile( |
| 124 | + key: ObjectKey(item), |
| 125 | + visualDensity: VisualDensity.compact, |
| 126 | + shape: Border( |
| 127 | + bottom: BorderSide(color: Theme.of(context).scaffoldBackgroundColor), |
| 128 | + ), |
| 129 | + title: widget.itemBuilder(item), |
| 130 | + trailing: selected |
| 131 | + ? Icon(Icons.remove_circle_outline, color: Colors.red) |
| 132 | + : Icon(Icons.add_circle_outline, color: Colors.green), |
| 133 | + onTap: () => _onTapItem(item), |
| 134 | + ), |
| 135 | + ); |
| 136 | + } |
| 137 | +} |
0 commit comments