Skip to content

Commit

Permalink
Support multiple special items.
Browse files Browse the repository at this point in the history
  • Loading branch information
yujunetee committed Oct 10, 2024
1 parent d4bbece commit a764a6e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 66 deletions.
20 changes: 10 additions & 10 deletions example/lib/constants/picker_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ class PickMethod {
selectedAssets: assets,
specialItems: [
SpecialItem(
itemPosition: SpecialItemPosition.prepend,
itemBuilder: (
position: SpecialItemPosition.prepend,
builder: (
BuildContext context,
AssetPathEntity? path,
int length,
Expand Down Expand Up @@ -194,8 +194,8 @@ class PickMethod {
selectedAssets: assets,
specialItems: [
SpecialItem(
itemPosition: SpecialItemPosition.prepend,
itemBuilder: (
position: SpecialItemPosition.prepend,
builder: (
BuildContext context,
AssetPathEntity? path,
int length,
Expand Down Expand Up @@ -309,8 +309,8 @@ class PickMethod {
selectedAssets: assets,
specialItems: [
SpecialItem(
itemPosition: SpecialItemPosition.prepend,
itemBuilder: (
position: SpecialItemPosition.prepend,
builder: (
BuildContext context,
AssetPathEntity? path,
int length,
Expand Down Expand Up @@ -344,8 +344,8 @@ class PickMethod {
selectedAssets: assets,
specialItems: [
SpecialItem(
itemPosition: SpecialItemPosition.prepend,
itemBuilder: (
position: SpecialItemPosition.prepend,
builder: (
BuildContext context,
AssetPathEntity? path,
int length,
Expand All @@ -357,8 +357,8 @@ class PickMethod {
},
),
SpecialItem(
itemPosition: SpecialItemPosition.append,
itemBuilder: (
position: SpecialItemPosition.append,
builder: (
BuildContext context,
AssetPathEntity? path,
int length,
Expand Down
4 changes: 2 additions & 2 deletions example/lib/customs/pickers/directory_file_asset_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,8 @@ class FileAssetPickerBuilder
) {
int currentIndex = index;

if (prepandSpecialItems.isNotEmpty) {
currentIndex = index - prepandSpecialItems.length;
if (prependSpecialItems.isNotEmpty) {
currentIndex = index - prependSpecialItems.length;
}

final File asset = currentAssets.elementAt(currentIndex);
Expand Down
4 changes: 2 additions & 2 deletions example/lib/l10n/gen/app_localizations_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ class AppLocalizationsZh extends AppLocalizations {
String get pickMethodPrependItemDescription => '网格的靠前位置会添加一个自定义的 widget。';

@override
String get pickMethodMultiSpecialItemsName => '多个特殊item';
String get pickMethodMultiSpecialItemsName => '多个特殊 widget';

@override
String get pickMethodMultiSpecialItemsDescription =>
'网格的靠前或靠后位置会添加多个自定义的 widget。';
'网格的靠前或靠后位置会可以多个自定义的 widget。';

@override
String get pickMethodNoPreviewName => '禁止预览';
Expand Down
4 changes: 2 additions & 2 deletions lib/src/constants/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import 'package:flutter/material.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:wechat_assets_picker/src/models/special_item.dart';

import '../constants/typedefs.dart';
import '../delegates/asset_picker_text_delegate.dart';
import '../delegates/sort_path_delegate.dart';
import '../models/special_item.dart';
import 'constants.dart';
import 'enums.dart';

Expand Down Expand Up @@ -193,5 +193,5 @@ class AssetPickerConfig {

/// List of special items.
/// 自定义item列表
final List<SpecialItem> specialItems;
final List<SpecialItem<AssetPathEntity>> specialItems;
}
7 changes: 7 additions & 0 deletions lib/src/constants/typedefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import 'package:flutter/widgets.dart';
import 'package:photo_manager/photo_manager.dart' show PermissionState;
import 'package:provider/provider.dart';

import './enums.dart';

/// Mirroring [ChangeNotifierProvider].
typedef CNP<T extends ChangeNotifier?> = ChangeNotifierProvider<T>;

Expand Down Expand Up @@ -76,3 +78,8 @@ typedef AssetsChangeRefreshPredicate<Path> = bool Function(
MethodCall call,
Path? path,
);

typedef SpecialItemModel = ({
SpecialItemPosition position,
Widget item,
});
88 changes: 38 additions & 50 deletions lib/src/delegates/asset_picker_builder_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
),
themeColor = pickerTheme?.colorScheme.secondary ??
themeColor ??
defaultThemeColorWeChat {
defaultThemeColorWeChat,
prependSpecialItems = specialItems
.where(
(item) => item.position == SpecialItemPosition.prepend,
)
.toList() {
Singleton.textDelegate =
textDelegate ?? assetPickerTextDelegateFromLocale(locale);
}
Expand Down Expand Up @@ -86,7 +91,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {

/// List of special items.
/// 自定义item列表
final List<SpecialItem> specialItems;
final List<SpecialItem<Path>> specialItems;

/// Indicates the loading status for the builder.
/// 指示目前加载的状态
Expand Down Expand Up @@ -126,6 +131,10 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
final AssetsChangeRefreshPredicate<AssetPathEntity>?
assetsChangeRefreshPredicate;

/// List of prepend special items.
/// 前置自定义item列表
final List<SpecialItem<Path>> prependSpecialItems;

/// [ThemeData] for the picker.
/// 选择器使用的主题
ThemeData get theme => pickerTheme ?? AssetPicker.themeData(themeColor);
Expand Down Expand Up @@ -220,14 +229,6 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
AssetPickerTextDelegate get semanticsTextDelegate =>
Singleton.textDelegate.semanticsTextDelegate;

Iterable<SpecialItem> get prepandSpecialItems => specialItems.where(
(model) => model.itemPosition == SpecialItemPosition.prepend,
);

Iterable<SpecialItem> get appendSpecialItems => specialItems.where(
(model) => model.itemPosition == SpecialItemPosition.append,
);

/// Keep a `initState` method to sync with [State].
/// 保留一个 `initState` 方法与 [State] 同步。
@mustCallSuper
Expand Down Expand Up @@ -759,7 +760,7 @@ class DefaultAssetPickerBuilderDelegate
this.specialPickerType,
this.keepScrollOffset = false,
this.shouldAutoplayPreview = false,
this.specialItems = const [],
super.specialItems = const [],
}) {
// Add the listener if [keepScrollOffset] is true.
if (keepScrollOffset) {
Expand Down Expand Up @@ -818,11 +819,6 @@ class DefaultAssetPickerBuilderDelegate
/// 预览是否自动播放
final bool shouldAutoplayPreview;

/// List of special items.
/// 自定义item列表
@override
final List<SpecialItem> specialItems;

/// [Duration] when triggering path switching.
/// 切换路径时的动画时长
Duration get switchingPathDuration => const Duration(milliseconds: 300);
Expand Down Expand Up @@ -1236,29 +1232,23 @@ class DefaultAssetPickerBuilderDelegate
builder: (context, wrapper, _) {
// First, we need the count of the assets.
int totalCount = wrapper?.assetCount ?? 0;
final List<
({SpecialItemPosition specialItemPosition, Widget specialItem})>
specialItemModels = [];
// If user chose a special item's position, add 1 count.
if (specialItems.isNotEmpty) {
for (final item in specialItems) {
final specialItem = item.itemBuilder?.call(
context,
wrapper?.path,
totalCount,
isPermissionLimited,
);
if (specialItem != null) {
specialItemModels.add(
(
specialItemPosition: item.itemPosition,
specialItem: specialItem,
),

final List<SpecialItemModel> specialItemModels = specialItems
.map((item) {
return (
position: item.position,
item: item.builder?.call(
context,
wrapper?.path,
totalCount,
isPermissionLimited,
)
);
totalCount += 1;
}
}
}
})
.whereType<SpecialItemModel>()
.toList();

totalCount += specialItemModels.length;

if (totalCount == 0 && specialItemModels.isEmpty) {
return loadingIndicator(context);
Expand Down Expand Up @@ -1419,8 +1409,7 @@ class DefaultAssetPickerBuilderDelegate
BuildContext context,
int index,
List<AssetEntity> currentAssets, {
List<({SpecialItemPosition specialItemPosition, Widget specialItem})>
specialItemModels = const [],
List<SpecialItemModel> specialItemModels = const [],
}) {
final DefaultAssetPickerProvider p =
context.read<DefaultAssetPickerProvider>();
Expand All @@ -1429,22 +1418,22 @@ class DefaultAssetPickerBuilderDelegate
final AssetPathEntity? currentPathEntity = currentWrapper?.path;

final prepandSpecialItemModels = specialItemModels.where(
(model) => model.specialItemPosition == SpecialItemPosition.prepend,
(model) => model.position == SpecialItemPosition.prepend,
);
final appendSpecialItemModels = specialItemModels.where(
(model) => model.specialItemPosition == SpecialItemPosition.append,
(model) => model.position == SpecialItemPosition.append,
);

if (specialItemModels.isNotEmpty) {
if (prepandSpecialItemModels.isNotEmpty) {
if (index < prepandSpecialItemModels.length) {
return specialItemModels[index].specialItem;
return specialItemModels[index].item;
}
}

if (appendSpecialItemModels.isNotEmpty) {
if (index >= length + prepandSpecialItemModels.length) {
return specialItemModels[index - length].specialItem;
return specialItemModels[index - length].item;
}
}
}
Expand Down Expand Up @@ -1488,8 +1477,8 @@ class DefaultAssetPickerBuilderDelegate
}

int semanticIndex(int index) {
if (prepandSpecialItems.isNotEmpty) {
return index - prepandSpecialItems.length + 1;
if (prependSpecialItems.isNotEmpty) {
return index - prependSpecialItems.length + 1;
}

return index;
Expand Down Expand Up @@ -1584,8 +1573,8 @@ class DefaultAssetPickerBuilderDelegate
}) {
int index = assets.indexWhere((AssetEntity e) => e.id == id);

if (prepandSpecialItems.isNotEmpty) {
index += prepandSpecialItems.length;
if (prependSpecialItems.isNotEmpty) {
index += prependSpecialItems.length;
}
index += placeholderCount;
return index;
Expand All @@ -1596,8 +1585,7 @@ class DefaultAssetPickerBuilderDelegate
required BuildContext context,
required List<AssetEntity> assets,
int placeholderCount = 0,
List<({SpecialItemPosition specialItemPosition, Widget specialItem})>
specialItemModels = const [],
List<SpecialItemModel> specialItemModels = const [],
}) {
final PathWrapper<AssetPathEntity>? currentWrapper = context
.select<DefaultAssetPickerProvider, PathWrapper<AssetPathEntity>?>(
Expand Down

0 comments on commit a764a6e

Please sign in to comment.