From 8292f0e15a33816e034ea697e0186013b7dbcc65 Mon Sep 17 00:00:00 2001 From: guozhigq Date: Sun, 20 Oct 2024 00:29:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8A=A8=E6=80=81=E6=A0=B7=E5=BC=8F?= =?UTF-8?q?=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pages/dynamics/view.dart | 72 +++++----- lib/pages/dynamics/widgets/up_panel.dart | 166 +++++++++++++---------- lib/pages/setting/style_setting.dart | 6 + lib/utils/global_data_cache.dart | 4 + lib/utils/storage.dart | 1 + 5 files changed, 144 insertions(+), 105 deletions(-) diff --git a/lib/pages/dynamics/view.dart b/lib/pages/dynamics/view.dart index 38411613..939d6d1f 100644 --- a/lib/pages/dynamics/view.dart +++ b/lib/pages/dynamics/view.dart @@ -11,6 +11,7 @@ import 'package:pilipala/common/widgets/no_data.dart'; import 'package:pilipala/models/dynamics/result.dart'; import 'package:pilipala/plugin/pl_popup/index.dart'; import 'package:pilipala/utils/feed_back.dart'; +import 'package:pilipala/utils/global_data_cache.dart'; import 'package:pilipala/utils/main_stream.dart'; import 'package:pilipala/utils/route_push.dart'; import 'package:pilipala/utils/storage.dart'; @@ -90,32 +91,34 @@ class _DynamicsPageState extends State mainAxisAlignment: MainAxisAlignment.center, children: [ Obx(() { - if (_dynamicsController.mid.value != -1 && - _dynamicsController.upInfo.value.uname != null) { - return SizedBox( - height: 36, - child: AnimatedSwitcher( - duration: const Duration(milliseconds: 300), - transitionBuilder: - (Widget child, Animation animation) { - return ScaleTransition( - scale: animation, child: child); - }, - child: Text( - '${_dynamicsController.upInfo.value.uname!}的动态', - key: ValueKey( - _dynamicsController.upInfo.value.uname!), - style: TextStyle( - fontSize: Theme.of(context) - .textTheme - .labelLarge! - .fontSize, - )), - ), - ); - } else { + final mid = _dynamicsController.mid.value; + final uname = _dynamicsController.upInfo.value.uname; + + if (mid == -1 || uname == null) { return const SizedBox(); } + + return SizedBox( + height: 36, + child: AnimatedSwitcher( + duration: const Duration(milliseconds: 300), + transitionBuilder: + (Widget child, Animation animation) { + return ScaleTransition( + scale: animation, child: child); + }, + child: Text( + '$uname的动态', + key: ValueKey(uname), + style: TextStyle( + fontSize: Theme.of(context) + .textTheme + .labelLarge! + .fontSize, + ), + ), + ), + ); }), Obx( () => _dynamicsController.userLogin.value @@ -207,14 +210,19 @@ class _DynamicsPageState extends State () => UpPanel( upData: _dynamicsController.upData.value, onClickUpCb: (data) { - // _dynamicsController.onTapUp(data); - Navigator.push( - context, - PlPopupRoute( - child: OverlayPanel( - ctr: _dynamicsController, upInfo: data), - ), - ); + if (GlobalDataCache().enableDynamicSwitch) { + Navigator.push( + context, + PlPopupRoute( + child: OverlayPanel( + ctr: _dynamicsController, + upInfo: data, + ), + ), + ); + } else { + _dynamicsController.onTapUp(data); + } }, ), ); diff --git a/lib/pages/dynamics/widgets/up_panel.dart b/lib/pages/dynamics/widgets/up_panel.dart index 3a0edcf6..f45214e3 100644 --- a/lib/pages/dynamics/widgets/up_panel.dart +++ b/lib/pages/dynamics/widgets/up_panel.dart @@ -5,6 +5,7 @@ import 'package:pilipala/common/widgets/network_img_layer.dart'; import 'package:pilipala/models/dynamics/up.dart'; import 'package:pilipala/models/live/item.dart'; import 'package:pilipala/utils/feed_back.dart'; +import 'package:pilipala/utils/global_data_cache.dart'; import 'package:pilipala/utils/utils.dart'; class UpPanel extends StatefulWidget { @@ -23,7 +24,7 @@ class UpPanel extends StatefulWidget { class _UpPanelState extends State { final ScrollController scrollController = ScrollController(); - int currentMid = -1; + RxInt currentMid = (-1).obs; late double contentWidth = 56; List upList = []; List liveList = []; @@ -37,26 +38,36 @@ class _UpPanelState extends State { } void onClickUp(data, i) { - currentMid = data.mid; + currentMid.value = data.mid; widget.onClickUpCb?.call(data); - // int liveLen = liveList.length; - // int upLen = upList.length; - // double itemWidth = contentWidth + itemPadding.horizontal; - // double screenWidth = MediaQuery.sizeOf(context).width; - // double moveDistance = 0.0; - // if (itemWidth * (upList.length + liveList.length) <= screenWidth) { - // } else if ((upLen - i - 0.5) * itemWidth > screenWidth / 2) { - // moveDistance = (i + liveLen + 0.5) * itemWidth + 46 - screenWidth / 2; - // } else { - // moveDistance = (upLen + liveLen) * itemWidth + 46 - screenWidth; - // } - // data.hasUpdate = false; - // scrollController.animateTo( - // moveDistance, - // duration: const Duration(milliseconds: 200), - // curve: Curves.linear, - // ); - // setState(() {}); + } + + void onClickUpAni(data, i) { + final screenWidth = MediaQuery.sizeOf(context).width; + final itemWidth = contentWidth + itemPadding.horizontal; + final liveLen = liveList.length; + final upLen = upList.length; + + currentMid.value = data.mid; + widget.onClickUpCb?.call(data); + + double moveDistance = 0.0; + final totalItemsWidth = itemWidth * (upLen + liveLen); + + if (totalItemsWidth > screenWidth) { + if ((upLen - i - 0.5) * itemWidth > screenWidth / 2) { + moveDistance = (i + liveLen + 0.5) * itemWidth + 46 - screenWidth / 2; + } else { + moveDistance = totalItemsWidth + 46 - screenWidth; + } + } + + data.hasUpdate = false; + scrollController.animateTo( + moveDistance, + duration: const Duration(milliseconds: 500), + curve: Curves.easeInOut, + ); } @override @@ -144,14 +155,17 @@ class _UpPanelState extends State { } Widget upItemBuild(data, i) { - bool isCurrent = currentMid == data.mid || currentMid == -1; return InkWell( onTap: () { feedBack(); if (data.type == 'up') { EasyThrottle.throttle('follow', const Duration(milliseconds: 300), () { - onClickUp(data, i); + if (GlobalDataCache().enableDynamicSwitch) { + onClickUp(data, i); + } else { + onClickUpAni(data, i); + } }); } else if (data.type == 'live') { LiveItemModel liveItem = LiveItemModel.fromJson({ @@ -177,60 +191,66 @@ class _UpPanelState extends State { }, child: Padding( padding: itemPadding, - child: AnimatedOpacity( - opacity: isCurrent ? 1 : 0.3, - duration: const Duration(milliseconds: 200), - curve: Curves.easeInOut, - child: Column( - mainAxisSize: MainAxisSize.min, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Badge( - smallSize: 8, - label: data.type == 'live' ? const Text('Live') : null, - textColor: Theme.of(context).colorScheme.onSecondaryContainer, - alignment: data.type == 'live' - ? AlignmentDirectional.topCenter - : AlignmentDirectional.topEnd, - padding: const EdgeInsets.only(left: 6, right: 6), - isLabelVisible: data.type == 'live' || - (data.type == 'up' && (data.hasUpdate ?? false)), - backgroundColor: data.type == 'live' - ? Theme.of(context).colorScheme.secondaryContainer - : Theme.of(context).colorScheme.primary, - child: data.face != '' - ? NetworkImgLayer( - width: 50, - height: 50, - src: data.face, - type: 'avatar', - ) - : const CircleAvatar( - radius: 25, - backgroundImage: AssetImage( - 'assets/images/noface.jpeg', + child: Obx( + () => AnimatedOpacity( + opacity: currentMid.value == data.mid || currentMid.value == -1 + ? 1 + : 0.3, + duration: const Duration(milliseconds: 200), + curve: Curves.easeInOut, + child: Column( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Badge( + smallSize: 8, + label: data.type == 'live' ? const Text('Live') : null, + textColor: Theme.of(context).colorScheme.onSecondaryContainer, + alignment: data.type == 'live' + ? AlignmentDirectional.topCenter + : AlignmentDirectional.topEnd, + padding: const EdgeInsets.only(left: 6, right: 6), + isLabelVisible: data.type == 'live' || + (data.type == 'up' && (data.hasUpdate ?? false)), + backgroundColor: data.type == 'live' + ? Theme.of(context).colorScheme.secondaryContainer + : Theme.of(context).colorScheme.primary, + child: data.face != '' + ? NetworkImgLayer( + width: 50, + height: 50, + src: data.face, + type: 'avatar', + ) + : const CircleAvatar( + radius: 25, + backgroundImage: AssetImage( + 'assets/images/noface.jpeg', + ), ), - ), - ), - Padding( - padding: const EdgeInsets.only(top: 4), - child: SizedBox( - width: contentWidth, - child: Text( - data.uname, - overflow: TextOverflow.ellipsis, - softWrap: false, - textAlign: TextAlign.center, - style: TextStyle( - color: currentMid == data.mid - ? Theme.of(context).colorScheme.primary - : Theme.of(context).colorScheme.outline, - fontSize: - Theme.of(context).textTheme.labelMedium!.fontSize), + ), + Padding( + padding: const EdgeInsets.only(top: 4), + child: SizedBox( + width: contentWidth, + child: Text( + data.uname, + overflow: TextOverflow.ellipsis, + softWrap: false, + textAlign: TextAlign.center, + style: TextStyle( + color: currentMid.value == data.mid + ? Theme.of(context).colorScheme.primary + : Theme.of(context).colorScheme.outline, + fontSize: Theme.of(context) + .textTheme + .labelMedium! + .fontSize), + ), ), ), - ), - ], + ], + ), ), ), ), diff --git a/lib/pages/setting/style_setting.dart b/lib/pages/setting/style_setting.dart index 80f3b1ed..930a6bde 100644 --- a/lib/pages/setting/style_setting.dart +++ b/lib/pages/setting/style_setting.dart @@ -108,6 +108,12 @@ class _StyleSettingState extends State { defaultVal: true, needReboot: true, ), + const SetSwitchItem( + title: '动态页滑动切换up', + setKey: SettingBoxKey.enableDynamicSwitch, + defaultVal: true, + needReboot: true, + ), ListTile( onTap: () async { int? result = await showDialog( diff --git a/lib/utils/global_data_cache.dart b/lib/utils/global_data_cache.dart index fb1d3400..794d4737 100644 --- a/lib/utils/global_data_cache.dart +++ b/lib/utils/global_data_cache.dart @@ -51,6 +51,8 @@ class GlobalDataCache { late bool enableSearchSuggest = true; // 简介默认展开 late bool enableAutoExpand = false; + // + late bool enableDynamicSwitch = true; // 私有构造函数 GlobalDataCache._(); @@ -116,5 +118,7 @@ class GlobalDataCache { setting.get(SettingBoxKey.enableSearchSuggest, defaultValue: true); enableAutoExpand = setting.get(SettingBoxKey.enableAutoExpand, defaultValue: false); + enableDynamicSwitch = + setting.get(SettingBoxKey.enableDynamicSwitch, defaultValue: true); } } diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart index 46a0dd63..ed14b8df 100644 --- a/lib/utils/storage.dart +++ b/lib/utils/storage.dart @@ -131,6 +131,7 @@ class SettingBoxKey { tabbarSort = 'tabbarSort', // 首页tabbar dynamicBadgeMode = 'dynamicBadgeMode', enableGradientBg = 'enableGradientBg', + enableDynamicSwitch = 'enableDynamicSwitch', navBarSort = 'navBarSort', actionTypeSort = 'actionTypeSort'; }