@@ -28,10 +28,17 @@ import '../widget/asset_picker_app_bar.dart';
28
28
import '../widget/asset_picker_viewer.dart' ;
29
29
import '../widget/builder/asset_entity_grid_item_builder.dart' ;
30
30
31
- typedef _SpecialItemModel = ({
32
- SpecialItemPosition position,
33
- Widget item,
34
- });
31
+ /// Class which contains non-null special item widget and its position which derived from the [SpecialItem]
32
+ /// 包含非空自定义item,并指定其位置。
33
+ class SpecialItemResult {
34
+ SpecialItemResult ({
35
+ required this .position,
36
+ required this .item,
37
+ });
38
+
39
+ SpecialItemPosition position;
40
+ Widget item;
41
+ }
35
42
36
43
/// The delegate to build the whole picker's components.
37
44
///
@@ -62,17 +69,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
62
69
),
63
70
themeColor = pickerTheme? .colorScheme.secondary ??
64
71
themeColor ??
65
- defaultThemeColorWeChat,
66
- prependSpecialItems = specialItems
67
- .where (
68
- (item) => item.position == SpecialItemPosition .prepend,
69
- )
70
- .toList (),
71
- appendSpecialItems = specialItems
72
- .where (
73
- (item) => item.position == SpecialItemPosition .append,
74
- )
75
- .toList () {
72
+ defaultThemeColorWeChat {
76
73
Singleton .textDelegate =
77
74
textDelegate ?? assetPickerTextDelegateFromLocale (locale);
78
75
}
@@ -141,14 +138,6 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
141
138
final AssetsChangeRefreshPredicate <AssetPathEntity >?
142
139
assetsChangeRefreshPredicate;
143
140
144
- /// List of prepend special items.
145
- /// 前置自定义item列表
146
- final List <SpecialItem <Path >> prependSpecialItems;
147
-
148
- /// List of append special items.
149
- /// 附加自定义item列表
150
- final List <SpecialItem <Path >> appendSpecialItems;
151
-
152
141
/// [ThemeData] for the picker.
153
142
/// 选择器使用的主题
154
143
ThemeData get theme => pickerTheme ?? AssetPicker .themeData (themeColor);
@@ -341,6 +330,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
341
330
int ? findChildIndexBuilder ({
342
331
required String id,
343
332
required List <Asset > assets,
333
+ required List <SpecialItemResult > prependSpecialItemResults,
344
334
int placeholderCount = 0 ,
345
335
}) =>
346
336
null ;
@@ -368,6 +358,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
368
358
int index,
369
359
Asset asset,
370
360
Widget child,
361
+ List <SpecialItemResult > prependSpecialItemResults,
371
362
);
372
363
373
364
/// The item builder for audio type of asset.
@@ -1248,24 +1239,28 @@ class DefaultAssetPickerBuilderDelegate
1248
1239
// First, we need the count of the assets.
1249
1240
int totalCount = wrapper? .assetCount ?? 0 ;
1250
1241
1251
- final List <_SpecialItemModel > specialItemModels = specialItems
1242
+ final List <SpecialItemResult > specialItemResults = specialItems
1252
1243
.map ((item) {
1253
- return (
1254
- position: item.position,
1255
- item: item.builder? .call (
1256
- context,
1257
- wrapper? .path,
1258
- totalCount,
1259
- permissionNotifier.value,
1260
- )
1244
+ final specialItem = item.builder? .call (
1245
+ context,
1246
+ wrapper? .path,
1247
+ totalCount,
1248
+ permissionNotifier.value,
1261
1249
);
1250
+ if (specialItem != null ) {
1251
+ return SpecialItemResult (
1252
+ position: item.position,
1253
+ item: specialItem,
1254
+ );
1255
+ }
1256
+ return null ;
1262
1257
})
1263
- .whereType <_SpecialItemModel >()
1258
+ .whereType <SpecialItemResult >()
1264
1259
.toList ();
1265
1260
1266
- totalCount += specialItemModels .length;
1261
+ totalCount += specialItemResults .length;
1267
1262
1268
- if (totalCount == 0 && specialItemModels .isEmpty) {
1263
+ if (totalCount == 0 && specialItemResults .isEmpty) {
1269
1264
return loadingIndicator (context);
1270
1265
}
1271
1266
// Then we use the [totalCount] to calculate placeholders we need.
@@ -1305,7 +1300,7 @@ class DefaultAssetPickerBuilderDelegate
1305
1300
context,
1306
1301
index,
1307
1302
assets,
1308
- specialItemModels : specialItemModels ,
1303
+ specialItemResults : specialItemResults ,
1309
1304
),
1310
1305
),
1311
1306
);
@@ -1314,14 +1309,20 @@ class DefaultAssetPickerBuilderDelegate
1314
1309
context: context,
1315
1310
assets: assets,
1316
1311
placeholderCount: placeholderCount,
1317
- specialItemModels : specialItemModels ,
1312
+ specialItemResults : specialItemResults ,
1318
1313
),
1319
1314
findChildIndexCallback: (Key ? key) {
1320
1315
if (key is ValueKey <String >) {
1321
1316
return findChildIndexBuilder (
1322
1317
id: key.value,
1323
1318
assets: assets,
1324
1319
placeholderCount: placeholderCount,
1320
+ prependSpecialItemResults: specialItemResults
1321
+ .where (
1322
+ (item) =>
1323
+ item.position == SpecialItemPosition .prepend,
1324
+ )
1325
+ .toList (),
1325
1326
);
1326
1327
}
1327
1328
return null ;
@@ -1424,17 +1425,17 @@ class DefaultAssetPickerBuilderDelegate
1424
1425
BuildContext context,
1425
1426
int index,
1426
1427
List <AssetEntity > currentAssets, {
1427
- List <_SpecialItemModel > specialItemModels = const [],
1428
+ List <SpecialItemResult > specialItemResults = const [],
1428
1429
}) {
1429
1430
final DefaultAssetPickerProvider p =
1430
1431
context.read <DefaultAssetPickerProvider >();
1431
1432
final int length = currentAssets.length;
1432
1433
final PathWrapper <AssetPathEntity >? currentWrapper = p.currentPath;
1433
1434
final AssetPathEntity ? currentPathEntity = currentWrapper? .path;
1434
1435
1435
- final prependItems = < _SpecialItemModel > [];
1436
- final appendItems = < _SpecialItemModel > [];
1437
- for (final model in specialItemModels ) {
1436
+ final prependItems = < SpecialItemResult > [];
1437
+ final appendItems = < SpecialItemResult > [];
1438
+ for (final model in specialItemResults ) {
1438
1439
switch (model.position) {
1439
1440
case SpecialItemPosition .prepend:
1440
1441
prependItems.add (model);
@@ -1445,13 +1446,13 @@ class DefaultAssetPickerBuilderDelegate
1445
1446
1446
1447
if (prependItems.isNotEmpty) {
1447
1448
if (index < prependItems.length) {
1448
- return specialItemModels [index].item;
1449
+ return specialItemResults [index].item;
1449
1450
}
1450
1451
}
1451
1452
1452
1453
if (appendItems.isNotEmpty) {
1453
1454
if (index >= length + prependItems.length) {
1454
- return specialItemModels [index - length].item;
1455
+ return specialItemResults [index - length].item;
1455
1456
}
1456
1457
}
1457
1458
@@ -1486,11 +1487,20 @@ class DefaultAssetPickerBuilderDelegate
1486
1487
itemBannedIndicator (context, asset),
1487
1488
],
1488
1489
);
1489
- return assetGridItemSemanticsBuilder (context, index, asset, content);
1490
+ return assetGridItemSemanticsBuilder (
1491
+ context,
1492
+ index,
1493
+ asset,
1494
+ content,
1495
+ prependItems,
1496
+ );
1490
1497
}
1491
1498
1492
- int semanticIndex (int index) {
1493
- return index - prependSpecialItems.length;
1499
+ int semanticIndex (
1500
+ int index,
1501
+ List <SpecialItemResult > prependSpecialItemResults,
1502
+ ) {
1503
+ return index - prependSpecialItemResults.length;
1494
1504
}
1495
1505
1496
1506
@override
@@ -1499,6 +1509,7 @@ class DefaultAssetPickerBuilderDelegate
1499
1509
int index,
1500
1510
AssetEntity asset,
1501
1511
Widget child,
1512
+ List <SpecialItemResult > prependSpecialItemResults,
1502
1513
) {
1503
1514
return ValueListenableBuilder <bool >(
1504
1515
valueListenable: isSwitchingPath,
@@ -1532,7 +1543,7 @@ class DefaultAssetPickerBuilderDelegate
1532
1543
excludeSemantics: true ,
1533
1544
focusable: ! isSwitchingPath,
1534
1545
label: '${semanticsTextDelegate .semanticTypeLabel (asset .type )}'
1535
- '${semanticIndex (index )}, '
1546
+ '${semanticIndex (index , prependSpecialItemResults )}, '
1536
1547
'${asset .createDateTime .toString ().replaceAll ('.000' , '' )}' ,
1537
1548
hidden: isSwitchingPath,
1538
1549
hint: hint,
@@ -1550,7 +1561,7 @@ class DefaultAssetPickerBuilderDelegate
1550
1561
onLongPressHint: semanticsTextDelegate.sActionPreviewHint,
1551
1562
selected: isSelected,
1552
1563
sortKey: OrdinalSortKey (
1553
- semanticIndex (index).toDouble (),
1564
+ semanticIndex (index, prependSpecialItemResults ).toDouble (),
1554
1565
name: 'GridItem' ,
1555
1566
),
1556
1567
value: selectedIndex > 0 ? '$selectedIndex ' : null ,
@@ -1563,7 +1574,7 @@ class DefaultAssetPickerBuilderDelegate
1563
1574
}
1564
1575
: null ,
1565
1576
child: IndexedSemantics (
1566
- index: semanticIndex (index),
1577
+ index: semanticIndex (index, prependSpecialItemResults ),
1567
1578
child: child,
1568
1579
),
1569
1580
),
@@ -1579,10 +1590,11 @@ class DefaultAssetPickerBuilderDelegate
1579
1590
int findChildIndexBuilder ({
1580
1591
required String id,
1581
1592
required List <AssetEntity > assets,
1593
+ required List <SpecialItemResult > prependSpecialItemResults,
1582
1594
int placeholderCount = 0 ,
1583
1595
}) {
1584
1596
int index = assets.indexWhere ((AssetEntity e) => e.id == id);
1585
- index += prependSpecialItems .length;
1597
+ index += prependSpecialItemResults .length;
1586
1598
index += placeholderCount;
1587
1599
return index;
1588
1600
}
@@ -1592,7 +1604,7 @@ class DefaultAssetPickerBuilderDelegate
1592
1604
required BuildContext context,
1593
1605
required List <AssetEntity > assets,
1594
1606
int placeholderCount = 0 ,
1595
- List <_SpecialItemModel > specialItemModels = const [],
1607
+ List <SpecialItemResult > specialItemResults = const [],
1596
1608
}) {
1597
1609
final PathWrapper <AssetPathEntity >? currentWrapper = context
1598
1610
.select <DefaultAssetPickerProvider , PathWrapper <AssetPathEntity >?>(
@@ -1602,17 +1614,17 @@ class DefaultAssetPickerBuilderDelegate
1602
1614
final int length = assets.length + placeholderCount;
1603
1615
1604
1616
// Return 1 if the [specialItem] build something.
1605
- if (currentPathEntity == null && specialItemModels .isNotEmpty) {
1606
- return placeholderCount + specialItemModels .length;
1617
+ if (currentPathEntity == null && specialItemResults .isNotEmpty) {
1618
+ return placeholderCount + specialItemResults .length;
1607
1619
}
1608
1620
1609
1621
// Return actual length if the current path is all.
1610
1622
// 如果当前目录是全部内容,则返回实际的内容数量。
1611
- if (currentPathEntity? .isAll != true && specialItemModels .isEmpty) {
1623
+ if (currentPathEntity? .isAll != true && specialItemResults .isEmpty) {
1612
1624
return length;
1613
1625
}
1614
1626
1615
- return length + specialItemModels .length;
1627
+ return length + specialItemResults .length;
1616
1628
}
1617
1629
1618
1630
@override
0 commit comments