diff --git a/lib/pages/common/publish/common_publish_page.dart b/lib/pages/common/publish/common_publish_page.dart index 5b410b5b3..197b040fc 100644 --- a/lib/pages/common/publish/common_publish_page.dart +++ b/lib/pages/common/publish/common_publish_page.dart @@ -25,7 +25,8 @@ abstract class CommonPublishPage extends StatefulWidget { abstract class CommonPublishPageState extends State with WidgetsBindingObserver { - late final FocusNode focusNode; + late bool _paused = false; + final FocusNode focusNode = FocusNode(); late final controller = ChatBottomPanelContainerController(); TextEditingController get editController; @@ -38,23 +39,19 @@ abstract class CommonPublishPageState bool hasPub = false; void initPubState(); + bool get handleKeyboard => Platform.isAndroid && widget.autofocus; + @override void initState() { super.initState(); - if (Platform.isAndroid) { + if (handleKeyboard) { WidgetsBinding.instance.addObserver(this); } - focusNode = FocusNode(); - initPubState(); if (widget.autofocus) { - Future.delayed(const Duration(milliseconds: 300), () { - if (mounted) { - focusNode.requestFocus(); - } - }); + _requestFocus(duration: const Duration(milliseconds: 300)); } } @@ -65,34 +62,41 @@ abstract class CommonPublishPageState } focusNode.dispose(); editController.dispose(); - if (Platform.isAndroid) { + if (handleKeyboard) { WidgetsBinding.instance.removeObserver(this); } super.dispose(); } - void _requestFocus() { - Future.delayed(const Duration(microseconds: 200), focusNode.requestFocus); + void _safeRequestFocus() { + if (mounted) { + focusNode.requestFocus(); + } + } + + void _requestFocus({Duration duration = const Duration(microseconds: 200)}) { + Future.delayed(duration, _safeRequestFocus); } @override void didChangeAppLifecycleState(AppLifecycleState state) { - if (state == AppLifecycleState.resumed) { - if (mounted && - widget.autofocus && - (panelType.value == .keyboard || panelType.value == .none)) { - controller.restoreChatPanel(); - WidgetsBinding.instance.addPostFrameCallback((_) { - if (focusNode.hasFocus) { - focusNode.unfocus(); - _requestFocus(); - } else { - _requestFocus(); - } - }); + if (state == .resumed) { + if (_paused) { + _paused = false; + final panelType = this.panelType.value; + if (panelType == .keyboard || panelType == .none) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (focusNode.hasFocus) { + focusNode.unfocus(); + _requestFocus(); + } else { + _requestFocus(); + } + }); + } } - } else if (state == AppLifecycleState.paused) { - controller.keepChatPanel(); + } else if (state == .paused) { + _paused = true; if (focusNode.hasFocus) { focusNode.unfocus(); } diff --git a/lib/pages/common/publish/common_rich_text_pub_page.dart b/lib/pages/common/publish/common_rich_text_pub_page.dart index 92148268e..81a95c24d 100644 --- a/lib/pages/common/publish/common_rich_text_pub_page.dart +++ b/lib/pages/common/publish/common_rich_text_pub_page.dart @@ -108,28 +108,24 @@ abstract class CommonRichTextPubPageState clipBehavior: Clip.none, children: [ GestureDetector( - onTap: () async { - controller.keepChatPanel(); - await PageUtils.imageView( - imgList: imageList - .map( - (img) => switch (img) { - FilePicModel e => SourceModel( - url: e.path, - sourceType: .fileImage, - ), - OpusPicModel e => SourceModel( - url: e.url!, - sourceType: .networkImage, - size: e.size, - ), - }, - ) - .toList(), - initialPage: index, - ); - controller.restoreChatPanel(); - }, + onTap: () => PageUtils.imageView( + imgList: imageList + .map( + (img) => switch (img) { + FilePicModel e => SourceModel( + url: e.path, + sourceType: .fileImage, + ), + OpusPicModel e => SourceModel( + url: e.url!, + sourceType: .networkImage, + size: e.size, + ), + }, + ) + .toList(), + initialPage: index, + ), onLongPress: () { Feedback.forLongPress(context); onClear(); @@ -326,7 +322,6 @@ abstract class CommonRichTextPubPageState late double _mentionOffset = 0; Future? onMention([bool fromClick = false]) async { - controller.keepChatPanel(); final res = await DynMentionPanel.onDynMention( context, offset: _mentionOffset, @@ -343,7 +338,6 @@ abstract class CommonRichTextPubPageState res.clear(); } } - controller.restoreChatPanel(); } void _onInsertUser(MentionItem e, bool fromClick) { diff --git a/lib/pages/dynamics_create/view.dart b/lib/pages/dynamics_create/view.dart index a14b06bc6..1b6dda72d 100644 --- a/lib/pages/dynamics_create/view.dart +++ b/lib/pages/dynamics_create/view.dart @@ -498,7 +498,6 @@ class _CreateDynPanelState extends CommonRichTextPubPageState { onPressed: _isEdit || _isPrivate.value ? null : () async { - controller.keepChatPanel(); DateTime nowDate = DateTime.now(); final selectedDate = await showDatePicker( context: context, @@ -544,7 +543,6 @@ class _CreateDynPanelState extends CommonRichTextPubPageState { ); } } - controller.restoreChatPanel(); }, child: const Text('定时发布'), ) @@ -649,7 +647,6 @@ class _CreateDynPanelState extends CommonRichTextPubPageState { Widget get voteBtn => ToolbarIconButton( onPressed: () async { - controller.keepChatPanel(); final voteItem = editController.items.firstWhereOrNull( (e) => e.type == .vote, ); @@ -694,7 +691,6 @@ class _CreateDynPanelState extends CommonRichTextPubPageState { ); } } - controller.restoreChatPanel(); }, icon: const Icon(Icons.bar_chart_rounded, size: 24), tooltip: '投票', @@ -812,7 +808,6 @@ class _CreateDynPanelState extends CommonRichTextPubPageState { double _topicOffset = 0; Future _onSelectTopic() async { - controller.keepChatPanel(); TopicItem? res = await SelectTopicPanel.onSelectTopic( context, offset: _topicOffset, @@ -821,7 +816,6 @@ class _CreateDynPanelState extends CommonRichTextPubPageState { if (res != null) { _topic.value = Pair(first: res.id, second: res.name); } - controller.restoreChatPanel(); } @override @@ -879,7 +873,6 @@ class _CreateDynPanelState extends CommonRichTextPubPageState { } Future _onReserve() async { - controller.keepChatPanel(); final ReserveInfoData? reserveInfo = await Navigator.of(context).push( GetPageRoute( page: () => CreateReservePage(sid: _reserveCard.value?.id), @@ -888,6 +881,5 @@ class _CreateDynPanelState extends CommonRichTextPubPageState { if (reserveInfo != null) { _reserveCard.value = reserveInfo; } - controller.restoreChatPanel(); } } diff --git a/lib/pages/video/reply_new/view.dart b/lib/pages/video/reply_new/view.dart index 2edf14978..9c1b2fb97 100644 --- a/lib/pages/video/reply_new/view.dart +++ b/lib/pages/video/reply_new/view.dart @@ -281,7 +281,6 @@ class _ReplyPageState extends CommonRichTextPubPageState { children: [ item( onTap: () async { - controller.keepChatPanel(); final ({String title, String url})? res = await Get.to( ReplySearchPage(type: widget.replyType, oid: widget.oid), ); @@ -292,7 +291,6 @@ class _ReplyPageState extends CommonRichTextPubPageState { rawText: '${res.url} ', ); } - controller.restoreChatPanel(); }, icon: Icon(Icons.post_add, size: 28, color: color), title: '插入内容', diff --git a/lib/pages/video/send_danmaku/view.dart b/lib/pages/video/send_danmaku/view.dart index 4e4fbd241..5daa9cb7a 100644 --- a/lib/pages/video/send_danmaku/view.dart +++ b/lib/pages/video/send_danmaku/view.dart @@ -419,9 +419,8 @@ class _SendDanmakuPanelState extends CommonTextPubPageState { ); } - Future _showColorPicker() async { - controller.keepChatPanel(); - await showDialog( + void _showColorPicker() { + showDialog( context: context, builder: (context) => AlertDialog( clipBehavior: Clip.hardEdge, @@ -437,7 +436,6 @@ class _SendDanmakuPanelState extends CommonTextPubPageState { ), ), ); - controller.restoreChatPanel(); } @override diff --git a/pubspec.lock b/pubspec.lock index 9cb688679..ef4ff9f94 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -255,12 +255,11 @@ packages: chat_bottom_container: dependency: "direct main" description: - path: "packages/chat_bottom_container" - ref: main - resolved-ref: dba2bf10db4a6f89564d9be63ce17b18f0f7e3e5 - url: "https://github.com/bggRGjQaUbCoE/flutter_chat_packages.git" - source: git - version: "0.3.2" + name: chat_bottom_container + sha256: e9c3b9879251658e403d2a93057c5db469c99237fef62ca562ec6d223970ef41 + url: "https://pub.dev" + source: hosted + version: "0.5.0" checked_yaml: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 97fdc822f..afad0b88c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -55,11 +55,7 @@ dependencies: url: https://github.com/My-Responsitories/catcher_2.git ref: dev characters: ^1.4.1 - chat_bottom_container: - git: - url: https://github.com/bggRGjQaUbCoE/flutter_chat_packages.git - ref: main - path: packages/chat_bottom_container + chat_bottom_container: ^0.5.0 collection: ^1.19.1 connectivity_plus: ^7.1.1 cookie_jar: ^4.0.8