Toggle original or compressed one via video control

This commit is contained in:
LittleSheep 2024-12-28 19:59:04 +08:00
parent bb66d5b684
commit 44f2c5fe0e

View File

@ -1,7 +1,9 @@
import 'dart:io';
import 'dart:ui'; import 'dart:ui';
import 'dart:math' as math; import 'dart:math' as math;
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:gap/gap.dart'; import 'package:gap/gap.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
@ -62,14 +64,12 @@ class AttachmentItem extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (data!.contentRating > 0) { if (data!.contentRating > 0) {
return LayoutBuilder( return LayoutBuilder(builder: (context, constraints) {
builder: (context, constraints) { return _AttachmentItemSensitiveBlur(
return _AttachmentItemSensitiveBlur( isCompact: constraints.maxHeight < 360,
isCompact: constraints.maxHeight < 360, child: _buildContent(context),
child: _buildContent(context), );
); });
}
);
} }
return _buildContent(context); return _buildContent(context);
@ -176,6 +176,7 @@ class _AttachmentItemContentVideo extends StatefulWidget {
class _AttachmentItemContentVideoState extends State<_AttachmentItemContentVideo> { class _AttachmentItemContentVideoState extends State<_AttachmentItemContentVideo> {
bool _showContent = false; bool _showContent = false;
bool _showOriginal = false;
Player? _videoPlayer; Player? _videoPlayer;
VideoController? _videoController; VideoController? _videoController;
@ -184,15 +185,29 @@ class _AttachmentItemContentVideoState extends State<_AttachmentItemContentVideo
setState(() => _showContent = true); setState(() => _showContent = true);
MediaKit.ensureInitialized(); MediaKit.ensureInitialized();
final sn = context.read<SnNetworkProvider>(); final sn = context.read<SnNetworkProvider>();
final url = sn.getAttachmentUrl(widget.data.rid); final url = _showOriginal ? sn.getAttachmentUrl(widget.data.rid) : sn.getAttachmentUrl(widget.data.compressed!.rid);
_videoPlayer = Player(); _videoPlayer = Player();
_videoController = VideoController(_videoPlayer!); _videoController = VideoController(_videoPlayer!);
_videoPlayer!.open(Media(url), play: !widget.isAutoload); _videoPlayer!.open(Media(url), play: !widget.isAutoload);
} }
void _toggleOriginal() {
if (!mounted) return;
if (widget.data.compressedId == null) return;
setState(() => _showOriginal = !_showOriginal);
final sn = context.read<SnNetworkProvider>();
_videoPlayer?.open(
Media(
_showOriginal ? sn.getAttachmentUrl(widget.data.rid) : sn.getAttachmentUrl(widget.data.compressed!.rid),
),
play: true,
);
}
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_showOriginal = widget.data.compressedId == null;
if (widget.isAutoload) _startLoad(); if (widget.isAutoload) _startLoad();
} }
@ -297,9 +312,43 @@ class _AttachmentItemContentVideoState extends State<_AttachmentItemContentVideo
); );
} }
return Video( return MaterialDesktopVideoControlsTheme(
controller: _videoController!, normal: MaterialDesktopVideoControlsThemeData(
aspectRatio: ratio, buttonBarButtonSize: 24,
buttonBarButtonColor: Colors.white,
topButtonBarMargin: EdgeInsets.symmetric(horizontal: 12, vertical: 2),
topButtonBar: [
const Spacer(),
MaterialDesktopCustomButton(
iconSize: 24,
onPressed: _toggleOriginal,
icon: _showOriginal ? const Icon(Symbols.high_quality, size: 24) : const Icon(Symbols.sd, size: 24),
),
],
),
fullscreen: const MaterialDesktopVideoControlsThemeData(),
child: MaterialVideoControlsTheme(
normal: MaterialVideoControlsThemeData(
buttonBarButtonSize: 24,
buttonBarButtonColor: Colors.white,
topButtonBarMargin: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
topButtonBar: [
const Spacer(),
MaterialDesktopCustomButton(
iconSize: 24,
onPressed: _toggleOriginal,
icon: _showOriginal ? const Icon(Symbols.high_quality, size: 24) : const Icon(Symbols.sd, size: 24),
),
],
),
fullscreen: const MaterialVideoControlsThemeData(),
child: Video(
controller: _videoController!,
aspectRatio: ratio,
controls:
!kIsWeb && (Platform.isAndroid || Platform.isIOS) ? MaterialVideoControls : MaterialDesktopVideoControls,
),
),
); );
} }