Skip to content

Commit f00976f

Browse files
committed
♻️ Improve AssetPickerViewerBuilderDelegate's abstraction
1 parent 6800dc6 commit f00976f

File tree

2 files changed

+57
-123
lines changed

2 files changed

+57
-123
lines changed

example/lib/customs/pickers/directory_file_asset_picker.dart

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,13 +1200,6 @@ class FileAssetPickerViewerBuilderDelegate
12001200

12011201
bool _isDisplayingDetail = true;
12021202

1203-
late final AnimationController _doubleTapAnimationController;
1204-
late final Animation<double> _doubleTapCurveAnimation;
1205-
Animation<double>? _doubleTapAnimation;
1206-
late VoidCallback _doubleTapListener;
1207-
1208-
late final PageController pageController;
1209-
12101203
AssetsPickerTextDelegate get textDelegate => AssetsPickerTextDelegate();
12111204

12121205
@override
@@ -1218,30 +1211,6 @@ class FileAssetPickerViewerBuilderDelegate
12181211
}
12191212
}
12201213

1221-
@override
1222-
void updateAnimation(ExtendedImageGestureState state) {
1223-
final double begin = state.gestureDetails!.totalScale!;
1224-
final double end = state.gestureDetails!.totalScale! == 1.0 ? 3.0 : 1.0;
1225-
final Offset pointerDownPosition = state.pointerDownPosition!;
1226-
1227-
_doubleTapAnimation?.removeListener(_doubleTapListener);
1228-
_doubleTapAnimationController
1229-
..stop()
1230-
..reset();
1231-
_doubleTapListener = () {
1232-
state.handleDoubleTap(
1233-
scale: _doubleTapAnimation!.value,
1234-
doubleTapPosition: pointerDownPosition,
1235-
);
1236-
};
1237-
_doubleTapAnimation = Tween<double>(
1238-
begin: begin,
1239-
end: end,
1240-
).animate(_doubleTapCurveAnimation)
1241-
..addListener(_doubleTapListener);
1242-
_doubleTapAnimationController.forward();
1243-
}
1244-
12451214
@override
12461215
Widget assetPageBuilder(BuildContext context, int index) {
12471216
final File asset = previewAssets.elementAt(index);
@@ -1515,29 +1484,6 @@ class FileAssetPickerViewerBuilderDelegate
15151484
);
15161485
}
15171486

1518-
@override
1519-
void initStateAndTicker(
1520-
AssetPickerViewerState<File, Directory> s,
1521-
TickerProvider v,
1522-
) {
1523-
super.initStateAndTicker(s, v);
1524-
_doubleTapAnimationController = AnimationController(
1525-
duration: const Duration(milliseconds: 200),
1526-
vsync: v,
1527-
);
1528-
_doubleTapCurveAnimation = CurvedAnimation(
1529-
parent: _doubleTapAnimationController,
1530-
curve: Curves.easeInOut,
1531-
);
1532-
pageController = PageController(initialPage: currentIndex);
1533-
}
1534-
1535-
@override
1536-
void dispose() {
1537-
_doubleTapAnimationController.dispose();
1538-
pageStreamController.close();
1539-
}
1540-
15411487
@override
15421488
Widget selectButton(BuildContext context) {
15431489
return Row(

lib/src/delegates/asset_picker_viewer_builder_delegate.dart

Lines changed: 57 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ abstract class AssetPickerViewerBuilderDelegate<Asset, Path> {
7272
/// 用于动画的 [TickerProvider]
7373
late final TickerProvider vsync;
7474

75+
/// [AnimationController] for double tap animation.
76+
/// 双击缩放的动画控制器
77+
late final AnimationController doubleTapAnimationController;
78+
79+
/// [CurvedAnimation] for double tap.
80+
/// 双击缩放的动画曲线
81+
late final Animation<double> doubleTapCurveAnimation;
82+
83+
/// [Animation] for double tap.
84+
/// 双击缩放的动画
85+
Animation<double>? doubleTapAnimation;
86+
87+
/// Callback for double tap.
88+
/// 双击缩放的回调
89+
late VoidCallback doubleTapListener;
90+
91+
/// [PageController] for assets preview [PageView].
92+
/// 查看图片资源的页面控制器
93+
late final PageController pageController = PageController(
94+
initialPage: currentIndex,
95+
);
96+
7597
/// Current previewing index in assets.
7698
/// 当前查看的索引
7799
int currentIndex;
@@ -110,21 +132,55 @@ abstract class AssetPickerViewerBuilderDelegate<Asset, Path> {
110132
) {
111133
viewerState = s;
112134
vsync = v;
135+
doubleTapAnimationController = AnimationController(
136+
duration: const Duration(milliseconds: 200),
137+
vsync: v,
138+
);
139+
doubleTapCurveAnimation = CurvedAnimation(
140+
parent: doubleTapAnimationController,
141+
curve: Curves.easeInOut,
142+
);
113143
}
114144

115145
/// Keep a dispose method to sync with [State].
116146
/// 保留一个 dispose 方法与 [State] 同步。
117147
void dispose() {
118148
provider?.dispose();
149+
pageController.dispose();
119150
pageStreamController.close();
120151
previewingListController.dispose();
121152
selectedNotifier.dispose();
122153
isDisplayingDetail.dispose();
154+
doubleTapAnimationController
155+
..stop()
156+
..reset()
157+
..dispose();
123158
}
124159

125160
/// Execute scale animation when double tap.
126161
/// 双击时执行缩放动画
127-
void updateAnimation(ExtendedImageGestureState state);
162+
void updateAnimation(ExtendedImageGestureState state) {
163+
final double begin = state.gestureDetails!.totalScale!;
164+
final double end = state.gestureDetails!.totalScale! == 1.0 ? 3.0 : 1.0;
165+
final Offset pointerDownPosition = state.pointerDownPosition!;
166+
167+
doubleTapAnimation?.removeListener(doubleTapListener);
168+
doubleTapAnimationController
169+
..stop()
170+
..reset();
171+
doubleTapListener = () {
172+
state.handleDoubleTap(
173+
scale: doubleTapAnimation!.value,
174+
doubleTapPosition: pointerDownPosition,
175+
);
176+
};
177+
doubleTapAnimation = Tween<double>(
178+
begin: begin,
179+
end: end,
180+
).animate(doubleTapCurveAnimation)
181+
..addListener(doubleTapListener);
182+
doubleTapAnimationController.forward();
183+
}
128184

129185
/// The length getter for selected assets currently.
130186
/// 当前选中的资源的长度获取
@@ -273,27 +329,6 @@ class DefaultAssetPickerViewerBuilderDelegate
273329
/// 如果类型不为空,则标题将不会显示。
274330
final SpecialPickerType? specialPickerType;
275331

276-
/// [AnimationController] for double tap animation.
277-
/// 双击缩放的动画控制器
278-
late final AnimationController _doubleTapAnimationController;
279-
280-
/// [CurvedAnimation] for double tap.
281-
/// 双击缩放的动画曲线
282-
late final Animation<double> _doubleTapCurveAnimation;
283-
284-
/// [Animation] for double tap.
285-
/// 双击缩放的动画
286-
Animation<double>? _doubleTapAnimation;
287-
288-
/// Callback for double tap.
289-
/// 双击缩放的回调
290-
late VoidCallback _doubleTapListener;
291-
292-
/// [PageController] for assets preview [PageView].
293-
/// 查看图片资源的页面控制器
294-
late final PageController pageController =
295-
PageController(initialPage: currentIndex);
296-
297332
/// Whether the [SpecialPickerType.wechatMoment] is enabled.
298333
/// 当前是否为微信朋友圈选择模式
299334
bool get isWeChatMoment =>
@@ -306,53 +341,6 @@ class DefaultAssetPickerViewerBuilderDelegate
306341
(selectedAssets?.any((AssetEntity e) => e.type == AssetType.video) ??
307342
false);
308343

309-
@override
310-
void initStateAndTicker(
311-
AssetPickerViewerState<AssetEntity, AssetPathEntity> s,
312-
TickerProvider v,
313-
) {
314-
super.initStateAndTicker(s, v);
315-
_doubleTapAnimationController = AnimationController(
316-
duration: const Duration(milliseconds: 200),
317-
vsync: v,
318-
);
319-
_doubleTapCurveAnimation = CurvedAnimation(
320-
parent: _doubleTapAnimationController,
321-
curve: Curves.easeInOut,
322-
);
323-
}
324-
325-
@override
326-
void dispose() {
327-
_doubleTapAnimationController.dispose();
328-
pageController.dispose();
329-
super.dispose();
330-
}
331-
332-
@override
333-
void updateAnimation(ExtendedImageGestureState state) {
334-
final double begin = state.gestureDetails!.totalScale!;
335-
final double end = state.gestureDetails!.totalScale! == 1.0 ? 3.0 : 1.0;
336-
final Offset pointerDownPosition = state.pointerDownPosition!;
337-
338-
_doubleTapAnimation?.removeListener(_doubleTapListener);
339-
_doubleTapAnimationController
340-
..stop()
341-
..reset();
342-
_doubleTapListener = () {
343-
state.handleDoubleTap(
344-
scale: _doubleTapAnimation!.value,
345-
doubleTapPosition: pointerDownPosition,
346-
);
347-
};
348-
_doubleTapAnimation = Tween<double>(
349-
begin: begin,
350-
end: end,
351-
).animate(_doubleTapCurveAnimation)
352-
..addListener(_doubleTapListener);
353-
_doubleTapAnimationController.forward();
354-
}
355-
356344
@override
357345
Widget assetPageBuilder(BuildContext context, int index) {
358346
final AssetEntity asset = previewAssets.elementAt(index);

0 commit comments

Comments
 (0)