Skip to content

Commit

Permalink
feat: Main layout redirections and styles
Browse files Browse the repository at this point in the history
  • Loading branch information
enrique-lozano committed Feb 15, 2024
1 parent 887a9cb commit bb28b01
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 58 deletions.
23 changes: 8 additions & 15 deletions lib/app/layout/navigation_sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:monekin/core/presentation/responsive/breakpoint_container.dart';
import 'package:monekin/core/presentation/responsive/breakpoints.dart';
import 'package:monekin/core/presentation/widgets/user_avatar.dart';
import 'package:monekin/core/routes/destinations.dart';
import 'package:monekin/core/routes/route_utils.dart';
import 'package:monekin/main.dart';

double getNavigationSidebarWidth(BuildContext context) {
Expand Down Expand Up @@ -52,6 +53,11 @@ class NavigationSidebarState extends State<NavigationSidebar> {
final selectedNavItemIndex = menuItems
.indexWhere((element) => element.id == selectedDestination!.id);

onDestinationSelected(int e) {
RouteUtils.popAllRoutesExceptFirst();
tabsPageKey.currentState?.changePage(menuItems.elementAt(e));
}

return AnimatedContainer(
duration: const Duration(milliseconds: 1500),
curve: Curves.easeInOutCubicEmphasized,
Expand Down Expand Up @@ -84,27 +90,14 @@ class NavigationSidebarState extends State<NavigationSidebar> {
destinations: menuItems
.map((e) => e.toNavigationRailDestinationWidget())
.toList(),
onDestinationSelected: (e) =>
tabsPageKey.currentState?.changePage(menuItems.elementAt(e)),
onDestinationSelected: onDestinationSelected,
selectedIndex:
selectedNavItemIndex < 0 ? null : selectedNavItemIndex),
),
xlChild: SafeArea(
child: HomeDrawer(
drawerActions: menuItems,
onDestinationSelected: (e) {
// Pop all routes without animation:
navigatorKey.currentState!.pushAndRemoveUntil(
PageRouteBuilder(
pageBuilder: (context, animation1, animation2) =>
const SizedBox(),
transitionDuration: const Duration(seconds: 0),
),
(route) => route.isFirst);
navigatorKey.currentState!.pop();

tabsPageKey.currentState?.changePage(menuItems.elementAt(e));
},
onDestinationSelected: onDestinationSelected,
selectedIndex: selectedNavItemIndex,
),
),
Expand Down
39 changes: 3 additions & 36 deletions lib/app/layout/tabs.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:monekin/app/layout/lazy_indexed_stack.dart';
import 'package:monekin/core/presentation/responsive/breakpoint_container.dart';
import 'package:monekin/core/presentation/responsive/breakpoints.dart';
import 'package:monekin/core/routes/destinations.dart';
import 'package:monekin/main.dart';
Expand All @@ -17,6 +16,8 @@ class TabsPageState extends State<TabsPage> {
MainMenuDestination? selectedDestination;

void changePage(MainMenuDestination destination) {
print("HOLA");

navigationSidebarKey.currentState?.setSelectedDestination(destination);

setState(() {
Expand Down Expand Up @@ -57,14 +58,7 @@ class TabsPageState extends State<TabsPage> {
index: allDestinations
.indexWhere((element) => element.id == selectedDestination?.id),
duration: const Duration(milliseconds: 300),
children: allDestinations
.map(
(e) => BreakpointContainer(
mdChild: _RightWidgetContainer(child: e.destination),
child: e.destination,
),
)
.toList(),
children: allDestinations.map((e) => e.destination).toList(),
);
}),
// selectedDestination?.destination ?? const SizedBox.shrink(),
Expand Down Expand Up @@ -135,30 +129,3 @@ class FadeIndexedStackState extends State<FadeIndexedStack>
);
}
}

class _RightWidgetContainer extends StatelessWidget {
const _RightWidgetContainer({
super.key,
required this.child,
});

final Widget child;

@override
Widget build(BuildContext context) {
return Expanded(
child: Container(
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
),
margin: const EdgeInsets.all(16),
child: Card(
elevation: 4,
clipBehavior: Clip.hardEdge,
child: child,
),
),
);
}
}
11 changes: 9 additions & 2 deletions lib/app/settings/backup_settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import 'package:monekin/app/settings/settings.page.dart';
import 'package:monekin/core/database/app_db.dart';
import 'package:monekin/core/database/backup/backup_database_service.dart';
import 'package:monekin/core/presentation/widgets/confirm_dialog.dart';
import 'package:monekin/core/routes/destinations.dart';
import 'package:monekin/core/routes/route_utils.dart';
import 'package:monekin/core/utils/number_utils.dart';
import 'package:monekin/i18n/translations.g.dart';
import 'package:monekin/main.dart';

class BackupSettingsPage extends StatelessWidget {
const BackupSettingsPage({super.key});
Expand Down Expand Up @@ -53,8 +55,13 @@ class BackupSettingsPage extends StatelessWidget {
return;
}

// TODO: REPLACE ALL
// context.router.replaceAll([const MainLayoutRoute()]);
RouteUtils.popAllRoutesExceptFirst();

tabsPageKey.currentState!.changePage(
getAllDestinations(context, shortLabels: false)
.firstWhere((element) =>
element.id == AppMenuDestinationsID.dashboard),
);

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(t.backup.import.success)),
Expand Down
27 changes: 22 additions & 5 deletions lib/core/routes/route_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ abstract class RouteUtils {
}) {
if (navigatorKey.currentState == null) return Future.value(null);

var pageRouteBuilder = PageRouteBuilder<T>(
var pageRouteBuilder = getPageRouteBuilder<T>(page);

if (withReplacement) {
return navigatorKey.currentState!.pushReplacement(pageRouteBuilder);
}

return navigatorKey.currentState!.push(pageRouteBuilder);
}

static PageRouteBuilder<T> getPageRouteBuilder<T>(Widget page) {
return PageRouteBuilder<T>(
opaque: false,
transitionDuration: const Duration(milliseconds: 300),
reverseTransitionDuration: const Duration(milliseconds: 125),
Expand All @@ -30,11 +40,18 @@ abstract class RouteUtils {
return page;
},
);
}

if (withReplacement) {
return navigatorKey.currentState!.pushReplacement(pageRouteBuilder);
}
/// Pop all the routes in the stack except the first one without any animation
static void popAllRoutesExceptFirst() {
// This function can be useful when we want to return to the main layout page
navigatorKey.currentState!.pushAndRemoveUntil(
PageRouteBuilder(
pageBuilder: (context, animation1, animation2) => const SizedBox(),
transitionDuration: const Duration(seconds: 0),
),
(route) => route.isFirst);

return navigatorKey.currentState!.push(pageRouteBuilder);
navigatorKey.currentState!.pop();
}
}
7 changes: 7 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:monekin/app/layout/tabs.dart';
import 'package:monekin/app/onboarding/intro.page.dart';
import 'package:monekin/core/database/services/app-data/app_data_service.dart';
import 'package:monekin/core/database/services/user-setting/user_setting_service.dart';
import 'package:monekin/core/presentation/responsive/breakpoints.dart';
import 'package:monekin/core/presentation/theme.dart';
import 'package:monekin/core/utils/scroll_behavior_override.dart';
import 'package:monekin/i18n/translations.g.dart';
Expand Down Expand Up @@ -164,6 +165,12 @@ class MaterialAppContainer extends StatelessWidget {
introSeen ? getNavigationSidebarWidth(context) : 0,
color: Theme.of(context).canvasColor,
),
if (BreakPoint.of(context).isLargerThan(BreakpointID.sm))
Container(
width: 2,
height: MediaQuery.of(context).size.height,
color: Theme.of(context).dividerColor,
),
Expanded(child: child ?? const SizedBox.shrink()),
],
),
Expand Down

0 comments on commit bb28b01

Please sign in to comment.