3.3 KiB
Modification Design: Login-Based Home Screen
Overview
This document outlines the design for modifying the home screen to display different content based on the user's authentication status. The goal is to show a "logged out" view to unauthenticated users and a "logged in" view to authenticated users, as depicted in the provided design images.
Analysis
The current HomeScreen displays a static UI that does not change based on the authentication state. The AuthState class manages the user's login status, and the HomeScreen needs to react to changes in this state. The provider package is already in use for state management, which makes it the ideal tool for this task.
Alternatives Considered
-
Conditional Logic in
build()Method: The simplest approach is to use a conditional statement (like anifor ternary operator) directly within theHomeScreen'sbuildmethod. This would involve wrapping theScaffold's body with aConsumer<AuthState>widget to listen for changes and rebuild the UI accordingly. This is a straightforward and efficient solution for this use case. -
Separate
StatelessWidgets for Each State: A more modular approach would be to create two separate widgets, for instanceLoggedInHomeandLoggedOutHome. TheHomeScreenwould then act as a container that decides which of these two widgets to display based on the authentication state. This approach is slightly more verbose but can lead to cleaner code if the UI for each state is complex.
For this modification, the first alternative is the most appropriate due to its simplicity and the relatively contained nature of the changes.
Detailed Design
The HomeScreen will be modified to use a Consumer<AuthState> widget from the provider package. This widget will wrap the Scaffold's body. The builder of the Consumer will receive the authState and decide which UI to render.
Mermaid Diagram
graph TD
A[HomeScreen build()] --> B{Consumer<AuthState>};
B --> C{authState.isLoggedIn?};
C -- Yes --> D["LoggedIn UI (home_login.png)"];
C -- No --> E["LoggedOut UI (home_logout.png)"];
Implementation Details
-
lib/home/home_screen.dart:- The
buildmethod will be updated. - The
bodyof theScaffoldwill be aConsumer<AuthState>. - The
builderof theConsumerwill have the following logic:builder: (context, authState, child) { if (authState.isLoggedIn) { // Return the widget for the logged-in state } else { // Return the widget for the logged-out state } }
- The
-
Logged-In UI: This will be a new private widget,
_LoggedInView, that shows aListViewwith some dummy data, anAppBarwith a "Profile" button, and a "Logout" button. -
Logged-Out UI: This will be a new private widget,
_LoggedOutView, that shows a centeredColumnwith a "Login" button.
Summary
The HomeScreen will be refactored to be dynamic, showing either a logged-in or logged-out view based on the AuthState. This will be achieved using the provider package's Consumer widget to listen for authentication state changes and conditionally render the appropriate UI. This approach is clean, efficient, and aligns with the existing architecture of the application.