mod: replace stream

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-04-10 09:29:29 +08:00
parent cef7bfd534
commit 796494e53f
3 changed files with 24 additions and 54 deletions

View File

@@ -1,4 +1,3 @@
import 'dart:async';
import 'dart:math';
import 'package:PiliPlus/common/widgets/custom_sliver_persistent_header_delegate.dart';
@@ -41,8 +40,7 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
with TickerProviderStateMixin {
late DynamicDetailController _dynamicDetailController;
AnimationController? _fabAnimationCtr;
late StreamController<bool> _titleStreamC; // appBar title
bool _visibleTitle = false;
final RxBool _visibleTitle = false.obs;
// String? action;
// 回复类型
late int replyType;
@@ -105,7 +103,6 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
super.initState();
// floor 1原创 2转发
init();
_titleStreamC = StreamController<bool>();
// if (action == 'comment') {
// _visibleTitle = true;
// _titleStreamC.add(true);
@@ -240,15 +237,8 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
void listener() {
// 标题
if (_dynamicDetailController.scrollController.positions.length == 1) {
if (_dynamicDetailController.scrollController.offset > 55 &&
!_visibleTitle) {
_visibleTitle = true;
_titleStreamC.add(true);
} else if (_dynamicDetailController.scrollController.offset <= 55 &&
_visibleTitle) {
_visibleTitle = false;
_titleStreamC.add(false);
}
_visibleTitle.value =
_dynamicDetailController.scrollController.offset > 55;
}
// fab按钮
@@ -281,7 +271,6 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
@override
void dispose() {
_titleStreamC.close();
_fabAnimationCtr?.dispose();
_fabAnimationCtr = null;
_dynamicDetailController.scrollController.removeListener(listener);
@@ -293,12 +282,10 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: StreamBuilder(
stream: _titleStreamC.stream.distinct(),
initialData: false,
builder: (context, AsyncSnapshot snapshot) {
title: Obx(
() {
return AnimatedOpacity(
opacity: snapshot.data ? 1 : 0,
opacity: _visibleTitle.value ? 1 : 0,
duration: const Duration(milliseconds: 300),
child: AuthorPanel(
item: _dynamicDetailController.item,

View File

@@ -26,23 +26,18 @@ class _SubDetailPageState extends State<SubDetailPage> {
late final SubDetailController _subDetailController = Get.put(
SubDetailController(),
tag: Utils.makeHeroTag(Get.parameters['id']));
late StreamController<bool> titleStreamC;
final RxBool showTitle = false.obs;
late Future _futureBuilderFuture;
@override
void initState() {
super.initState();
_futureBuilderFuture = _subDetailController.queryUserSubFolderDetail();
titleStreamC = StreamController<bool>();
_controller.addListener(listener);
}
void listener() {
if (_controller.offset > 160) {
titleStreamC.add(true);
} else if (_controller.offset <= 160) {
titleStreamC.add(false);
}
showTitle.value = _controller.offset > 160;
if (_controller.position.pixels >=
_controller.position.maxScrollExtent - 200) {
@@ -54,7 +49,6 @@ class _SubDetailPageState extends State<SubDetailPage> {
@override
void dispose() {
titleStreamC.close();
_controller.removeListener(listener);
_controller.dispose();
super.dispose();
@@ -70,12 +64,10 @@ class _SubDetailPageState extends State<SubDetailPage> {
SliverAppBar(
expandedHeight: 215 - MediaQuery.of(context).padding.top,
pinned: true,
title: StreamBuilder(
stream: titleStreamC.stream.distinct(),
initialData: false,
builder: (context, AsyncSnapshot snapshot) {
title: Obx(
() {
return AnimatedOpacity(
opacity: snapshot.data ? 1 : 0,
opacity: showTitle.value ? 1 : 0,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 500),
child: Row(

View File

@@ -1,4 +1,3 @@
import 'dart:async';
import 'dart:io';
import 'package:PiliPlus/http/init.dart';
@@ -50,8 +49,8 @@ class WebviewPageNew extends StatefulWidget {
class _WebviewPageNewState extends State<WebviewPageNew> {
late final String _url = widget.url ?? Get.parameters['url'] ?? '';
late final uaType = widget.uaType ?? Get.parameters['uaType'] ?? 'mob';
final _titleStream = StreamController<String?>();
final _progressStream = StreamController<double>();
final RxString title = ''.obs;
final RxDouble progress = 1.0.obs;
bool? _inApp;
bool? _off;
@@ -68,8 +67,6 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
@override
void dispose() {
_titleStream.close();
_progressStream.close();
_webViewController = null;
super.dispose();
}
@@ -80,24 +77,18 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
appBar: widget.url != null
? null
: AppBar(
title: StreamBuilder(
initialData: null,
stream: _titleStream.stream.distinct(),
builder: (context, snapshot) => Text(
title: Obx(
() => Text(
title.value.isNotEmpty ? title.value : _url,
maxLines: 1,
snapshot.hasData ? snapshot.data! : _url,
overflow: TextOverflow.ellipsis,
),
),
bottom: PreferredSize(
preferredSize: Size.zero,
child: StreamBuilder(
initialData: 0.0,
stream: _progressStream.stream.distinct(),
builder: (context, snapshot) => snapshot.data as double < 1
? LinearProgressIndicator(
value: snapshot.data as double,
)
child: Obx(
() => progress.value < 1
? LinearProgressIndicator(value: progress.value)
: const SizedBox.shrink(),
),
),
@@ -207,10 +198,10 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
);
},
onProgressChanged: (controller, progress) {
_progressStream.add(progress / 100);
this.progress.value = progress / 100;
},
onTitleChanged: (controller, title) {
_titleStream.add(title);
this.title.value = title ?? '';
},
onCloseWindow: (controller) => Get.back(),
onLoadStop: (controller, url) {
@@ -277,7 +268,7 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
],
);
});
_progressStream.add(1);
progress.value = 1;
}
: null,
shouldInterceptAjaxRequest: (controller, ajaxRequest) async {
@@ -311,7 +302,7 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
);
// debugPrint('webview: [$url], [$hasMatch]');
if (hasMatch) {
_progressStream.add(1.0);
progress.value = 1;
return NavigationActionPolicy.CANCEL;
} else if (RegExp(r'^(?!(https?://))\S+://', caseSensitive: false)
.hasMatch(url)) {
@@ -326,7 +317,7 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
_progressStream.add(1.0);
progress.value = 1;
return NavigationActionPolicy.CANCEL;
}