From 12ff1d61c25b83269c6aa6f2b0af6d60605de96c Mon Sep 17 00:00:00 2001 From: soragui Date: Mon, 19 Jan 2026 14:56:39 +0800 Subject: [PATCH] feat: add home and profile screens --- lib/app.dart | 29 +++++++++++++++- lib/auth/auth_state.dart | 17 +++------- lib/home/home_screen.dart | 59 +++++++++++++++++++++++++++++++++ lib/profile/profile_screen.dart | 24 ++++++++++++++ 4 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 lib/home/home_screen.dart create mode 100644 lib/profile/profile_screen.dart diff --git a/lib/app.dart b/lib/app.dart index 8c27d90..81e56af 100644 --- a/lib/app.dart +++ b/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/phone_input_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'; final _router = GoRouter( routes: [ - GoRoute(path: '/', builder: (context, state) => const PhoneInputScreen()), + GoRoute(path: '/', builder: (context, state) => const HomeScreen()), + GoRoute( + path: '/login', + builder: (context, state) => const PhoneInputScreen(), + ), GoRoute( path: '/sms_verify', builder: (context, state) => const SmsVerificationScreen(), ), + GoRoute( + path: '/profile', + builder: (context, state) => const ProfileScreen(), + ), ], + redirect: (BuildContext context, GoRouterState state) { + final authState = Provider.of(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 { diff --git a/lib/auth/auth_state.dart b/lib/auth/auth_state.dart index ae24c8e..4e73cde 100644 --- a/lib/auth/auth_state.dart +++ b/lib/auth/auth_state.dart @@ -1,18 +1,11 @@ import 'package:flutter/material.dart'; -import 'package:firebase_auth/firebase_auth.dart'; class AuthState extends ChangeNotifier { - User? _user; - User? get user => _user; + bool _isLoggedIn = false; + bool get isLoggedIn => _isLoggedIn; - final FirebaseAuth _auth = FirebaseAuth.instance; - - AuthState() { - _auth.authStateChanges().listen((user) { - _user = user; - notifyListeners(); - }); + void toggleLogin() { + _isLoggedIn = !_isLoggedIn; + notifyListeners(); } - - bool get isLoggedIn => _user != null; } diff --git a/lib/home/home_screen.dart b/lib/home/home_screen.dart new file mode 100644 index 0000000..e9b909c --- /dev/null +++ b/lib/home/home_screen.dart @@ -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 createState() => _HomeScreenState(); +} + +class _HomeScreenState extends State { + 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(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(icon: Icon(Icons.home), label: 'Home'), + BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'), + ], + currentIndex: _selectedIndex, + onTap: _onItemTapped, + ), + ); + } +} diff --git a/lib/profile/profile_screen.dart b/lib/profile/profile_screen.dart new file mode 100644 index 0000000..7074e70 --- /dev/null +++ b/lib/profile/profile_screen.dart @@ -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(context, listen: false); + + return Scaffold( + appBar: AppBar(title: const Text('Profile')), + body: Center( + child: ElevatedButton( + onPressed: () { + authState.toggleLogin(); + }, + child: const Text('Logout'), + ), + ), + ); + } +}