logout working
This commit is contained in:
@@ -25,7 +25,9 @@ String _apiErrorMessage(RemoteDeckException e) {
|
||||
}
|
||||
|
||||
class CommunityDecksScreen extends StatefulWidget {
|
||||
const CommunityDecksScreen({super.key});
|
||||
const CommunityDecksScreen({super.key, this.showAppBar = true});
|
||||
|
||||
final bool showAppBar;
|
||||
|
||||
@override
|
||||
State<CommunityDecksScreen> createState() => CommunityDecksScreenState();
|
||||
@@ -178,9 +180,7 @@ class CommunityDecksScreenState extends State<CommunityDecksScreen> {
|
||||
final user = ApiAuthService.instance.currentUser.value;
|
||||
|
||||
if (user == null) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Community decks')),
|
||||
body: Center(
|
||||
final body = Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
@@ -200,13 +200,14 @@ class CommunityDecksScreenState extends State<CommunityDecksScreen> {
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
);
|
||||
if (widget.showAppBar) {
|
||||
return Scaffold(appBar: AppBar(title: const Text('Community decks')), body: body);
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Community decks')),
|
||||
body: _loading
|
||||
final body = _loading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _error != null
|
||||
? Center(
|
||||
@@ -294,7 +295,10 @@ class CommunityDecksScreenState extends State<CommunityDecksScreen> {
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
);
|
||||
if (widget.showAppBar) {
|
||||
return Scaffold(appBar: AppBar(title: const Text('Community decks')), body: body);
|
||||
}
|
||||
return body;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@ import '../data/default_deck.dart';
|
||||
import '../utils/top_snackbar.dart';
|
||||
|
||||
class DeckListScreen extends StatefulWidget {
|
||||
const DeckListScreen({super.key});
|
||||
const DeckListScreen({super.key, this.showAppBar = true});
|
||||
|
||||
final bool showAppBar;
|
||||
|
||||
@override
|
||||
State<DeckListScreen> createState() => DeckListScreenState();
|
||||
@@ -437,94 +439,8 @@ class DeckListScreenState extends State<DeckListScreen> {
|
||||
_loadDecks();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Image.asset(
|
||||
'android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png',
|
||||
height: 32,
|
||||
width: 32,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (_, __, ___) => Icon(
|
||||
Icons.auto_stories,
|
||||
size: 28,
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
'omotomo',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
letterSpacing: -0.5,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
ValueListenableBuilder<ApiUser?>(
|
||||
valueListenable: ApiAuthService.instance.currentUser,
|
||||
builder: (context, user, _) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (user != null) ...[
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 4),
|
||||
child: PopupMenuButton<void>(
|
||||
offset: const Offset(0, 40),
|
||||
tooltip: 'Account',
|
||||
child: CircleAvatar(
|
||||
radius: 18,
|
||||
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
|
||||
child: Text(
|
||||
_userInitials(user),
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.onPrimaryContainer,
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
),
|
||||
itemBuilder: (context) => [
|
||||
const PopupMenuItem<void>(
|
||||
value: null,
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.logout),
|
||||
SizedBox(width: 12),
|
||||
Text('Log out'),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
onSelected: (_) async {
|
||||
await ApiAuthService.instance.logout();
|
||||
if (!mounted) return;
|
||||
Navigator.of(context).pushNamedAndRemoveUntil(
|
||||
Routes.login,
|
||||
(route) => false,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.sync),
|
||||
tooltip: 'Sync from server',
|
||||
onPressed: () => _loadDecks(),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _decks.isEmpty
|
||||
Widget _buildBody(BuildContext context) {
|
||||
return _decks.isEmpty
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
@@ -727,7 +643,95 @@ class DeckListScreenState extends State<DeckListScreen> {
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final body = _buildBody(context);
|
||||
if (!widget.showAppBar) {
|
||||
return body;
|
||||
}
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Image.asset(
|
||||
'android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png',
|
||||
height: 32,
|
||||
width: 32,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (_, __, ___) => Icon(
|
||||
Icons.auto_stories,
|
||||
size: 28,
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
'omotomo',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
letterSpacing: -0.5,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
ValueListenableBuilder<ApiUser?>(
|
||||
valueListenable: ApiAuthService.instance.currentUser,
|
||||
builder: (context, user, _) {
|
||||
if (user == null) {
|
||||
return FilledButton.tonal(
|
||||
onPressed: () => Navigator.of(context).pushNamed(Routes.login),
|
||||
child: const Text('Log in'),
|
||||
);
|
||||
}
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.sync),
|
||||
tooltip: 'Sync from server',
|
||||
onPressed: () => _loadDecks(),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 4),
|
||||
child: PopupMenuButton<String>(
|
||||
offset: const Offset(0, 40),
|
||||
tooltip: 'Account',
|
||||
child: UserAvatar(user: user),
|
||||
itemBuilder: (context) => [
|
||||
const PopupMenuItem<String>(
|
||||
value: 'logout',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.logout),
|
||||
SizedBox(width: 12),
|
||||
Text('Log out'),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
onSelected: (value) async {
|
||||
if (value != 'logout') return;
|
||||
final navigator = Navigator.of(context);
|
||||
await ApiAuthService.instance.logout();
|
||||
navigator.pushNamedAndRemoveUntil(
|
||||
Routes.login,
|
||||
(route) => false,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: body,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -767,6 +771,49 @@ class _StatChip extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows profile image from [ApiUser.avatarUrl] when available, otherwise initials.
|
||||
/// Public so HomeScreen can use the same avatar in the shared app bar.
|
||||
class UserAvatar extends StatelessWidget {
|
||||
const UserAvatar({super.key, required this.user});
|
||||
|
||||
final ApiUser user;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final initials = DeckListScreenState._userInitials(user);
|
||||
final initialsWidget = Text(
|
||||
initials,
|
||||
style: TextStyle(
|
||||
color: theme.colorScheme.onPrimaryContainer,
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 16,
|
||||
),
|
||||
);
|
||||
final url = user.avatarUrl?.trim();
|
||||
if (url == null || url.isEmpty) {
|
||||
return CircleAvatar(
|
||||
radius: 18,
|
||||
backgroundColor: theme.colorScheme.primaryContainer,
|
||||
child: initialsWidget,
|
||||
);
|
||||
}
|
||||
return CircleAvatar(
|
||||
radius: 18,
|
||||
backgroundColor: theme.colorScheme.primaryContainer,
|
||||
child: ClipOval(
|
||||
child: Image.network(
|
||||
url,
|
||||
width: 36,
|
||||
height: 36,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => initialsWidget,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MockDataDialog extends StatefulWidget {
|
||||
@override
|
||||
State<_MockDataDialog> createState() => _MockDataDialogState();
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'deck_list_screen.dart';
|
||||
import '../routes.dart';
|
||||
import '../services/api_auth_service.dart';
|
||||
import 'community_decks_screen.dart';
|
||||
import 'deck_list_screen.dart';
|
||||
|
||||
/// Shell with tab navigation: My Decks and Community.
|
||||
class HomeScreen extends StatefulWidget {
|
||||
@@ -29,11 +31,89 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Image.asset(
|
||||
'android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png',
|
||||
height: 32,
|
||||
width: 32,
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (_, __, ___) => Icon(
|
||||
Icons.auto_stories,
|
||||
size: 28,
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
'omotomo',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
letterSpacing: -0.5,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
ValueListenableBuilder<ApiUser?>(
|
||||
valueListenable: ApiAuthService.instance.currentUser,
|
||||
builder: (context, user, _) {
|
||||
if (user == null) {
|
||||
return FilledButton.tonal(
|
||||
onPressed: () => Navigator.of(context).pushNamed(Routes.login),
|
||||
child: const Text('Log in'),
|
||||
);
|
||||
}
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.sync),
|
||||
tooltip: 'Sync from server',
|
||||
onPressed: () => _deckListKey.currentState?.refresh(),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 4),
|
||||
child: PopupMenuButton<String>(
|
||||
offset: const Offset(0, 40),
|
||||
tooltip: 'Account',
|
||||
child: UserAvatar(user: user),
|
||||
itemBuilder: (context) => [
|
||||
const PopupMenuItem<String>(
|
||||
value: 'logout',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.logout),
|
||||
SizedBox(width: 12),
|
||||
Text('Log out'),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
onSelected: (value) async {
|
||||
if (value != 'logout') return;
|
||||
final navigator = Navigator.of(context);
|
||||
await ApiAuthService.instance.logout();
|
||||
navigator.pushNamedAndRemoveUntil(
|
||||
Routes.login,
|
||||
(route) => false,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: IndexedStack(
|
||||
index: _currentIndex == 2 ? 1 : 0,
|
||||
children: [
|
||||
DeckListScreen(key: _deckListKey),
|
||||
CommunityDecksScreen(key: _communityKey),
|
||||
DeckListScreen(key: _deckListKey, showAppBar: false),
|
||||
CommunityDecksScreen(key: _communityKey, showAppBar: false),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
|
||||
Reference in New Issue
Block a user