feat: 直播、推荐、热门封面图片保存

This commit is contained in:
guozhigq
2023-08-18 20:16:15 +08:00
parent b55568ef2a
commit b435023c99
10 changed files with 196 additions and 102 deletions

View File

@ -1,9 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class AnimatedDialog extends StatefulWidget { class AnimatedDialog extends StatefulWidget {
const AnimatedDialog({Key? key, required this.child}) : super(key: key); const AnimatedDialog({Key? key, required this.child, this.closeFn})
: super(key: key);
final Widget child; final Widget child;
final Function? closeFn;
@override @override
State<StatefulWidget> createState() => AnimatedDialogState(); State<StatefulWidget> createState() => AnimatedDialogState();
@ -39,12 +41,16 @@ class AnimatedDialogState extends State<AnimatedDialog>
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Material( return Material(
color: Colors.black.withOpacity(opacityAnimation!.value), color: Colors.black.withOpacity(opacityAnimation!.value),
child: Center( child: InkWell(
child: FadeTransition( splashColor: Colors.transparent,
opacity: scaleAnimation!, onTap: () => widget.closeFn!(),
child: ScaleTransition( child: Center(
scale: scaleAnimation!, child: FadeTransition(
child: widget.child, opacity: scaleAnimation!,
child: ScaleTransition(
scale: scaleAnimation!,
child: widget.child,
),
), ),
), ),
), ),

View File

@ -1,42 +1,82 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pilipala/common/constants.dart'; import 'package:pilipala/common/constants.dart';
import 'package:pilipala/common/widgets/network_img_layer.dart'; import 'package:pilipala/common/widgets/network_img_layer.dart';
import 'package:pilipala/utils/download.dart';
class OverlayPop extends StatelessWidget { class OverlayPop extends StatelessWidget {
final dynamic videoItem; final dynamic videoItem;
const OverlayPop({super.key, this.videoItem}); final Function? closeFn;
const OverlayPop({super.key, this.videoItem, this.closeFn});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
double imgWidth = MediaQuery.of(context).size.width - 8 * 2;
return Container( return Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0), margin: const EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Theme.of(context).colorScheme.background, color: Theme.of(context).colorScheme.background,
borderRadius: BorderRadius.circular(6.0), borderRadius: BorderRadius.circular(10.0),
), ),
child: ClipRRect( child: Column(
borderRadius: BorderRadius.circular(6.0), mainAxisSize: MainAxisSize.min,
child: Column( mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start, children: [
crossAxisAlignment: CrossAxisAlignment.start, Stack(
children: [ children: [
NetworkImgLayer( NetworkImgLayer(
width: (MediaQuery.of(context).size.width - 16), width: imgWidth,
height: (MediaQuery.of(context).size.width - 16) / height: imgWidth / StyleString.aspectRatio,
StyleString.aspectRatio, src: videoItem.pic!,
src: videoItem.pic!, quality: 100,
),
Padding(
padding: const EdgeInsets.fromLTRB(12, 15, 10, 15),
child: Text(
videoItem.title!,
// maxLines: 1,
// overflow: TextOverflow.ellipsis,
), ),
), Positioned(
], right: 8,
), top: 8,
child: Container(
width: 30,
height: 30,
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.3),
borderRadius:
const BorderRadius.all(Radius.circular(20))),
child: IconButton(
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.zero),
),
onPressed: () => closeFn!(),
icon: const Icon(
Icons.close,
size: 18,
color: Colors.white,
),
),
),
),
],
),
Padding(
padding: const EdgeInsets.fromLTRB(12, 10, 8, 10),
child: Row(
children: [
Expanded(
child: Text(
videoItem.title!,
),
),
const SizedBox(width: 4),
IconButton(
tooltip: '保存封面图',
onPressed: () async {
await DownloadUtils.downloadImg(
videoItem.pic ?? videoItem.cover);
// closeFn!();
},
icon: const Icon(Icons.download, size: 20),
)
],
)),
],
), ),
); );
} }

View File

@ -37,11 +37,11 @@ class VideoCardH extends StatelessWidget {
longPress!(); longPress!();
} }
}, },
onLongPressEnd: (details) { // onLongPressEnd: (details) {
if (longPressEnd != null) { // if (longPressEnd != null) {
longPressEnd!(); // longPressEnd!();
} // }
}, // },
child: InkWell( child: InkWell(
onTap: () async { onTap: () async {
try { try {

View File

@ -79,11 +79,11 @@ class VideoCardV extends StatelessWidget {
longPress!(); longPress!();
} }
}, },
onLongPressEnd: (details) { // onLongPressEnd: (details) {
if (longPressEnd != null) { // if (longPressEnd != null) {
longPressEnd!(); // longPressEnd!();
} // }
}, // },
child: InkWell( child: InkWell(
onTap: () async => onPushDetail(heroTag), onTap: () async => onPushDetail(heroTag),
child: Column( child: Column(

View File

@ -126,7 +126,9 @@ class _HotPageState extends State<HotPage> with AutomaticKeepAliveClientMixin {
OverlayEntry _createPopupDialog(videoItem) { OverlayEntry _createPopupDialog(videoItem) {
return OverlayEntry( return OverlayEntry(
builder: (context) => AnimatedDialog( builder: (context) => AnimatedDialog(
child: OverlayPop(videoItem: videoItem), closeFn: _hotController.popupDialog?.remove,
child: OverlayPop(
videoItem: videoItem, closeFn: _hotController.popupDialog?.remove),
), ),
); );
} }

View File

@ -113,7 +113,9 @@ class _LivePageState extends State<LivePage> {
OverlayEntry _createPopupDialog(liveItem) { OverlayEntry _createPopupDialog(liveItem) {
return OverlayEntry( return OverlayEntry(
builder: (context) => AnimatedDialog( builder: (context) => AnimatedDialog(
child: OverlayPop(videoItem: liveItem), closeFn: _liveController.popupDialog?.remove,
child: OverlayPop(
videoItem: liveItem, closeFn: _liveController.popupDialog?.remove),
), ),
); );
} }

View File

@ -35,11 +35,11 @@ class LiveCardV extends StatelessWidget {
longPress!(); longPress!();
} }
}, },
onLongPressEnd: (details) { // onLongPressEnd: (details) {
if (longPressEnd != null) { // if (longPressEnd != null) {
longPressEnd!(); // longPressEnd!();
} // }
}, // },
child: InkWell( child: InkWell(
onTap: () async { onTap: () async {
Get.toNamed('/liveRoom?roomid=${liveItem.roomId}', Get.toNamed('/liveRoom?roomid=${liveItem.roomId}',

View File

@ -128,7 +128,9 @@ class _RcmdPageState extends State<RcmdPage>
OverlayEntry _createPopupDialog(videoItem) { OverlayEntry _createPopupDialog(videoItem) {
return OverlayEntry( return OverlayEntry(
builder: (context) => AnimatedDialog( builder: (context) => AnimatedDialog(
child: OverlayPop(videoItem: videoItem), closeFn: _rcmdController.popupDialog?.remove,
child: OverlayPop(
videoItem: videoItem, closeFn: _rcmdController.popupDialog?.remove),
), ),
); );
} }

View File

@ -6,71 +6,74 @@ import 'package:pilipala/common/widgets/overlay_pop.dart';
import 'package:pilipala/common/widgets/video_card_h.dart'; import 'package:pilipala/common/widgets/video_card_h.dart';
import './controller.dart'; import './controller.dart';
class RelatedVideoPanel extends GetView<ReleatedController> { class RelatedVideoPanel extends StatefulWidget {
const RelatedVideoPanel({super.key}); const RelatedVideoPanel({super.key});
@override
State<RelatedVideoPanel> createState() => _RelatedVideoPanelState();
}
class _RelatedVideoPanelState extends State<RelatedVideoPanel> {
final ReleatedController _releatedController =
Get.put(ReleatedController(), tag: Get.arguments['heroTag']);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GetBuilder( return FutureBuilder(
init: ReleatedController(), future: _releatedController.queryRelatedVideo(),
id: Get.arguments['heroTag'], builder: (context, snapshot) {
builder: (context) { if (snapshot.connectionState == ConnectionState.done) {
return FutureBuilder( if (snapshot.data!['status']) {
future: ReleatedController().queryRelatedVideo(), // 请求成功
builder: (context, snapshot) { return SliverList(
if (snapshot.connectionState == ConnectionState.done) { delegate: SliverChildBuilderDelegate((context, index) {
if (snapshot.data!['status']) { if (index == snapshot.data['data'].length) {
// 请求成功 return SizedBox(height: MediaQuery.of(context).padding.bottom);
return SliverList(
delegate: SliverChildBuilderDelegate((context, index) {
if (index == snapshot.data['data'].length) {
return SizedBox(
height: MediaQuery.of(context).padding.bottom);
} else {
return Material(
child: VideoCardH(
videoItem: snapshot.data['data'][index],
longPress: () {
try {
ReleatedController().popupDialog =
_createPopupDialog(
snapshot.data['data'][index]);
Overlay.of(context)
.insert(ReleatedController().popupDialog!);
} catch (_) {
return {};
}
},
longPressEnd: () {
ReleatedController().popupDialog?.remove();
},
),
);
}
}, childCount: snapshot.data['data'].length + 1));
} else {
// 请求错误
return const Center(
child: Text('出错了'),
);
}
} else { } else {
// 骨架屏 return Material(
return SliverList( child: VideoCardH(
delegate: SliverChildBuilderDelegate((context, index) { videoItem: snapshot.data['data'][index],
return const VideoCardHSkeleton(); longPress: () {
}, childCount: 5), try {
_releatedController.popupDialog =
_createPopupDialog(snapshot.data['data'][index]);
Overlay.of(context)
.insert(_releatedController.popupDialog!);
} catch (err) {
return {};
}
},
longPressEnd: () {
_releatedController.popupDialog?.remove();
},
),
); );
} }
}, }, childCount: snapshot.data['data'].length + 1));
} else {
// 请求错误
return const Center(
child: Text('出错了'),
);
}
} else {
// 骨架屏
return SliverList(
delegate: SliverChildBuilderDelegate((context, index) {
return const VideoCardHSkeleton();
}, childCount: 5),
); );
}); }
},
);
} }
OverlayEntry _createPopupDialog(videoItem) { OverlayEntry _createPopupDialog(videoItem) {
return OverlayEntry( return OverlayEntry(
builder: (context) => AnimatedDialog( builder: (context) => AnimatedDialog(
child: OverlayPop(videoItem: videoItem), closeFn: _releatedController.popupDialog?.remove,
child: OverlayPop(
videoItem: videoItem,
closeFn: _releatedController.popupDialog?.remove),
), ),
); );
} }

39
lib/utils/download.dart Normal file
View File

@ -0,0 +1,39 @@
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';
class DownloadUtils {
// 获取存储全县
static requestStoragePer() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
Permission.photos,
].request();
statuses[Permission.storage].toString();
}
static Future<bool> downloadImg(String imgUrl) async {
await requestStoragePer();
SmartDialog.showLoading(msg: '保存中');
var response = await Dio()
.get(imgUrl, options: Options(responseType: ResponseType.bytes));
String picName =
"plpl_cover_${DateTime.now().toString().split('-').join()}.png";
final result = await ImageGallerySaver.saveImage(
Uint8List.fromList(response.data),
quality: 100,
name: picName,
);
SmartDialog.dismiss();
if (result != null) {
if (result['isSuccess']) {
// ignore: avoid_print
await SmartDialog.showToast('$picName」已保存 ');
}
}
return true;
}
}