fix: 直播间布局异常、控制器初始化异常、imgQuality/cover取值异常

This commit is contained in:
guozhigq
2024-10-11 00:23:27 +08:00
parent 935d56fcb1
commit fb84ba0481
4 changed files with 123 additions and 117 deletions

View File

@ -33,7 +33,11 @@ class NetworkImgLayer extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final int defaultImgQuality = GlobalDataCache().imgQuality; int defaultImgQuality = 10;
try {
defaultImgQuality = GlobalDataCache().imgQuality;
} catch (_) {}
if (src == '' || src == null) { if (src == '' || src == null) {
return placeholder(context); return placeholder(context);
} }

View File

@ -64,7 +64,7 @@ class LiveRoomController extends GetxController {
? liveItem.pic ? liveItem.pic
: (liveItem.cover != null && liveItem.cover != '') : (liveItem.cover != null && liveItem.cover != '')
? liveItem.cover ? liveItem.cover
: null; : '';
} }
Request.getBuvid().then((value) => buvid = value); Request.getBuvid().then((value) => buvid = value);
} }

View File

@ -108,6 +108,12 @@ class _LiveRoomPageState extends State<LiveRoomPage>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final mediaQuery = MediaQuery.of(context);
final isPortrait = mediaQuery.orientation == Orientation.portrait;
final isLandscape = mediaQuery.orientation == Orientation.landscape;
final padding = mediaQuery.padding;
Widget videoPlayerPanel = FutureBuilder( Widget videoPlayerPanel = FutureBuilder(
future: _futureBuilderFuture, future: _futureBuilderFuture,
builder: (BuildContext context, AsyncSnapshot snapshot) { builder: (BuildContext context, AsyncSnapshot snapshot) {
@ -187,10 +193,8 @@ class _LiveRoomPageState extends State<LiveRoomPage>
children: [ children: [
Obx( Obx(
() => SizedBox( () => SizedBox(
height: MediaQuery.of(context).padding.top + height: padding.top +
(_liveRoomController.isPortrait.value || (_liveRoomController.isPortrait.value || isLandscape
MediaQuery.of(context).orientation ==
Orientation.landscape
? 0 ? 0
: kToolbarHeight), : kToolbarHeight),
), ),
@ -201,21 +205,18 @@ class _LiveRoomPageState extends State<LiveRoomPage>
if (plPlayerController.isFullScreen.value == true) { if (plPlayerController.isFullScreen.value == true) {
plPlayerController.triggerFullScreen(status: false); plPlayerController.triggerFullScreen(status: false);
} }
if (MediaQuery.of(context).orientation == if (isLandscape) {
Orientation.landscape) {
verticalScreen(); verticalScreen();
} }
}, },
child: Obx( child: Obx(
() => Container( () => Container(
width: Get.size.width, width: Get.size.width,
height: MediaQuery.of(context).orientation == height: isLandscape
Orientation.landscape
? Get.size.height ? Get.size.height
: !_liveRoomController.isPortrait.value : !_liveRoomController.isPortrait.value
? Get.size.width * 9 / 16 ? Get.size.width * 9 / 16
: Get.size.height - : Get.size.height - padding.top,
MediaQuery.of(context).padding.top,
clipBehavior: Clip.hardEdge, clipBehavior: Clip.hardEdge,
decoration: const BoxDecoration( decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(6)), borderRadius: BorderRadius.all(Radius.circular(6)),
@ -229,7 +230,7 @@ class _LiveRoomPageState extends State<LiveRoomPage>
// 定位 快速滑动到底部 // 定位 快速滑动到底部
Positioned( Positioned(
right: 20, right: 20,
bottom: MediaQuery.of(context).padding.bottom + 80, bottom: padding.bottom + 80,
child: SlideTransition( child: SlideTransition(
position: Tween<Offset>( position: Tween<Offset>(
begin: const Offset(0, 4), begin: const Offset(0, 4),
@ -262,10 +263,7 @@ class _LiveRoomPageState extends State<LiveRoomPage>
titleSpacing: 0, titleSpacing: 0,
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
foregroundColor: Colors.white, foregroundColor: Colors.white,
toolbarHeight: toolbarHeight: isPortrait ? 56 : 0,
MediaQuery.of(context).orientation == Orientation.portrait
? 56
: 0,
title: FutureBuilder( title: FutureBuilder(
future: _futureBuilder, future: _futureBuilder,
builder: (context, snapshot) { builder: (context, snapshot) {
@ -317,35 +315,38 @@ class _LiveRoomPageState extends State<LiveRoomPage>
), ),
// 消息列表 // 消息列表
Obx( Obx(
() => Positioned( () => Align(
top: MediaQuery.of(context).padding.top + alignment: Alignment.bottomCenter,
kToolbarHeight + child: Container(
(_liveRoomController.isPortrait.value margin: EdgeInsets.only(
? Get.size.width bottom: 90 + padding.bottom,
: Get.size.width * 9 / 16), ),
bottom: 90 + MediaQuery.of(context).padding.bottom, height: Get.size.height -
left: 0, (padding.top +
right: 0, kToolbarHeight +
child: buildMessageListUI( (_liveRoomController.isPortrait.value
context, ? Get.size.width
_liveRoomController, : Get.size.width * 9 / 16) +
_scrollController, 100 +
padding.bottom),
child: buildMessageListUI(
context,
_liveRoomController,
_scrollController,
),
), ),
), ),
), ),
// 消息输入框 // 消息输入框
Visibility( Visibility(
visible: MediaQuery.of(context).orientation == Orientation.portrait, visible: isPortrait,
child: Positioned( child: Positioned(
bottom: 0, bottom: 0,
left: 0, left: 0,
right: 0, right: 0,
child: Container( child: Container(
padding: EdgeInsets.only( padding: EdgeInsets.only(
left: 14, left: 14, right: 14, top: 4, bottom: padding.bottom + 20),
right: 14,
top: 4,
bottom: MediaQuery.of(context).padding.bottom + 20),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.grey.withOpacity(0.1), color: Colors.grey.withOpacity(0.1),
borderRadius: const BorderRadius.all(Radius.circular(20)), borderRadius: const BorderRadius.all(Radius.circular(20)),
@ -421,6 +422,7 @@ class _LiveRoomPageState extends State<LiveRoomPage>
], ],
), ),
); );
if (Platform.isAndroid) { if (Platform.isAndroid) {
return PiPSwitcher( return PiPSwitcher(
childWhenDisabled: childWhenDisabled, childWhenDisabled: childWhenDisabled,
@ -438,84 +440,82 @@ Widget buildMessageListUI(
LiveRoomController liveRoomController, LiveRoomController liveRoomController,
ScrollController scrollController, ScrollController scrollController,
) { ) {
return Expanded( return Obx(
child: Obx( () => MediaQuery.removePadding(
() => MediaQuery.removePadding( context: context,
context: context, removeTop: true,
removeTop: true, removeBottom: true,
removeBottom: true, child: ShaderMask(
child: ShaderMask( shaderCallback: (Rect bounds) {
shaderCallback: (Rect bounds) { return LinearGradient(
return LinearGradient( begin: Alignment.topCenter,
begin: Alignment.topCenter, end: Alignment.bottomCenter,
end: Alignment.bottomCenter, colors: [
colors: [ Colors.transparent,
Colors.transparent, Colors.black.withOpacity(0.5),
Colors.black.withOpacity(0.5), Colors.black,
Colors.black, ],
], stops: const [0.01, 0.05, 0.2],
stops: const [0.01, 0.05, 0.2], ).createShader(bounds);
).createShader(bounds); },
blendMode: BlendMode.dstIn,
child: GestureDetector(
onTap: () {
// 键盘失去焦点
FocusScope.of(context).requestFocus(FocusNode());
}, },
blendMode: BlendMode.dstIn, child: ListView.builder(
child: GestureDetector( controller: scrollController,
onTap: () { itemCount: liveRoomController.messageList.length,
// 键盘失去焦点 itemBuilder: (context, index) {
FocusScope.of(context).requestFocus(FocusNode()); final LiveMessageModel liveMsgItem =
}, liveRoomController.messageList[index];
child: ListView.builder( return Align(
controller: scrollController, alignment: Alignment.centerLeft,
itemCount: liveRoomController.messageList.length, child: Container(
itemBuilder: (context, index) { decoration: BoxDecoration(
final LiveMessageModel liveMsgItem = color: liveRoomController.isPortrait.value
liveRoomController.messageList[index]; ? Colors.black.withOpacity(0.3)
return Align( : Colors.grey.withOpacity(0.1),
alignment: Alignment.centerLeft, borderRadius: const BorderRadius.all(Radius.circular(20)),
child: Container( ),
decoration: BoxDecoration( margin: EdgeInsets.only(
color: liveRoomController.isPortrait.value top: index == 0 ? 20.0 : 0.0,
? Colors.black.withOpacity(0.3) bottom: 6.0,
: Colors.grey.withOpacity(0.1), left: 14.0,
borderRadius: const BorderRadius.all(Radius.circular(20)), right: 14.0,
), ),
margin: EdgeInsets.only( padding: const EdgeInsets.symmetric(
top: index == 0 ? 20.0 : 0.0, vertical: 3.0,
bottom: 6.0, horizontal: 10.0,
left: 14.0, ),
right: 14.0, child: Text.rich(
), TextSpan(
padding: const EdgeInsets.symmetric( style: const TextStyle(color: Colors.white),
vertical: 3.0, children: [
horizontal: 10.0, TextSpan(
), text: '${liveMsgItem.userName}: ',
child: Text.rich( style: TextStyle(
TextSpan( color: Colors.white.withOpacity(0.6),
style: const TextStyle(color: Colors.white),
children: [
TextSpan(
text: '${liveMsgItem.userName}: ',
style: TextStyle(
color: Colors.white.withOpacity(0.6),
),
recognizer: TapGestureRecognizer()
..onTap = () {
// 处理点击事件
print('Text clicked');
},
), ),
TextSpan( recognizer: TapGestureRecognizer()
children: [ ..onTap = () {
...buildMessageTextSpan(context, liveMsgItem) // 处理点击事件
], print('Text clicked');
// text: liveMsgItem.message, },
), ),
], TextSpan(
), children: [
...buildMessageTextSpan(context, liveMsgItem)
],
// text: liveMsgItem.message,
),
],
), ),
), ),
); ),
}, );
), },
), ),
), ),
), ),

View File

@ -24,8 +24,8 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
final MainController _mainController = Get.put(MainController()); final MainController _mainController = Get.put(MainController());
late HomeController _homeController; late HomeController _homeController;
RankController? _rankController; RankController? _rankController;
DynamicsController? _dynamicController; late DynamicsController _dynamicController;
MediaController? _mediaController; late MediaController _mediaController;
int? _lastSelectTime; //上次点击时间 int? _lastSelectTime; //上次点击时间
Box setting = GStrorage.setting; Box setting = GStrorage.setting;
@ -76,28 +76,30 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
} }
if (currentPage is DynamicsPage) { if (currentPage is DynamicsPage) {
if (_dynamicController!.flag) { if (_dynamicController.flag) {
// 单击返回顶部 双击并刷新 // 单击返回顶部 双击并刷新
if (DateTime.now().millisecondsSinceEpoch - _lastSelectTime! < 500) { if (DateTime.now().millisecondsSinceEpoch - _lastSelectTime! < 500) {
_dynamicController!.onRefresh(); _dynamicController.onRefresh();
} else { } else {
_dynamicController!.animateToTop(); _dynamicController.animateToTop();
} }
_lastSelectTime = DateTime.now().millisecondsSinceEpoch; _lastSelectTime = DateTime.now().millisecondsSinceEpoch;
} }
_dynamicController!.flag = true; _dynamicController.flag = true;
_mainController.clearUnread(); _mainController.clearUnread();
} else { } else {
_dynamicController?.flag = false; _dynamicController.flag = false;
} }
if (currentPage is MediaPage) { if (currentPage is MediaPage) {
_mediaController!.queryFavFolder(); _mediaController.queryFavFolder();
} }
} }
void controllerInit() { void controllerInit() {
_homeController = Get.put(HomeController()); _homeController = Get.put(HomeController());
_dynamicController = Get.put(DynamicsController());
_mediaController = Get.put(MediaController());
if (_mainController.pagesIds.contains(1)) { if (_mainController.pagesIds.contains(1)) {
_rankController = Get.put(RankController()); _rankController = Get.put(RankController());
} }