Skip to content

Commit 0b6e335

Browse files
committed
feat(ui): optimize image and video viewing
Closes #89
1 parent d6acb97 commit 0b6e335

File tree

7 files changed

+96
-30
lines changed

7 files changed

+96
-30
lines changed

lib/components/loading/loading.dart

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class EditingLoading extends StatelessWidget {
4949
}
5050
}
5151

52-
class NetworkLoading extends StatelessWidget {
53-
const NetworkLoading({super.key});
52+
class NetworkLoading1 extends StatelessWidget {
53+
const NetworkLoading1({super.key});
5454

5555
@override
5656
Widget build(BuildContext context) {
@@ -64,3 +64,39 @@ class NetworkLoading extends StatelessWidget {
6464
);
6565
}
6666
}
67+
68+
class NetworkLoading2 extends StatelessWidget {
69+
const NetworkLoading2({super.key});
70+
71+
@override
72+
Widget build(BuildContext context) {
73+
final colorScheme = Theme.of(context).colorScheme;
74+
return LoopingRiveIcon(
75+
riveIcon: RiveIcon.globe,
76+
width: 80,
77+
height: 80,
78+
color: colorScheme.onSurface,
79+
strokeWidth: 4.0,
80+
);
81+
}
82+
}
83+
84+
class MediaLoading extends StatelessWidget {
85+
final Color? color;
86+
87+
final double? size;
88+
89+
const MediaLoading({super.key, this.color, this.size});
90+
91+
@override
92+
Widget build(BuildContext context) {
93+
final colorScheme = Theme.of(context).colorScheme;
94+
return LoopingRiveIcon(
95+
riveIcon: RiveIcon.gallery,
96+
width: size ?? 80,
97+
height: size ?? 80,
98+
color: color ?? colorScheme.onSurface,
99+
strokeWidth: 4.0,
100+
);
101+
}
102+
}

lib/components/quill_embed/image_embed.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'dart:io';
22

33
import 'package:flutter/material.dart';
44
import 'package:flutter_quill/flutter_quill.dart';
5+
import 'package:get/get.dart';
6+
import 'package:mood_diary/router/app_routes.dart';
57

68
import '../../utils/file_util.dart';
79

@@ -35,10 +37,20 @@ class ImageEmbedBuilder extends EmbedBuilder {
3537
// 从数据构造 ImageBlockEmbed
3638
final imagePath = isEdit ? imageEmbed.name : FileUtil.getRealPath('image', imageEmbed.name);
3739

38-
return Card.outlined(
39-
clipBehavior: Clip.hardEdge,
40-
color: Colors.transparent,
41-
child: Image.file(File(imagePath)),
40+
return GestureDetector(
41+
onTap: () {
42+
if (!isEdit) {
43+
Get.toNamed(AppRoutes.photoPage, arguments: [
44+
[imagePath],
45+
0,
46+
]);
47+
}
48+
},
49+
child: Card.outlined(
50+
clipBehavior: Clip.hardEdge,
51+
color: Colors.transparent,
52+
child: Image.file(File(imagePath)),
53+
),
4254
);
4355
}
4456
}

lib/components/quill_embed/video_embed.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class VideoEmbedBuilder extends EmbedBuilder {
3232
) {
3333
final videoEmbed = VideoBlockEmbed(embedContext.node.value.data);
3434
final videoPath = isEdit ? videoEmbed.name : FileUtil.getRealPath('video', videoEmbed.name);
35-
35+
final colorScheme = Theme.of(context).colorScheme;
3636
return Card.outlined(
3737
clipBehavior: Clip.hardEdge,
38-
color: Colors.transparent,
38+
color: colorScheme.surfaceContainerLowest,
3939
child: VideoPlayerComponent(
4040
videoPath: videoPath,
4141
),

lib/components/video_player/video_player_logic.dart

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:io';
23

34
import 'package:chewie/chewie.dart';
@@ -12,26 +13,40 @@ class VideoPlayerLogic extends GetxController {
1213
late final videoPlayerController = VideoPlayerController.file(File(state.videoPath));
1314
late final chewieController = ChewieController(
1415
videoPlayerController: videoPlayerController,
15-
aspectRatio: 16 / 9,
1616
useRootNavigator: false,
17-
autoInitialize: true,
1817
);
1918

19+
Completer<void>? _initCompleter;
20+
2021
VideoPlayerLogic({required String videoPath}) {
2122
state.videoPath = videoPath;
2223
}
2324

2425
@override
25-
void onInit() {
26-
Future.delayed(const Duration(milliseconds: 500), () {
27-
state.isInitialized.value = true;
26+
void onReady() async {
27+
_initCompleter = Completer<void>();
28+
Future.delayed(const Duration(milliseconds: 300), () async {
29+
if (!(_initCompleter?.isCompleted ?? true)) {
30+
await videoPlayerController.initialize();
31+
state.isInitialized.value = true;
32+
_initCompleter?.complete();
33+
}
2834
});
29-
super.onInit();
35+
36+
super.onReady();
37+
}
38+
39+
void cancelInitialization() {
40+
if (!(_initCompleter?.isCompleted ?? true)) {
41+
_initCompleter?.complete();
42+
state.isInitialized.value = false;
43+
}
3044
}
3145

3246
@override
33-
void onClose() {
34-
videoPlayerController.dispose();
47+
void onClose() async {
48+
cancelInitialization();
49+
await videoPlayerController.dispose();
3550
chewieController.dispose();
3651
super.onClose();
3752
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import 'package:get/get.dart';
2-
import 'package:media_kit/media_kit.dart';
32

43
class VideoPlayerState {
5-
late Playable playable;
6-
74
late String videoPath;
85

96
RxBool isInitialized = false.obs;
107

11-
VideoPlayerState() {
12-
///Initialize variables
13-
}
8+
VideoPlayerState();
149
}

lib/components/video_player/video_player_view.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:chewie/chewie.dart';
44
import 'package:flutter/material.dart';
55
import 'package:get/get.dart';
66
import 'package:media_kit_video/media_kit_video.dart';
7+
import 'package:mood_diary/components/loading/loading.dart';
78

89
import 'video_player_logic.dart';
910
import 'video_player_state.dart';
@@ -49,11 +50,18 @@ class VideoPlayerComponent extends StatelessWidget {
4950

5051
return AspectRatio(
5152
aspectRatio: 16 / 9,
52-
child: Obx(() {
53-
return state.isInitialized.value
54-
? Chewie(controller: logic.chewieController)
55-
: const Center(child: CircularProgressIndicator());
56-
}),
53+
child: AnimatedSwitcher(
54+
duration: const Duration(milliseconds: 300),
55+
child: Obx(() {
56+
return state.isInitialized.value
57+
? Chewie(controller: logic.chewieController)
58+
: Center(
59+
child: MediaLoading(
60+
color: colorScheme.primary,
61+
size: 56,
62+
));
63+
}),
64+
),
5765
);
5866
}
5967
}

lib/router/app_pages.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ class AppPages {
7373
//日记页路由
7474
GetPage(
7575
name: AppRoutes.diaryPage,
76-
transition: Transition.fadeIn,
76+
//transition: Transition.topLevel,
7777
page: () => DiaryDetailsPage(),
7878
),
7979
//图片路由
8080
GetPage(
8181
name: AppRoutes.photoPage,
8282
page: () => const ImagePage(),
8383
popGesture: false,
84-
transition: Transition.fadeIn,
84+
//transition: Transition.topLevel,
8585
binds: [Bind.lazyPut(() => ImageLogic())],
8686
),
8787
//回收站
@@ -162,9 +162,9 @@ class AppPages {
162162
),
163163
GetPage(
164164
name: AppRoutes.videoPage,
165-
transition: Transition.fadeIn,
166165
popGesture: false,
167166
page: () => const VideoPage(),
167+
// transition: Transition.topLevel,
168168
binds: [Bind.lazyPut(() => VideoLogic())],
169169
),
170170
GetPage(

0 commit comments

Comments
 (0)