From f1e5e3ec716b229bc324955e2d7717732b044355 Mon Sep 17 00:00:00 2001 From: Juan Gilsanz Polo Date: Fri, 23 Feb 2024 13:06:53 +0100 Subject: [PATCH] Router changes --- lib/config/app_screens.dart | 23 ++++++++++++ lib/models/app_route.dart | 13 +++++++ lib/router/routes.dart | 61 ++++++++++++-------------------- lib/screens/links/ui/links.dart | 18 ++++++---- lib/utils/color_translation.dart | 2 -- lib/widgets/layout.dart | 50 ++++++++++++-------------- 6 files changed, 91 insertions(+), 76 deletions(-) create mode 100644 lib/config/app_screens.dart create mode 100644 lib/models/app_route.dart diff --git a/lib/config/app_screens.dart b/lib/config/app_screens.dart new file mode 100644 index 0000000..9391a5a --- /dev/null +++ b/lib/config/app_screens.dart @@ -0,0 +1,23 @@ +import 'package:flutter/material.dart'; + +import 'package:linkdy/i18n/strings.g.dart'; +import 'package:linkdy/models/app_route.dart'; +import 'package:linkdy/router/paths.dart'; + +final appScreens = [ + AppRoute( + icon: Icons.link_rounded, + route: RoutesPaths.links, + name: t.links.links, + ), + AppRoute( + icon: Icons.search_rounded, + route: RoutesPaths.search, + name: t.search.search, + ), + AppRoute( + icon: Icons.settings_rounded, + route: RoutesPaths.settings, + name: t.settings.settings, + ), +]; diff --git a/lib/models/app_route.dart b/lib/models/app_route.dart new file mode 100644 index 0000000..2283b87 --- /dev/null +++ b/lib/models/app_route.dart @@ -0,0 +1,13 @@ +import 'package:flutter/material.dart'; + +class AppRoute { + final IconData icon; + final String route; + final String name; + + const AppRoute({ + required this.icon, + required this.route, + required this.name, + }); +} diff --git a/lib/router/routes.dart b/lib/router/routes.dart index 6b0e553..3847324 100644 --- a/lib/router/routes.dart +++ b/lib/router/routes.dart @@ -27,49 +27,32 @@ final List appRoutes = [ path: RoutesPaths.webview, builder: (context, state) => WebView(bookmark: state.extra as Bookmark), ), - StatefulShellRoute.indexedStack( - builder: (context, state, navigationShell) => Layout( - navigationShell: navigationShell, + ShellRoute( + builder: (context, state, child) => Layout( + state: state, + child: child, ), - branches: [ - StatefulShellBranch( - navigatorKey: linksNavigatorKey, - initialLocation: RoutesPaths.links, - routes: [ - GoRoute( - path: RoutesPaths.links, - builder: (context, state) => const Links(), - ), - ], + routes: [ + GoRoute( + path: RoutesPaths.links, + builder: (context, state) => const Links(), ), - StatefulShellBranch( - navigatorKey: searchNavigatorKey, - initialLocation: RoutesPaths.search, - routes: [ - GoRoute( - path: RoutesPaths.search, - builder: (context, state) => const Search(), - ), - ], + GoRoute( + path: RoutesPaths.search, + builder: (context, state) => const Search(), ), - StatefulShellBranch( - navigatorKey: settingsNavigatorKey, - initialLocation: RoutesPaths.settings, - routes: [ - GoRoute( - path: RoutesPaths.settings, - builder: (context, state) => const Settings(), - ), - GoRoute( - path: RoutesPaths.customization, - builder: (context, state) => const Customization(), - ), - GoRoute( - path: RoutesPaths.generalSettings, - builder: (context, state) => const GeneralSettings(), - ), - ], + GoRoute( + path: RoutesPaths.settings, + builder: (context, state) => const Settings(), ), ], ), + GoRoute( + path: RoutesPaths.customization, + builder: (context, state) => const Customization(), + ), + GoRoute( + path: RoutesPaths.generalSettings, + builder: (context, state) => const GeneralSettings(), + ), ]; diff --git a/lib/screens/links/ui/links.dart b/lib/screens/links/ui/links.dart index b8ff8a8..2474a83 100644 --- a/lib/screens/links/ui/links.dart +++ b/lib/screens/links/ui/links.dart @@ -94,13 +94,17 @@ class Links extends ConsumerWidget { ListTile( onTap: () => ref.watch(routerProvider).push(RoutesPaths.webview, extra: link), isThreeLine: true, - title: Text( - validateStrings(link?.title, link?.websiteTitle), - maxLines: 2, - overflow: TextOverflow.ellipsis, - style: TextStyle( - fontSize: 18, - color: Theme.of(context).colorScheme.onSurface, + title: Padding( + padding: const EdgeInsets.only(bottom: 4), + child: Text( + validateStrings(link?.title, link?.websiteTitle), + maxLines: 2, + overflow: TextOverflow.ellipsis, + style: TextStyle( + fontSize: 16, + fontWeight: FontWeight.w500, + color: Theme.of(context).colorScheme.onSurface, + ), ), ), subtitle: Column( diff --git a/lib/utils/color_translation.dart b/lib/utils/color_translation.dart index fb17e29..0b59177 100644 --- a/lib/utils/color_translation.dart +++ b/lib/utils/color_translation.dart @@ -1,5 +1,3 @@ -import 'package:flutter/material.dart'; - import 'package:linkdy/i18n/strings.g.dart'; String colorTranslation(int index) { diff --git a/lib/widgets/layout.dart b/lib/widgets/layout.dart index 2facd44..2194a4f 100644 --- a/lib/widgets/layout.dart +++ b/lib/widgets/layout.dart @@ -1,26 +1,26 @@ import 'package:animations/animations.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; -import 'package:linkdy/i18n/strings.g.dart'; import 'package:linkdy/widgets/system_overlay_style.dart'; -class Layout extends StatelessWidget { - final StatefulNavigationShell navigationShell; +import 'package:linkdy/config/app_screens.dart'; +import 'package:linkdy/providers/router_provider.dart'; + +class Layout extends ConsumerWidget { + final GoRouterState state; + final Widget child; const Layout({ Key? key, - required this.navigationShell, + required this.state, + required this.child, }) : super(key: key); @override - Widget build(BuildContext context) { - void goBranch(int index) { - navigationShell.goBranch( - index, - initialLocation: index == navigationShell.currentIndex, - ); - } + Widget build(BuildContext context, WidgetRef ref) { + final screenIndex = appScreens.map((s) => s.route).toList().indexOf("/${state.matchedLocation.split("/")[1]}"); return OverlayStyle( child: Scaffold( @@ -31,25 +31,19 @@ class Layout extends StatelessWidget { secondaryAnimation: secondaryAnimation, child: child, ), - child: navigationShell, + child: child, ), bottomNavigationBar: NavigationBar( - onDestinationSelected: (s) => goBranch(s), - selectedIndex: navigationShell.currentIndex, - destinations: [ - NavigationDestination( - icon: const Icon(Icons.link_rounded), - label: t.links.links, - ), - NavigationDestination( - icon: const Icon(Icons.search_rounded), - label: t.search.search, - ), - NavigationDestination( - icon: const Icon(Icons.settings_rounded), - label: t.settings.settings, - ), - ], + onDestinationSelected: (s) => ref.watch(routerProvider).replace(appScreens[s].route), + selectedIndex: screenIndex, + destinations: appScreens + .map( + (screen) => NavigationDestination( + icon: Icon(screen.icon), + label: screen.name, + ), + ) + .toList(), ), ), );