From c7f037de0ff1d9089996fcb57b3bd9d50d3f05f3 Mon Sep 17 00:00:00 2001 From: Marcin Wojnarowski Date: Thu, 30 Sep 2021 12:55:23 +0200 Subject: [PATCH] Feature/routing refactor (#258) * Use MaterialPageRoute * Use T.route --- analysis_options.yaml | 8 ++---- lib/pages/add_account.dart | 12 ++++---- lib/pages/add_instance.dart | 5 ++++ lib/pages/create_post.dart | 28 ++++++++++++++----- lib/pages/full_post.dart | 6 ++-- lib/pages/home_tab.dart | 7 ++--- lib/pages/inbox.dart | 18 ++++++------ lib/pages/instance.dart | 1 - lib/pages/profile_tab.dart | 1 - lib/pages/search_tab.dart | 1 - lib/pages/settings.dart | 20 ++++--------- lib/pages/user.dart | 15 +++++----- lib/pages/write_message.dart | 18 ++++++++++++ lib/theme.dart | 7 +++++ lib/url_launcher.dart | 1 - lib/util/extensions/spaced.dart | 2 +- lib/util/goto.dart | 5 ++-- lib/widgets/about_tile.dart | 1 - lib/widgets/comment/comment.dart | 1 - lib/widgets/comment/comment_actions.dart | 6 ++-- .../comment/comment_more_menu_button.dart | 6 ++-- lib/widgets/fullscreenable_image.dart | 1 - lib/widgets/post.dart | 6 ++-- lib/widgets/write_comment.dart | 24 ++++++++++++++++ 24 files changed, 120 insertions(+), 80 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 3bb119f..2734950 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -117,8 +117,6 @@ analyzer: - "**/*.g.dart" - "**/*.freezed.dart" - "lib/gen/assets.gen.dart" - strong-mode: - # TODO: remove after migrating to null safety, this is already turned off by default - implicit-casts: false - # TODO: consider disabling - # implicit-dynamic: false + # strong-mode: + # TODO: consider disabling + # implicit-dynamic: false diff --git a/lib/pages/add_account.dart b/lib/pages/add_account.dart index 1a537e6..25822d3 100644 --- a/lib/pages/add_account.dart +++ b/lib/pages/add_account.dart @@ -1,5 +1,4 @@ import 'package:cached_network_image/cached_network_image.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:lemmy_api_client/v3.dart'; @@ -123,10 +122,8 @@ class AddAccountPage extends HookWidget { ), title: const Text('Add instance'), onTap: () async { - final value = await showCupertinoModalPopup( - context: context, - builder: (context) => const AddInstancePage(), - ); + final value = + await Navigator.of(context).push(AddInstancePage.route()); Navigator.of(context).pop(value); }, ), @@ -178,4 +175,9 @@ class AddAccountPage extends HookWidget { ), ); } + + static Route route(String instanceHost) => MaterialPageRoute( + builder: (context) => AddAccountPage(instanceHost: instanceHost), + fullscreenDialog: true, + ); } diff --git a/lib/pages/add_instance.dart b/lib/pages/add_instance.dart index edd6c83..12a4637 100644 --- a/lib/pages/add_instance.dart +++ b/lib/pages/add_instance.dart @@ -132,4 +132,9 @@ class AddInstancePage extends HookWidget { ), ); } + + static Route route() => MaterialPageRoute( + builder: (context) => const AddInstancePage(), + fullscreenDialog: true, + ); } diff --git a/lib/pages/create_post.dart b/lib/pages/create_post.dart index 1913a4e..c6a0e01 100644 --- a/lib/pages/create_post.dart +++ b/lib/pages/create_post.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:image_picker/image_picker.dart'; @@ -33,11 +32,10 @@ class CreatePostFab extends HookWidget { return FloatingActionButton( onPressed: loggedInAction((_) async { - final postView = await showCupertinoModalPopup( - context: context, - builder: (_) => community == null - ? const CreatePostPage() - : CreatePostPage.toCommunity(community!), + final postView = await Navigator.of(context).push( + community == null + ? CreatePostPage.route() + : CreatePostPage.toCommunityRoute(community!), ); if (postView != null) { @@ -67,7 +65,7 @@ class CreatePostPage extends HookWidget { const CreatePostPage.toCommunity(CommunityView this.community) : _isEdit = false, post = null; - const CreatePostPage.edit(this.post) + const CreatePostPage.edit(Post this.post) : _isEdit = true, community = null; @@ -352,4 +350,20 @@ class CreatePostPage extends HookWidget { ), ); } + + static Route route() => MaterialPageRoute( + builder: (context) => const CreatePostPage(), + fullscreenDialog: true, + ); + + static Route toCommunityRoute(CommunityView community) => + MaterialPageRoute( + builder: (context) => CreatePostPage.toCommunity(community), + fullscreenDialog: true, + ); + + static Route editRoute(Post post) => MaterialPageRoute( + builder: (context) => CreatePostPage.edit(post), + fullscreenDialog: true, + ); } diff --git a/lib/pages/full_post.dart b/lib/pages/full_post.dart index 3019c49..30e9bf3 100644 --- a/lib/pages/full_post.dart +++ b/lib/pages/full_post.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; @@ -84,9 +83,8 @@ class FullPostPage extends HookWidget { sharePost() => share(post.post.apId, context: context); comment() async { - final newComment = await showCupertinoModalPopup( - context: context, - builder: (_) => WriteComment.toPost(post.post), + final newComment = await Navigator.of(context).push( + WriteComment.toPostRoute(post.post), ); if (newComment != null) { newComments.value = [...newComments.value, newComment]; diff --git a/lib/pages/home_tab.dart b/lib/pages/home_tab.dart index 38dc2b2..2334d38 100644 --- a/lib/pages/home_tab.dart +++ b/lib/pages/home_tab.dart @@ -1,7 +1,6 @@ import 'dart:math' show max; import 'package:cached_network_image/cached_network_image.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:lemmy_api_client/v3.dart'; @@ -158,10 +157,8 @@ class HomeTab extends HookWidget { : null), ), onTap: accStore.isAnonymousFor(instance) - ? () => showCupertinoModalPopup( - context: context, - builder: (_) => - AddAccountPage(instanceHost: instance)) + ? () => Navigator.of(context) + .push(AddAccountPage.route(instance)) : () => pop(_SelectedList( listingType: PostListingType.subscribed, instanceHost: instance, diff --git a/lib/pages/inbox.dart b/lib/pages/inbox.dart index 464055b..b7fbdc7 100644 --- a/lib/pages/inbox.dart +++ b/lib/pages/inbox.dart @@ -1,7 +1,6 @@ import 'dart:math' show pi; import 'package:cached_network_image/cached_network_image.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:lemmy_api_client/v3.dart'; @@ -364,12 +363,12 @@ class PrivateMessageTile extends HookWidget { icon: Icons.reply, tooltip: L10n.of(context)!.reply, onPressed: () { - showCupertinoModalPopup( - context: context, - builder: (_) => WriteMessagePage.send( - instanceHost: pmv.value.instanceHost, - recipient: otherSide, - )); + Navigator.of(context).push( + WriteMessagePage.sendRoute( + instanceHost: pmv.value.instanceHost, + recipient: otherSide, + ), + ); }, ) ] else ...[ @@ -377,9 +376,8 @@ class PrivateMessageTile extends HookWidget { icon: Icons.edit, tooltip: L10n.of(context)!.edit, onPressed: () async { - final val = await showCupertinoModalPopup( - context: context, - builder: (_) => WriteMessagePage.edit(pmv.value)); + final val = await Navigator.of(context) + .push(WriteMessagePage.editRoute(pmv.value)); if (val != null) pmv.value = val; }, ), diff --git a/lib/pages/instance.dart b/lib/pages/instance.dart index 7b9bc8c..1c86694 100644 --- a/lib/pages/instance.dart +++ b/lib/pages/instance.dart @@ -1,5 +1,4 @@ import 'package:cached_network_image/cached_network_image.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:lemmy_api_client/v3.dart'; diff --git a/lib/pages/profile_tab.dart b/lib/pages/profile_tab.dart index 2e47a36..25635a8 100644 --- a/lib/pages/profile_tab.dart +++ b/lib/pages/profile_tab.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; diff --git a/lib/pages/search_tab.dart b/lib/pages/search_tab.dart index a0b02be..7336bc3 100644 --- a/lib/pages/search_tab.dart +++ b/lib/pages/search_tab.dart @@ -1,5 +1,4 @@ import 'package:collection/collection.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; diff --git a/lib/pages/settings.dart b/lib/pages/settings.dart index 54731ca..437876c 100644 --- a/lib/pages/settings.dart +++ b/lib/pages/settings.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; @@ -327,16 +326,13 @@ class AccountsConfigPage extends HookWidget { SpeedDialChild( child: const Icon(Icons.person_add), label: 'Add account', - onTap: () => showCupertinoModalPopup( - context: context, - builder: (_) => - AddAccountPage(instanceHost: accountsStore.instances.last)), + onTap: () => Navigator.of(context) + .push(AddAccountPage.route(accountsStore.instances.last)), ), SpeedDialChild( child: const Icon(Icons.dns), label: 'Add instance', - onTap: () => showCupertinoModalPopup( - context: context, builder: (_) => const AddInstancePage()), + onTap: () => Navigator.of(context).push(AddInstancePage.route()), ), ], child: const Icon(Icons.add), @@ -350,10 +346,8 @@ class AccountsConfigPage extends HookWidget { Padding( padding: const EdgeInsets.only(top: 100), child: TextButton.icon( - onPressed: () => showCupertinoModalPopup( - context: context, - builder: (_) => const AddInstancePage(), - ), + onPressed: () => + Navigator.of(context).push(AddInstancePage.route()), icon: const Icon(Icons.add), label: const Text('Add instance'), ), @@ -393,9 +387,7 @@ class AccountsConfigPage extends HookWidget { leading: const Icon(Icons.add), title: const Text('Add account'), onTap: () { - showCupertinoModalPopup( - context: context, - builder: (_) => AddAccountPage(instanceHost: instance)); + Navigator.of(context).push(AddAccountPage.route(instance)); }, ), ] diff --git a/lib/pages/user.dart b/lib/pages/user.dart index 9c55dcd..f2c03eb 100644 --- a/lib/pages/user.dart +++ b/lib/pages/user.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:lemmy_api_client/v3.dart'; @@ -69,12 +68,14 @@ class SendMessageButton extends HookWidget { return IconButton( icon: const Icon(Icons.email), - onPressed: loggedInAction((token) => showCupertinoModalPopup( - context: context, - builder: (_) => WriteMessagePage.send( - instanceHost: user.instanceHost, - recipient: user, - ))), + onPressed: loggedInAction( + (token) => Navigator.of(context).push( + WriteMessagePage.sendRoute( + instanceHost: user.instanceHost, + recipient: user, + ), + ), + ), ); } } diff --git a/lib/pages/write_message.dart b/lib/pages/write_message.dart index 37454dc..d6a8bd6 100644 --- a/lib/pages/write_message.dart +++ b/lib/pages/write_message.dart @@ -133,4 +133,22 @@ class WriteMessagePage extends HookWidget { ), ); } + + static Route sendRoute({ + required PersonSafe recipient, + required String instanceHost, + }) => + MaterialPageRoute( + builder: (context) => WriteMessagePage.send( + recipient: recipient, + instanceHost: instanceHost, + ), + fullscreenDialog: true, + ); + + static Route editRoute(PrivateMessageView pmv) => + MaterialPageRoute( + builder: (context) => WriteMessagePage.edit(pmv), + fullscreenDialog: true, + ); } diff --git a/lib/theme.dart b/lib/theme.dart index 0056bb9..07e3b3b 100644 --- a/lib/theme.dart +++ b/lib/theme.dart @@ -10,6 +10,13 @@ ThemeData _themeFactory({bool dark = false, bool amoled = false}) { final maybeAmoledColor = amoled ? Colors.black : null; return theme.copyWith( + pageTransitionsTheme: const PageTransitionsTheme( + builders: { + TargetPlatform.android: CupertinoPageTransitionsBuilder(), + TargetPlatform.iOS: CupertinoPageTransitionsBuilder(), + TargetPlatform.fuchsia: CupertinoPageTransitionsBuilder(), + }, + ), scaffoldBackgroundColor: maybeAmoledColor, backgroundColor: maybeAmoledColor, canvasColor: maybeAmoledColor, diff --git a/lib/url_launcher.dart b/lib/url_launcher.dart index dc14fe9..5069af9 100644 --- a/lib/url_launcher.dart +++ b/lib/url_launcher.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart' as ul; diff --git a/lib/util/extensions/spaced.dart b/lib/util/extensions/spaced.dart index 1e2fa9c..2660e82 100644 --- a/lib/util/extensions/spaced.dart +++ b/lib/util/extensions/spaced.dart @@ -1,4 +1,4 @@ -import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; /// Creates gaps between given widgets extension SpaceWidgets on List { diff --git a/lib/util/goto.dart b/lib/util/goto.dart index ca69c20..0a43b4a 100644 --- a/lib/util/goto.dart +++ b/lib/util/goto.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:lemmy_api_client/v3.dart'; @@ -13,7 +12,7 @@ Future goTo( BuildContext context, Widget Function(BuildContext context) builder, ) => - Navigator.of(context).push(CupertinoPageRoute( + Navigator.of(context).push(MaterialPageRoute( builder: builder, )); @@ -22,7 +21,7 @@ Future goToReplace( BuildContext context, Widget Function(BuildContext context) builder, ) => - Navigator.of(context).pushReplacement(CupertinoPageRoute( + Navigator.of(context).pushReplacement(MaterialPageRoute( builder: builder, )); diff --git a/lib/widgets/about_tile.dart b/lib/widgets/about_tile.dart index bfa0964..5d2161a 100644 --- a/lib/widgets/about_tile.dart +++ b/lib/widgets/about_tile.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:flutter_markdown/flutter_markdown.dart'; diff --git a/lib/widgets/comment/comment.dart b/lib/widgets/comment/comment.dart index 6130d6c..a76665d 100644 --- a/lib/widgets/comment/comment.dart +++ b/lib/widgets/comment/comment.dart @@ -1,6 +1,5 @@ import 'dart:math'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:lemmy_api_client/v3.dart'; import 'package:provider/provider.dart'; diff --git a/lib/widgets/comment/comment_actions.dart b/lib/widgets/comment/comment_actions.dart index a4d7f81..5a2e8d1 100644 --- a/lib/widgets/comment/comment_actions.dart +++ b/lib/widgets/comment/comment_actions.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; @@ -33,9 +32,8 @@ class CommentActions extends HookWidget { final post = store.comment.post; reply() async { - final newComment = await showCupertinoModalPopup( - context: context, - builder: (context) => WriteComment.toComment( + final newComment = await Navigator.of(context).push( + WriteComment.toCommentRoute( comment: comment, post: post, ), diff --git a/lib/widgets/comment/comment_more_menu_button.dart b/lib/widgets/comment/comment_more_menu_button.dart index 0e533c3..89158bc 100644 --- a/lib/widgets/comment/comment_more_menu_button.dart +++ b/lib/widgets/comment/comment_more_menu_button.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:lemmy_api_client/v3.dart'; @@ -63,9 +62,8 @@ class _CommentMoreMenuPopup extends HookWidget { } handleEdit() async { - final editedComment = await showCupertinoModalPopup( - context: context, - builder: (_) => WriteComment.edit( + final editedComment = await Navigator.of(context).push( + WriteComment.editRoute( comment: comment, post: post, ), diff --git a/lib/widgets/fullscreenable_image.dart b/lib/widgets/fullscreenable_image.dart index 2093ba0..a6aa4cb 100644 --- a/lib/widgets/fullscreenable_image.dart +++ b/lib/widgets/fullscreenable_image.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../pages/media_view.dart'; diff --git a/lib/widgets/post.dart b/lib/widgets/post.dart index 7561947..d35a97a 100644 --- a/lib/widgets/post.dart +++ b/lib/widgets/post.dart @@ -1,5 +1,4 @@ import 'package:cached_network_image/cached_network_image.dart'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; @@ -103,9 +102,8 @@ class PostWidget extends HookWidget { leading: const Icon(Icons.edit), title: const Text('Edit'), onTap: () async { - final postView = await showCupertinoModalPopup( - context: context, - builder: (_) => CreatePostPage.edit(post.post), + final postView = await Navigator.of(context).push( + CreatePostPage.editRoute(post.post), ); if (postView != null) { diff --git a/lib/widgets/write_comment.dart b/lib/widgets/write_comment.dart index 07e2146..4c1d54f 100644 --- a/lib/widgets/write_comment.dart +++ b/lib/widgets/write_comment.dart @@ -134,4 +134,28 @@ class WriteComment extends HookWidget { ), ); } + + static Route toPostRoute(Post post) => MaterialPageRoute( + builder: (context) => WriteComment.toPost(post), + fullscreenDialog: true, + ); + + static Route toCommentRoute({ + required Comment comment, + required Post post, + }) => + MaterialPageRoute( + builder: (context) => + WriteComment.toComment(comment: comment, post: post), + fullscreenDialog: true, + ); + + static Route editRoute({ + required Comment comment, + required Post post, + }) => + MaterialPageRoute( + builder: (context) => WriteComment.edit(comment: comment, post: post), + fullscreenDialog: true, + ); }