From cccb3d5c1606850c7554388cac0be60560726548 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 2 Aug 2024 01:00:31 +0800 Subject: [PATCH] :bug: Fix post won't refresh after post --- lib/router.dart | 1 + lib/screens/home.dart | 33 ++++++++++++++++++------------ lib/screens/posts/post_editor.dart | 15 +++++++++++++- lib/screens/realms.dart | 14 ++++++------- lib/screens/realms/realm_view.dart | 25 +--------------------- 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/lib/router.dart b/lib/router.dart index 8cc259e..fe08bb5 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -100,6 +100,7 @@ abstract class AppRouter { reply: arguments?.reply, repost: arguments?.repost, realm: arguments?.realm, + postListController: arguments?.postListController, mode: int.tryParse(state.uri.queryParameters['mode'] ?? '0') ?? 0, ); }, diff --git a/lib/screens/home.dart b/lib/screens/home.dart index e07445b..046b71a 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -4,6 +4,7 @@ import 'package:solian/controllers/post_list_controller.dart'; import 'package:solian/providers/auth.dart'; import 'package:solian/router.dart'; import 'package:solian/screens/account/notification.dart'; +import 'package:solian/screens/posts/post_editor.dart'; import 'package:solian/theme.dart'; import 'package:solian/widgets/app_bar_title.dart'; import 'package:solian/widgets/current_state_action.dart'; @@ -51,7 +52,9 @@ class _HomeScreenState extends State useRootNavigator: true, isScrollControlled: true, context: context, - builder: (context) => const PostCreatePopup(), + builder: (context) => PostCreatePopup( + controller: _postController, + ), ); }, ), @@ -118,12 +121,12 @@ class _HomeScreenState extends State class PostCreatePopup extends StatelessWidget { final bool hideDraftBox; - final Function? onCreated; + final PostListController controller; const PostCreatePopup({ super.key, this.hideDraftBox = false, - this.onCreated, + required this.controller, }); @override @@ -140,11 +143,13 @@ class PostCreatePopup extends StatelessWidget { label: 'postEditorModeStory'.tr, onTap: () { Navigator.pop(context); - AppRouter.instance.pushNamed('postEditor', queryParameters: { - 'mode': 0.toString(), - }).then((val) { - if (val != null && onCreated != null) onCreated!(); - }); + AppRouter.instance.pushNamed( + 'postEditor', + extra: PostPublishArguments(postListController: controller), + queryParameters: { + 'mode': 0.toString(), + }, + ); }, ), ( @@ -152,11 +157,13 @@ class PostCreatePopup extends StatelessWidget { label: 'postEditorModeArticle'.tr, onTap: () { Navigator.pop(context); - AppRouter.instance.pushNamed('postEditor', queryParameters: { - 'mode': 1.toString(), - }).then((val) { - if (val != null && onCreated != null) onCreated!(); - }); + AppRouter.instance.pushNamed( + 'postEditor', + extra: PostPublishArguments(postListController: controller), + queryParameters: { + 'mode': 1.toString(), + }, + ); }, ), ( diff --git a/lib/screens/posts/post_editor.dart b/lib/screens/posts/post_editor.dart index d281783..1d44818 100644 --- a/lib/screens/posts/post_editor.dart +++ b/lib/screens/posts/post_editor.dart @@ -5,6 +5,7 @@ import 'package:get/get.dart'; import 'package:intl/intl.dart'; import 'package:markdown_toolbar/markdown_toolbar.dart'; import 'package:solian/controllers/post_editor_controller.dart'; +import 'package:solian/controllers/post_list_controller.dart'; import 'package:solian/exts.dart'; import 'package:solian/models/post.dart'; import 'package:solian/models/realm.dart'; @@ -22,8 +23,15 @@ class PostPublishArguments { final Post? reply; final Post? repost; final Realm? realm; + final PostListController? postListController; - PostPublishArguments({this.edit, this.reply, this.repost, this.realm}); + PostPublishArguments({ + this.edit, + this.reply, + this.repost, + this.realm, + this.postListController, + }); } class PostPublishScreen extends StatefulWidget { @@ -31,6 +39,7 @@ class PostPublishScreen extends StatefulWidget { final Post? reply; final Post? repost; final Realm? realm; + final PostListController? postListController; final int mode; const PostPublishScreen({ @@ -39,6 +48,7 @@ class PostPublishScreen extends StatefulWidget { this.reply, this.repost, this.realm, + this.postListController, required this.mode, }); @@ -85,6 +95,9 @@ class _PostPublishScreenState extends State { context.showErrorDialog(resp.bodyString); } else { _editorController.localClear(); + if (widget.postListController != null) { + widget.postListController!.reloadAllOver(); + } AppRouter.instance.pop(resp.body); } diff --git a/lib/screens/realms.dart b/lib/screens/realms.dart index 070e110..68bad72 100644 --- a/lib/screens/realms.dart +++ b/lib/screens/realms.dart @@ -26,7 +26,7 @@ class _RealmListScreenState extends State { final List _realms = List.empty(growable: true); - getRealms() async { + _getRealms() async { final AuthProvider auth = Get.find(); if (auth.isAuthorized.isFalse) return; @@ -48,7 +48,7 @@ class _RealmListScreenState extends State { @override void initState() { super.initState(); - getRealms(); + _getRealms(); } @override @@ -71,7 +71,7 @@ class _RealmListScreenState extends State { onPressed: () { AppRouter.instance.pushNamed('realmOrganizing').then( (value) { - if (value != null) getRealms(); + if (value != null) _getRealms(); }, ); }, @@ -84,7 +84,7 @@ class _RealmListScreenState extends State { body: Obx(() { if (auth.isAuthorized.isFalse) { return SigninRequiredOverlay( - onSignedIn: () => getRealms(), + onSignedIn: () => _getRealms(), ); } @@ -94,12 +94,12 @@ class _RealmListScreenState extends State { Expanded( child: CenteredContainer( child: RefreshIndicator( - onRefresh: () => getRealms(), + onRefresh: () => _getRealms(), child: ListView.builder( itemCount: _realms.length, itemBuilder: (context, index) { final element = _realms[index]; - return buildRealm(element); + return _buildEntry(element); }, ), ), @@ -112,7 +112,7 @@ class _RealmListScreenState extends State { ); } - Widget buildRealm(Realm element) { + Widget _buildEntry(Realm element) { return Card( child: ClipRRect( borderRadius: const BorderRadius.all(Radius.circular(8)), diff --git a/lib/screens/realms/realm_view.dart b/lib/screens/realms/realm_view.dart index 2690c4f..141bef0 100644 --- a/lib/screens/realms/realm_view.dart +++ b/lib/screens/realms/realm_view.dart @@ -12,7 +12,6 @@ import 'package:solian/providers/content/posts.dart'; import 'package:solian/providers/content/realm.dart'; import 'package:solian/router.dart'; import 'package:solian/screens/channel/channel_organize.dart'; -import 'package:solian/screens/posts/post_editor.dart'; import 'package:solian/theme.dart'; import 'package:solian/widgets/app_bar_leading.dart'; import 'package:solian/widgets/channel/channel_list.dart'; @@ -136,6 +135,7 @@ class _RealmViewScreenState extends State { } return TabBarView( + physics: const NeverScrollableScrollPhysics(), children: [ RealmPostListWidget(realm: _realm!), RealmChannelListWidget( @@ -189,7 +189,6 @@ class _RealmPostListWidgetState extends State { @override void initState() { super.initState(); - _pagingController.addPageRequestListener(getPosts); } @@ -199,28 +198,6 @@ class _RealmPostListWidgetState extends State { onRefresh: () => Future.sync(() => _pagingController.refresh()), child: CustomScrollView( slivers: [ - SliverToBoxAdapter( - child: ListTile( - leading: const Icon(Icons.post_add), - contentPadding: const EdgeInsets.only(left: 24, right: 8), - tileColor: Theme.of(context).colorScheme.surfaceContainer, - title: Text('postNew'.tr), - subtitle: Text( - 'postNewInRealmHint' - .trParams({'realm': '#${widget.realm.alias}'}), - ), - onTap: () { - AppRouter.instance - .pushNamed( - 'postEditor', - extra: PostPublishArguments(realm: widget.realm), - ) - .then((value) { - if (value != null) _pagingController.refresh(); - }); - }, - ), - ), PostListWidget(controller: _pagingController), ], ),