Fix player overlay and danmaku rendering

This commit is contained in:
2026-04-08 11:13:52 +08:00
parent fa86c849ca
commit 6b1c7242c7
2 changed files with 295 additions and 120 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@@ -30,14 +32,17 @@ class _PlayerPageState extends State<PlayerPage> {
final ChatService _chatService = ChatService(); final ChatService _chatService = ChatService();
final TextEditingController _msgController = TextEditingController(); final TextEditingController _msgController = TextEditingController();
final List<ChatMessage> _messages = []; final List<ChatMessage> _messages = [];
final List<Widget> _danmakus = []; final List<_DanmakuEntry> _danmakus = [];
bool _isError = false; bool _isError = false;
String? _errorMessage; String? _errorMessage;
bool _showDanmaku = true; bool _showDanmaku = true;
bool _isRefreshing = false; bool _isRefreshing = false;
bool _isFullscreen = false; bool _isFullscreen = false;
bool _controlsVisible = true;
int _playerVersion = 0; int _playerVersion = 0;
String _selectedResolution = 'Source';
Timer? _controlsHideTimer;
@override @override
void initState() { void initState() {
@@ -46,6 +51,7 @@ class _PlayerPageState extends State<PlayerPage> {
_initializePlayer(); _initializePlayer();
} }
_initializeChat(); _initializeChat();
_showControls();
} }
Future<void> _initializePlayer() async { Future<void> _initializePlayer() async {
@@ -99,18 +105,23 @@ class _PlayerPageState extends State<PlayerPage> {
void _addDanmaku(String text) { void _addDanmaku(String text) {
final key = UniqueKey(); final key = UniqueKey();
final top = 20.0 + (DateTime.now().millisecondsSinceEpoch % 6) * 30.0; final lane = DateTime.now().millisecondsSinceEpoch % 8;
final topFactor = 0.06 + lane * 0.045;
final danmaku = _DanmakuItem( setState(() {
_danmakus.add(
_DanmakuEntry(
key: key, key: key,
text: text, text: text,
top: top, topFactor: topFactor,
onFinished: () { onFinished: () {
if (mounted) setState(() => _danmakus.removeWhere((w) => w.key == key)); if (mounted) {
setState(() => _danmakus.removeWhere((w) => w.key == key));
}
}, },
),
); );
});
setState(() => _danmakus.add(danmaku));
} }
void _sendMsg() { void _sendMsg() {
@@ -137,6 +148,7 @@ class _PlayerPageState extends State<PlayerPage> {
_danmakus.clear(); _danmakus.clear();
_playerVersion++; _playerVersion++;
}); });
_showControls();
if (kIsWeb) { if (kIsWeb) {
await Future<void>.delayed(const Duration(milliseconds: 150)); await Future<void>.delayed(const Duration(milliseconds: 150));
@@ -175,6 +187,7 @@ class _PlayerPageState extends State<PlayerPage> {
if (mounted) { if (mounted) {
setState(() => _isFullscreen = nextValue); setState(() => _isFullscreen = nextValue);
} }
_showControls();
} }
void _toggleDanmaku() { void _toggleDanmaku() {
@@ -184,6 +197,74 @@ class _PlayerPageState extends State<PlayerPage> {
_danmakus.clear(); _danmakus.clear();
} }
}); });
_showControls();
}
void _showControls() {
_controlsHideTimer?.cancel();
if (mounted) {
setState(() => _controlsVisible = true);
}
_controlsHideTimer = Timer(const Duration(seconds: 3), () {
if (mounted) {
setState(() => _controlsVisible = false);
}
});
}
void _toggleControlsVisibility() {
if (_controlsVisible) {
_controlsHideTimer?.cancel();
setState(() => _controlsVisible = false);
} else {
_showControls();
}
}
Future<void> _selectResolution() async {
_showControls();
final nextResolution = await showModalBottomSheet<String>(
context: context,
builder: (context) {
const options = ['Source', '720p', '480p'];
return SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const ListTile(
title: Text('Playback Resolution'),
subtitle: Text(
'Current backend only provides the source stream. Lower resolutions are reserved for future multi-bitrate output.',
),
),
...options.map((option) {
final enabled = option == 'Source';
return ListTile(
enabled: enabled,
leading: Icon(
option == _selectedResolution
? Icons.radio_button_checked
: Icons.radio_button_off,
),
title: Text(option),
subtitle: enabled
? const Text('Available now')
: const Text('Requires backend transcoding support'),
onTap: enabled ? () => Navigator.pop(context, option) : null,
);
}),
],
),
);
},
);
if (nextResolution == null || nextResolution == _selectedResolution) {
return;
}
setState(() => _selectedResolution = nextResolution);
await _refreshPlayer();
} }
@override @override
@@ -192,6 +273,7 @@ class _PlayerPageState extends State<PlayerPage> {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
SystemChrome.setPreferredOrientations(DeviceOrientation.values); SystemChrome.setPreferredOrientations(DeviceOrientation.values);
} }
_controlsHideTimer?.cancel();
_controller?.dispose(); _controller?.dispose();
_chatService.dispose(); _chatService.dispose();
_msgController.dispose(); _msgController.dispose();
@@ -203,11 +285,22 @@ class _PlayerPageState extends State<PlayerPage> {
bool isWide = MediaQuery.of(context).size.width > 900; bool isWide = MediaQuery.of(context).size.width > 900;
return Scaffold( return Scaffold(
appBar: AppBar(title: Text(widget.title)), backgroundColor: _isFullscreen
body: isWide ? _buildWideLayout() : _buildMobileLayout(), ? Colors.black
: Theme.of(context).colorScheme.surface,
appBar: _isFullscreen ? null : AppBar(title: Text(widget.title)),
body: _isFullscreen
? _buildFullscreenLayout()
: isWide
? _buildWideLayout()
: _buildMobileLayout(),
); );
} }
Widget _buildFullscreenLayout() {
return SizedBox.expand(child: _buildVideoPanel());
}
// 宽屏布局:左右分栏 // 宽屏布局:左右分栏
Widget _buildWideLayout() { Widget _buildWideLayout() {
return Row( return Row(
@@ -244,18 +337,34 @@ class _PlayerPageState extends State<PlayerPage> {
} }
Widget _buildVideoPanel() { Widget _buildVideoPanel() {
return Container( return MouseRegion(
onHover: (_) => _showControls(),
onEnter: (_) => _showControls(),
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: _toggleControlsVisibility,
onDoubleTap: _toggleFullscreen,
child: Container(
color: Colors.black, color: Colors.black,
child: Column( child: Stack(
children: [ children: [
Expanded(child: _buildVideoWithDanmaku()), Positioned.fill(child: _buildVideoWithDanmaku()),
_buildPlaybackControls(), Positioned(
left: 0,
right: 0,
bottom: 0,
child: _buildPlaybackControls(),
),
], ],
), ),
),
),
); );
} }
Widget _buildVideoWithDanmaku() { Widget _buildVideoWithDanmaku() {
return LayoutBuilder(
builder: (context, constraints) {
return Stack( return Stack(
children: [ children: [
Center( Center(
@@ -276,7 +385,23 @@ class _PlayerPageState extends State<PlayerPage> {
) )
: CircularProgressIndicator(), : CircularProgressIndicator(),
), ),
if (_showDanmaku) ClipRect(child: Stack(children: _danmakus)), if (_showDanmaku)
ClipRect(
child: Stack(
children: _danmakus
.map(
(item) => _DanmakuItem(
key: item.key,
text: item.text,
topFactor: item.topFactor,
containerWidth: constraints.maxWidth,
containerHeight: constraints.maxHeight,
onFinished: item.onFinished,
),
)
.toList(),
),
),
if (_isRefreshing) if (_isRefreshing)
const Positioned( const Positioned(
top: 16, top: 16,
@@ -289,21 +414,74 @@ class _PlayerPageState extends State<PlayerPage> {
), ),
], ],
); );
},
);
}
Color _usernameColor(String username, String type) {
if (type == "system") {
return Colors.blue;
}
final normalized = username.trim().toLowerCase();
var hash = 5381;
for (final codeUnit in normalized.codeUnits) {
hash = ((hash << 5) + hash) ^ codeUnit;
}
final hue = (hash.abs() % 360).toDouble();
return HSLColor.fromAHSL(1, hue, 0.72, 0.68).toColor();
}
Widget _buildMessageItem(ChatMessage message) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: RichText(
text: TextSpan(
style: TextStyle(
color: Theme.of(context).textTheme.bodyMedium?.color,
),
children: [
TextSpan(
text: "${message.username}: ",
style: TextStyle(
fontWeight: FontWeight.bold,
color: _usernameColor(message.username, message.type),
),
),
TextSpan(text: message.content),
],
),
),
);
} }
Widget _buildPlaybackControls() { Widget _buildPlaybackControls() {
return Container( return IgnorePointer(
ignoring: !_controlsVisible,
child: AnimatedOpacity(
opacity: _controlsVisible ? 1 : 0,
duration: const Duration(milliseconds: 220),
child: Container(
width: double.infinity, width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10), padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.black.withValues(alpha: 0.92), gradient: LinearGradient(
border: Border( begin: Alignment.bottomCenter,
top: BorderSide(color: Colors.white.withValues(alpha: 0.08)), end: Alignment.topCenter,
colors: [
Colors.black.withValues(alpha: 0.9),
Colors.black.withValues(alpha: 0.55),
Colors.transparent,
],
), ),
), ),
child: SafeArea(
top: false,
child: Align(
alignment: Alignment.bottomCenter,
child: Wrap( child: Wrap(
spacing: 10, spacing: 10,
runSpacing: 10, runSpacing: 10,
alignment: WrapAlignment.center,
children: [ children: [
_buildControlButton( _buildControlButton(
icon: Icons.refresh, icon: Icons.refresh,
@@ -316,35 +494,36 @@ class _PlayerPageState extends State<PlayerPage> {
onPressed: _toggleDanmaku, onPressed: _toggleDanmaku,
), ),
_buildControlButton( _buildControlButton(
icon: _isFullscreen ? Icons.fullscreen_exit : Icons.fullscreen, icon: _isFullscreen
? Icons.fullscreen_exit
: Icons.fullscreen,
label: _isFullscreen ? "Exit Fullscreen" : "Fullscreen", label: _isFullscreen ? "Exit Fullscreen" : "Fullscreen",
onPressed: _toggleFullscreen, onPressed: _toggleFullscreen,
), ),
_buildControlButton( _buildControlButton(
icon: Icons.high_quality, icon: Icons.high_quality,
label: "Resolution", label: _selectedResolution,
onPressed: () { onPressed: _selectResolution,
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Resolution switching is planned for a later update.",
),
),
);
},
), ),
], ],
), ),
),
),
),
),
); );
} }
Widget _buildControlButton({ Widget _buildControlButton({
required IconData icon, required IconData icon,
required String label, required String label,
required VoidCallback onPressed, required FutureOr<void> Function() onPressed,
}) { }) {
return FilledButton.tonalIcon( return FilledButton.tonalIcon(
onPressed: onPressed, onPressed: () async {
_showControls();
await onPressed();
},
icon: Icon(icon, size: 18), icon: Icon(icon, size: 18),
label: Text(label), label: Text(label),
style: FilledButton.styleFrom( style: FilledButton.styleFrom(
@@ -376,28 +555,7 @@ class _PlayerPageState extends State<PlayerPage> {
itemCount: _messages.length, itemCount: _messages.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final m = _messages[index]; final m = _messages[index];
return Padding( return _buildMessageItem(m);
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: RichText(
text: TextSpan(
style: TextStyle(
color: Theme.of(context).textTheme.bodyMedium?.color,
),
children: [
TextSpan(
text: "${m.username}: ",
style: TextStyle(
fontWeight: FontWeight.bold,
color: m.type == "system"
? Colors.blue
: Theme.of(context).colorScheme.primary,
),
),
TextSpan(text: m.content),
],
),
),
);
}, },
), ),
), ),
@@ -437,15 +595,33 @@ class _PlayerPageState extends State<PlayerPage> {
} }
} }
class _DanmakuEntry {
final Key key;
final String text;
final double topFactor;
final VoidCallback onFinished;
const _DanmakuEntry({
required this.key,
required this.text,
required this.topFactor,
required this.onFinished,
});
}
class _DanmakuItem extends StatefulWidget { class _DanmakuItem extends StatefulWidget {
final String text; final String text;
final double top; final double topFactor;
final double containerWidth;
final double containerHeight;
final VoidCallback onFinished; final VoidCallback onFinished;
const _DanmakuItem({ const _DanmakuItem({
super.key, super.key,
required this.text, required this.text,
required this.top, required this.topFactor,
required this.containerWidth,
required this.containerHeight,
required this.onFinished, required this.onFinished,
}); });
@@ -487,9 +663,8 @@ class __DanmakuItemState extends State<_DanmakuItem>
animation: _animation, animation: _animation,
builder: (context, child) { builder: (context, child) {
return Positioned( return Positioned(
top: widget.top, top: widget.containerHeight * widget.topFactor,
// left 使用 MediaQuery 获取屏幕宽度进行动态计算 left: widget.containerWidth * _animation.value,
left: MediaQuery.of(context).size.width * _animation.value,
child: Text( child: Text(
widget.text, widget.text,
style: TextStyle( style: TextStyle(

View File

@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts # In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix. # of the product and file versions while build-number is used as the build suffix.
version: 1.0.0-beta3.7 version: 1.0.0-beta4.1
environment: environment:
sdk: ^3.11.1 sdk: ^3.11.1