Skip to content

Commit ecb2cdd

Browse files
yujuneyujuneteeAlexV525
authored
Add Auto Play Flag In DefaultAssetPickerViewerBuilderDelegate (#585)
Add an autoplay flag to autoplay video, audio, and live photos. Co-authored-by: yujune <[email protected]> Co-authored-by: Alex Li <[email protected]>
1 parent 333ff71 commit ecb2cdd

11 files changed

+56
-4
lines changed

README-ZH.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ final List<AssetEntity>? result = await AssetPicker.pickAssets(
303303
| pathNameBuilder | `PathNameBuilder<AssetPathEntity>?` | 基于路径(相册)构建自定义名称的方法 | `null` |
304304
| assetsChangeCallback | `AssetsChangeCallback<AssetPathEntity>?` | 当系统通知资源变化时将调用的回调 | `null` |
305305
| assetsChangeRefreshPredicate | `AssetsChangeRefreshPredicate<AssetPathEntity>?` | 判断资源变化是否根据 call 和当前选中的路径进行更新 | `null` |
306+
| shouldAutoplayPreview | `bool` | 预览是否应自动播放 | `false` |
306307

307308
-`maxAssets` 等于 `1`(即单选模式),搭配
308309
`SpecialPickerType.noPreview` 使用会在用户点选资源换时立刻选中并返回。

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ Fields in `AssetPickerConfig`:
312312
| pathNameBuilder | `PathNameBuilder<AssetPathEntity>?` | Build customized path (album) name with the given path entity. | `null` |
313313
| assetsChangeCallback | `AssetsChangeCallback<AssetPathEntity>?` | The callback that will be called when the system notifies assets changes. | `null` |
314314
| assetsChangeRefreshPredicate | `AssetsChangeRefreshPredicate<AssetPathEntity>?` | Whether assets changing should call refresh with the given call and the current selected path. | `null` |
315+
| shouldAutoPlayPreview | `bool` | Whether the preview should auto play. | `false` |
315316

316317
- When `maxAssets` equals to `1` (a.k.a. single picking mode),
317318
use `SpecialPickerType.noPreview` will immediately select asset

lib/src/constants/config.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class AssetPickerConfig {
3838
this.pathNameBuilder,
3939
this.assetsChangeCallback,
4040
this.assetsChangeRefreshPredicate,
41+
this.shouldAutoplayPreview = false,
4142
}) : assert(
4243
pickerTheme == null || themeColor == null,
4344
'pickerTheme and themeColor cannot be set at the same time.',
@@ -200,4 +201,8 @@ class AssetPickerConfig {
200201
/// {@macro wechat_assets_picker.AssetsChangeRefreshPredicate}
201202
final AssetsChangeRefreshPredicate<AssetPathEntity>?
202203
assetsChangeRefreshPredicate;
204+
205+
/// Whether the preview should auto play.
206+
/// 预览是否自动播放
207+
final bool shouldAutoplayPreview;
203208
}

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ class DefaultAssetPickerBuilderDelegate
718718
this.previewThumbnailSize,
719719
this.specialPickerType,
720720
this.keepScrollOffset = false,
721+
this.shouldAutoplayPreview = false,
721722
}) {
722723
// Add the listener if [keepScrollOffset] is true.
723724
if (keepScrollOffset) {
@@ -772,6 +773,10 @@ class DefaultAssetPickerBuilderDelegate
772773
/// 选择器是否可以从同样的位置开始选择
773774
final bool keepScrollOffset;
774775

776+
/// Whether the preview should auto play.
777+
/// 预览是否自动播放
778+
final bool shouldAutoplayPreview;
779+
775780
/// [Duration] when triggering path switching.
776781
/// 切换路径时的动画时长
777782
Duration get switchingPathDuration => const Duration(milliseconds: 300);
@@ -1009,6 +1014,7 @@ class DefaultAssetPickerBuilderDelegate
10091014
specialPickerType: specialPickerType,
10101015
maxAssets: p.maxAssets,
10111016
shouldReversePreview: revert,
1017+
shouldAutoplayPreview: shouldAutoplayPreview,
10121018
);
10131019
if (result != null) {
10141020
Navigator.maybeOf(context)?.maybePop(result);

lib/src/delegates/asset_picker_delegate.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class AssetPickerDelegate {
118118
textDelegate: pickerConfig.textDelegate,
119119
themeColor: pickerConfig.themeColor,
120120
locale: Localizations.maybeLocaleOf(context),
121+
shouldAutoplayPreview: pickerConfig.shouldAutoplayPreview,
121122
),
122123
);
123124
final List<AssetEntity>? result = await Navigator.maybeOf(

lib/src/delegates/asset_picker_viewer_builder_delegate.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,13 @@ class DefaultAssetPickerViewerBuilderDelegate
390390
super.maxAssets,
391391
super.shouldReversePreview,
392392
super.selectPredicate,
393+
this.shouldAutoplayPreview = false,
393394
});
394395

396+
/// Whether the preview should auto play.
397+
/// 预览是否自动播放
398+
final bool shouldAutoplayPreview;
399+
395400
/// Thumb size for the preview of images in the viewer.
396401
/// 预览时图片的缩略图大小
397402
final ThumbnailSize? previewThumbnailSize;
@@ -421,16 +426,21 @@ class DefaultAssetPickerViewerBuilderDelegate
421426
shouldReversePreview ? previewAssets.length - index - 1 : index,
422427
);
423428
final Widget builder = switch (asset.type) {
424-
AssetType.audio => AudioPageBuilder(asset: asset),
429+
AssetType.audio => AudioPageBuilder(
430+
asset: asset,
431+
shouldAutoplayPreview: shouldAutoplayPreview,
432+
),
425433
AssetType.image => ImagePageBuilder(
426434
asset: asset,
427435
delegate: this,
428436
previewThumbnailSize: previewThumbnailSize,
437+
shouldAutoplayPreview: shouldAutoplayPreview,
429438
),
430439
AssetType.video => VideoPageBuilder(
431440
asset: asset,
432441
delegate: this,
433442
hasOnlyOneVideoAndMoment: isWeChatMoment && hasVideo,
443+
shouldAutoplayPreview: shouldAutoplayPreview,
434444
),
435445
AssetType.other => Center(
436446
child: ScaleText(

lib/src/widget/asset_picker_viewer.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class AssetPickerViewer<Asset, Path> extends StatefulWidget {
4343
AssetSelectPredicate<AssetEntity>? selectPredicate,
4444
PermissionRequestOption permissionRequestOption =
4545
const PermissionRequestOption(),
46+
bool shouldAutoplayPreview = false,
4647
}) async {
4748
await AssetPicker.permissionCheck(requestOption: permissionRequestOption);
4849
final Widget viewer = AssetPickerViewer<AssetEntity, AssetPathEntity>(
@@ -65,6 +66,7 @@ class AssetPickerViewer<Asset, Path> extends StatefulWidget {
6566
maxAssets: maxAssets,
6667
shouldReversePreview: shouldReversePreview,
6768
selectPredicate: selectPredicate,
69+
shouldAutoplayPreview: shouldAutoplayPreview,
6870
),
6971
);
7072
final PageRouteBuilder<List<AssetEntity>> pageRoute =

lib/src/widget/builder/audio_page_builder.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ import '../../constants/constants.dart';
1313
import '../../internals/singleton.dart';
1414

1515
class AudioPageBuilder extends StatefulWidget {
16-
const AudioPageBuilder({super.key, required this.asset});
16+
const AudioPageBuilder({
17+
super.key,
18+
required this.asset,
19+
this.shouldAutoplayPreview = false,
20+
});
1721

1822
/// Asset currently displayed.
1923
/// 展示的资源
2024
final AssetEntity asset;
2125

26+
/// Whether the preview should auto play.
27+
/// 预览是否自动播放
28+
final bool shouldAutoplayPreview;
29+
2230
@override
2331
State<StatefulWidget> createState() => _AudioPageBuilderState();
2432
}
@@ -92,6 +100,9 @@ class _AudioPageBuilderState extends State<AudioPageBuilder> {
92100
_controller = VideoPlayerController.networkUrl(Uri.parse(url!));
93101
await controller.initialize();
94102
controller.addListener(audioPlayerListener);
103+
if (widget.shouldAutoplayPreview) {
104+
controller.play();
105+
}
95106
} catch (e, s) {
96107
FlutterError.presentError(
97108
FlutterErrorDetails(

lib/src/widget/builder/image_page_builder.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ImagePageBuilder extends StatefulWidget {
2020
required this.asset,
2121
required this.delegate,
2222
this.previewThumbnailSize,
23+
this.shouldAutoplayPreview = false,
2324
});
2425

2526
/// Asset currently displayed.
@@ -30,6 +31,10 @@ class ImagePageBuilder extends StatefulWidget {
3031

3132
final ThumbnailSize? previewThumbnailSize;
3233

34+
/// Whether the preview should auto play.
35+
/// 预览是否自动播放
36+
final bool shouldAutoplayPreview;
37+
3338
@override
3439
State<ImagePageBuilder> createState() => _ImagePageBuilderState();
3540
}
@@ -79,7 +84,11 @@ class _ImagePageBuilderState extends State<ImagePageBuilder> {
7984
_controller = c;
8085
});
8186
c
82-
..initialize()
87+
..initialize().then((_) {
88+
if (widget.shouldAutoplayPreview) {
89+
_play();
90+
}
91+
})
8392
..setVolume(0)
8493
..addListener(() {
8594
if (mounted) {

lib/src/widget/builder/video_page_builder.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class VideoPageBuilder extends StatefulWidget {
1919
required this.asset,
2020
required this.delegate,
2121
this.hasOnlyOneVideoAndMoment = false,
22+
this.shouldAutoplayPreview = false,
2223
});
2324

2425
/// Asset currently displayed.
@@ -31,6 +32,10 @@ class VideoPageBuilder extends StatefulWidget {
3132
/// 是否处于 [SpecialPickerType.wechatMoment] 且只有一个视频
3233
final bool hasOnlyOneVideoAndMoment;
3334

35+
/// Whether the preview should auto play.
36+
/// 预览是否自动播放
37+
final bool shouldAutoplayPreview;
38+
3439
@override
3540
State<VideoPageBuilder> createState() => _VideoPageBuilderState();
3641
}
@@ -113,7 +118,7 @@ class _VideoPageBuilderState extends State<VideoPageBuilder> {
113118
controller
114119
..addListener(videoPlayerListener)
115120
..setLooping(widget.hasOnlyOneVideoAndMoment);
116-
if (widget.hasOnlyOneVideoAndMoment) {
121+
if (widget.hasOnlyOneVideoAndMoment || widget.shouldAutoplayPreview) {
117122
controller.play();
118123
}
119124
} catch (e, s) {

0 commit comments

Comments
 (0)