feat: add home and profile screens
This commit is contained in:
29
lib/app.dart
29
lib/app.dart
@@ -3,16 +3,43 @@ import 'package:go_router/go_router.dart';
|
|||||||
import 'package:phone_login/auth/auth_state.dart';
|
import 'package:phone_login/auth/auth_state.dart';
|
||||||
import 'package:phone_login/auth/phone_input_screen.dart';
|
import 'package:phone_login/auth/phone_input_screen.dart';
|
||||||
import 'package:phone_login/auth/sms_verification_screen.dart';
|
import 'package:phone_login/auth/sms_verification_screen.dart';
|
||||||
|
import 'package:phone_login/home/home_screen.dart';
|
||||||
|
import 'package:phone_login/profile/profile_screen.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
final _router = GoRouter(
|
final _router = GoRouter(
|
||||||
routes: [
|
routes: [
|
||||||
GoRoute(path: '/', builder: (context, state) => const PhoneInputScreen()),
|
GoRoute(path: '/', builder: (context, state) => const HomeScreen()),
|
||||||
|
GoRoute(
|
||||||
|
path: '/login',
|
||||||
|
builder: (context, state) => const PhoneInputScreen(),
|
||||||
|
),
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/sms_verify',
|
path: '/sms_verify',
|
||||||
builder: (context, state) => const SmsVerificationScreen(),
|
builder: (context, state) => const SmsVerificationScreen(),
|
||||||
),
|
),
|
||||||
|
GoRoute(
|
||||||
|
path: '/profile',
|
||||||
|
builder: (context, state) => const ProfileScreen(),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
|
redirect: (BuildContext context, GoRouterState state) {
|
||||||
|
final authState = Provider.of<AuthState>(context, listen: false);
|
||||||
|
final bool loggedIn = authState.isLoggedIn;
|
||||||
|
final bool loggingIn =
|
||||||
|
state.matchedLocation == '/login' ||
|
||||||
|
state.matchedLocation == '/sms_verify';
|
||||||
|
|
||||||
|
if (!loggedIn && !loggingIn) {
|
||||||
|
return '/login';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loggedIn && loggingIn) {
|
||||||
|
return '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
class App extends StatelessWidget {
|
class App extends StatelessWidget {
|
||||||
|
|||||||
@@ -1,18 +1,11 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:firebase_auth/firebase_auth.dart';
|
|
||||||
|
|
||||||
class AuthState extends ChangeNotifier {
|
class AuthState extends ChangeNotifier {
|
||||||
User? _user;
|
bool _isLoggedIn = false;
|
||||||
User? get user => _user;
|
bool get isLoggedIn => _isLoggedIn;
|
||||||
|
|
||||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
void toggleLogin() {
|
||||||
|
_isLoggedIn = !_isLoggedIn;
|
||||||
AuthState() {
|
notifyListeners();
|
||||||
_auth.authStateChanges().listen((user) {
|
|
||||||
_user = user;
|
|
||||||
notifyListeners();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool get isLoggedIn => _user != null;
|
|
||||||
}
|
}
|
||||||
|
|||||||
59
lib/home/home_screen.dart
Normal file
59
lib/home/home_screen.dart
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:phone_login/auth/auth_state.dart';
|
||||||
|
|
||||||
|
class HomeScreen extends StatefulWidget {
|
||||||
|
const HomeScreen({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<HomeScreen> createState() => _HomeScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _HomeScreenState extends State<HomeScreen> {
|
||||||
|
int _selectedIndex = 0;
|
||||||
|
|
||||||
|
void _onItemTapped(int index) {
|
||||||
|
setState(() {
|
||||||
|
_selectedIndex = index;
|
||||||
|
});
|
||||||
|
if (index == 1) {
|
||||||
|
context.go('/profile');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final authState = Provider.of<AuthState>(context);
|
||||||
|
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('Home')),
|
||||||
|
body: Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
if (authState.isLoggedIn)
|
||||||
|
const Text('Welcome back!')
|
||||||
|
else
|
||||||
|
const Text('Please log in.'),
|
||||||
|
const SizedBox(height: 20),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
authState.toggleLogin();
|
||||||
|
},
|
||||||
|
child: Text(authState.isLoggedIn ? 'Logout' : 'Login'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
bottomNavigationBar: BottomNavigationBar(
|
||||||
|
items: const <BottomNavigationBarItem>[
|
||||||
|
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
|
||||||
|
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
|
||||||
|
],
|
||||||
|
currentIndex: _selectedIndex,
|
||||||
|
onTap: _onItemTapped,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
24
lib/profile/profile_screen.dart
Normal file
24
lib/profile/profile_screen.dart
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:phone_login/auth/auth_state.dart';
|
||||||
|
|
||||||
|
class ProfileScreen extends StatelessWidget {
|
||||||
|
const ProfileScreen({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final authState = Provider.of<AuthState>(context, listen: false);
|
||||||
|
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('Profile')),
|
||||||
|
body: Center(
|
||||||
|
child: ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
authState.toggleLogin();
|
||||||
|
},
|
||||||
|
child: const Text('Logout'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user