Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

➕ Support Drag and Select feature #175 #660

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
130189a
➕ Support Drag and Select feature
WeiJun0507 Nov 9, 2024
a1e594d
➕ Support Drag and Select feature
WeiJun0507 Nov 17, 2024
aa6f46e
➕ Support Drag and Select feature
WeiJun0507 Nov 17, 2024
0e82a3b
➕ Support Drag and Select feature
WeiJun0507 Nov 23, 2024
3faa7a0
Merge branch 'fluttercandies:main' into drag_select_featue_v2
WeiJun0507 Nov 23, 2024
6a366fb
➕ Support Drag and Select feature
WeiJun0507 Nov 23, 2024
1f989bb
➕ Support Drag and Select feature
WeiJun0507 Nov 24, 2024
c42e7cc
➕ Support Drag and Select feature
WeiJun0507 Nov 24, 2024
f7dd57d
♻️ Use horizonal gestures to trigger aggregator calculations
AlexV525 Dec 2, 2024
ff77528
♻️ Refine the coordinator and add multiple gestures support
AlexV525 Dec 3, 2024
6bca4c2
♻️ `enableDragAndSelect` -> `dragToSelect`
AlexV525 Dec 3, 2024
cb46460
🐛 Fix dimension calculations
AlexV525 Dec 4, 2024
b6bcc6e
🐛 Fix the missing range from the largest index
AlexV525 Dec 4, 2024
ec57fba
♿️ Try to fix accessibility issues
AlexV525 Dec 4, 2024
9a1bcdf
🐛 Fix grid revert calculations
AlexV525 Dec 4, 2024
3026576
🐛 Fix column and row index inaccurate when asset is less than a page
WeiJun0507 Dec 12, 2024
9388ada
🐛 Fix android revert drag select calculation
WeiJun0507 Dec 17, 2024
f72478d
⚡️ Optimize column and row index calculation in reverse grid
WeiJun0507 Dec 18, 2024
f9016c1
:bug: Fix drag select index accuracy when asset size is one page
WeiJun0507 Dec 23, 2024
77d4429
Revert ":bug: Fix drag select index accuracy when asset size is one p…
WeiJun0507 Dec 23, 2024
dfa8352
🐛 Fix drag select index accuracy when asset size is one page
WeiJun0507 Dec 23, 2024
f39b2ba
🐛 Improve the accuracy of select asset on iOS device
WeiJun0507 Dec 30, 2024
8580c40
🐛 Fix bottom gaps when grid reverting on Android
AlexV525 Dec 31, 2024
60239f6
🐛 Improve the accuracy of select asset on iOS device
WeiJun0507 Jan 4, 2025
643af2f
🐛 Fix anchors and reverts
AlexV525 Jan 7, 2025
a02aa2c
🐛 Fix placeholders count
AlexV525 Jan 7, 2025
7eba1b4
⚡️ Unify calculations and comments
AlexV525 Jan 7, 2025
7835a6c
🚸 Improves auto-scroll
AlexV525 Jan 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/lib/constants/picker_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PickMethod {
pickerConfig: AssetPickerConfig(
maxAssets: maxAssetsCount,
selectedAssets: assets,
enableDragAndSelect: true,
),
);
},
Expand Down
5 changes: 5 additions & 0 deletions lib/src/constants/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AssetPickerConfig {
this.assetsChangeCallback,
this.assetsChangeRefreshPredicate,
this.shouldAutoplayPreview = false,
this.enableDragAndSelect = true,
}) : assert(
pickerTheme == null || themeColor == null,
'pickerTheme and themeColor cannot be set at the same time.',
Expand Down Expand Up @@ -205,4 +206,8 @@ class AssetPickerConfig {
/// Whether the preview should auto play.
/// 预览是否自动播放
final bool shouldAutoplayPreview;

/// Should enable drag and select function.
/// 是否开启拖拽选择
final bool enableDragAndSelect;
}
46 changes: 40 additions & 6 deletions lib/src/delegates/asset_picker_builder_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
this.pathNameBuilder,
this.assetsChangeCallback,
this.assetsChangeRefreshPredicate,
this.enableDragAndSelect = true,
AlexV525 marked this conversation as resolved.
Show resolved Hide resolved
Color? themeColor,
AssetPickerTextDelegate? textDelegate,
Locale? locale,
Expand Down Expand Up @@ -130,6 +131,10 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
final AssetsChangeRefreshPredicate<AssetPathEntity>?
assetsChangeRefreshPredicate;

/// Should enable drag and select function.
/// 是否开启拖拽选择
final bool enableDragAndSelect;

/// [ThemeData] for the picker.
/// 选择器使用的主题
ThemeData get theme => pickerTheme ?? AssetPicker.themeData(themeColor);
Expand Down Expand Up @@ -755,6 +760,7 @@ class DefaultAssetPickerBuilderDelegate
super.themeColor,
super.textDelegate,
super.locale,
super.enableDragAndSelect,
this.gridThumbnailSize = defaultAssetGridPreviewSize,
this.previewThumbnailSize,
this.specialPickerType,
Expand Down Expand Up @@ -1268,6 +1274,10 @@ class DefaultAssetPickerBuilderDelegate
context.topPadding + appBarPreferredSize!.height;

final textDirection = Directionality.of(context);
final provider = Provider.of<DefaultAssetPickerProvider>(context);
final double screenWidth = MediaQuery.sizeOf(context).width;
final double itemSize = screenWidth / gridCount;

Widget sliverGrid(BuildContext context, List<AssetEntity> assets) {
return SliverGrid(
delegate: SliverChildBuilderDelegate(
Expand All @@ -1278,15 +1288,39 @@ class DefaultAssetPickerBuilderDelegate
}
index -= placeholderCount;
}
return MergeSemantics(
child: Directionality(
textDirection: textDirection,
child: assetGridItemBuilder(

Widget child = assetGridItemBuilder(
context,
index,
assets,
specialItem: specialItem,
);

if (enableDragAndSelect) {
child = GestureDetector(
onLongPressStart: (d) => provider.onDragStart(
context,
d,
index,
assets,
specialItem: specialItem,
assets[index],
),
onLongPressMoveUpdate: (d) => provider.onDragUpdate(
context,
d,
itemSize,
gridCount,
topPadding,
),
onLongPressCancel: provider.resetDraggingStatus,
onLongPressEnd: provider.onDragEnd,
child: child,
);
}

return MergeSemantics(
child: Directionality(
textDirection: textDirection,
child: child,
),
);
},
Expand Down
1 change: 1 addition & 0 deletions lib/src/delegates/asset_picker_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class AssetPickerDelegate {
themeColor: pickerConfig.themeColor,
locale: Localizations.maybeLocaleOf(context),
shouldAutoplayPreview: pickerConfig.shouldAutoplayPreview,
enableDragAndSelect: pickerConfig.enableDragAndSelect,
),
);
final List<AssetEntity>? result = await Navigator.maybeOf(
Expand Down
149 changes: 149 additions & 0 deletions lib/src/provider/asset_picker_provider.dart
AlexV525 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,155 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
set.remove(item);
selectedAssets = set;
}

/// 拖拽状态
/// Drag status
bool isInDragging = false;

/// 边缘自动滚动控制器
/// Edge Auto Scrolling Detector. Use to support edge auto scroll when drag position reach the edge of device's screen.
EdgeDraggingAutoScroller? _autoScroller;

// An eyeballed value for a smooth scrolling experience.
static const double _kDefaultAutoScrollVelocityScalar = 50;

/// 起始选择序号
/// Item index of the first selected item
int initialSelectedIdx = -1;

/// 拖拽选择 或 拖拽取消选择
/// Drag to select or deselect state
bool dragSelect = true;

/// 长按启动拖拽
/// Long Press to enable drag and select
void onDragStart(
BuildContext context,
LongPressStartDetails details,
int index,
Asset entity,
) {
isInDragging = true;

final scrollableState = _checkScrollableStatePresent(context);
if (scrollableState == null) {
return;
}

_autoScroller = EdgeDraggingAutoScroller(
scrollableState,
velocityScalar: _kDefaultAutoScrollVelocityScalar,
);

initialSelectedIdx = index;

dragSelect = !selectedAssets.contains(entity);
}

void onDragUpdate(
BuildContext context,
LongPressMoveUpdateDetails details,
double itemSize,
int gridCount,
double topPadding,
) {
if (!isInDragging) {
return;
}

if (dragSelect && selectedAssets.length == maxAssets) {
return;
}

final scrollableState = _checkScrollableStatePresent(context);
if (scrollableState == null) {
return;
}

final column = _getIndex(details.globalPosition.dx, itemSize);
final row = _getIndex(
details.globalPosition.dy -
AlexV525 marked this conversation as resolved.
Show resolved Hide resolved
topPadding -
24.0 +
scrollableState.position.pixels,
itemSize,
);

final currentDragIndex = row * gridCount + column;

List<Asset> filteredAssetList = <Asset>[];
// add asset
if (currentDragIndex < initialSelectedIdx) {
filteredAssetList = _currentAssets
.getRange(
currentDragIndex,
math.min(initialSelectedIdx + 1, currentAssets.length),
)
.toList()
..reversed;
} else {
filteredAssetList = _currentAssets
.getRange(
initialSelectedIdx,
math.min(currentDragIndex + 1, currentAssets.length),
)
.toList();
}

filteredAssetList.forEach(dragSelect ? selectAsset : unSelectAsset);

_autoScroller?.startAutoScrollIfNecessary(
Rect.fromLTWH(
(column + 1) * itemSize,
details.globalPosition.dy > MediaQuery.sizeOf(context).height * 0.8
? (row + 1) * itemSize
: math.max(topPadding, details.globalPosition.dy),
itemSize,
itemSize,
),
);
}

void onDragEnd(LongPressEndDetails details) {
resetDraggingStatus();
}

/// 复原拖拽状态
/// Reset dragging status
void resetDraggingStatus() {
isInDragging = false;
initialSelectedIdx = -1;
dragSelect = true;
_autoScroller?.stopAutoScroll();
_autoScroller = null;
}

/// 检查[Scrollable] state是否存在
///
ScrollableState? _checkScrollableStatePresent(BuildContext context) {
final scrollable = Scrollable.maybeOf(context);
assert(
scrollable != null,
'To use drag and select function, Scrollable state must be the present to get the actual item position.',
);
assert(
scrollable?.position.axis == Axis.vertical,
'To use drag and select function. The Scrollable Axis must be in vertical direction',
);

if (scrollable == null || scrollable.position.axis != Axis.vertical) {
resetDraggingStatus();
return null;
}

return scrollable;
}

/// 获取坐标
/// Get Coordinate Helper
int _getIndex(double delta, double itemSize) {
AlexV525 marked this conversation as resolved.
Show resolved Hide resolved
return delta ~/ itemSize;
}
}

/// The default implementation of the [AssetPickerProvider] for the picker.
Expand Down