Surface/lib/screens/account.dart

260 lines
8.9 KiB
Dart
Raw Normal View History

import 'dart:ui';
2024-11-09 00:09:46 +08:00
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
2024-11-09 19:32:21 +08:00
import 'package:gap/gap.dart';
2024-11-09 18:28:45 +08:00
import 'package:go_router/go_router.dart';
2024-12-11 22:49:55 +08:00
import 'package:hive_flutter/hive_flutter.dart';
2024-11-09 19:32:21 +08:00
import 'package:material_symbols_icons/symbols.dart';
import 'package:provider/provider.dart';
import 'package:styled_widget/styled_widget.dart';
2024-12-08 01:01:20 +08:00
import 'package:surface/providers/sn_network.dart';
2024-11-09 19:32:21 +08:00
import 'package:surface/providers/userinfo.dart';
2024-12-09 21:47:27 +08:00
import 'package:surface/providers/websocket.dart';
2024-11-09 19:32:21 +08:00
import 'package:surface/widgets/account/account_image.dart';
2024-12-05 22:22:38 +08:00
import 'package:surface/widgets/app_bar_leading.dart';
2024-11-09 19:32:21 +08:00
import 'package:surface/widgets/dialog.dart';
import 'package:surface/widgets/navigation/app_scaffold.dart';
import 'package:surface/widgets/universal_image.dart';
2024-11-09 00:09:46 +08:00
2024-11-09 19:32:21 +08:00
class AccountScreen extends StatelessWidget {
2024-11-09 00:09:46 +08:00
const AccountScreen({super.key});
@override
Widget build(BuildContext context) {
2024-11-09 19:32:21 +08:00
final ua = context.watch<UserProvider>();
final sn = context.read<SnNetworkProvider>();
2024-11-09 19:32:21 +08:00
return AppScaffold(
2024-11-09 00:09:46 +08:00
appBar: AppBar(
2024-12-05 22:22:38 +08:00
leading: AutoAppBarLeading(),
title: Text(
"screenAccount",
style: TextStyle(
color: Colors.white,
shadows: [
Shadow(
offset: Offset(1, 1),
blurRadius: 5.0,
color: Color.fromARGB(255, 0, 0, 0),
),
],
),
).tr(),
flexibleSpace: ua.user != null && ua.user!.banner.isNotEmpty
? Stack(
fit: StackFit.expand,
children: [
AutoResizeUniversalImage(sn.getAttachmentUrl(ua.user!.banner), fit: BoxFit.cover),
Positioned(
top: 0,
left: 0,
right: 0,
height: 56 + MediaQuery.of(context).padding.top,
child: ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: 10,
sigmaY: 10,
),
child: Container(
color: Colors.black.withOpacity(
clampDouble(10 * 0.1, 0, 0.5),
),
),
),
),
),
],
)
: null,
actions: [
IconButton(
icon: const Icon(Symbols.settings, fill: 1),
onPressed: () {
GoRouter.of(context).pushNamed('settings');
},
),
2024-11-24 20:23:06 +08:00
const Gap(8),
],
2024-11-09 18:28:45 +08:00
),
2024-11-09 19:32:21 +08:00
body: SingleChildScrollView(
2024-12-08 01:12:45 +08:00
child: ua.isAuthorized ? _AuthorizedAccountScreen() : _UnauthorizedAccountScreen(),
2024-11-09 00:09:46 +08:00
),
);
}
}
2024-11-09 19:32:21 +08:00
class _AuthorizedAccountScreen extends StatelessWidget {
const _AuthorizedAccountScreen();
2024-11-09 19:32:21 +08:00
@override
Widget build(BuildContext context) {
final ua = context.watch<UserProvider>();
return Column(
children: [
Card(
child: Builder(builder: (context) {
if (ua.user == null) {
return SizedBox(
width: double.infinity,
height: 120,
child: CircularProgressIndicator().center(),
);
}
return SizedBox(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AccountImage(content: ua.user!.avatar, radius: 28),
const Gap(8),
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
2024-12-08 01:12:45 +08:00
Text(ua.user!.nick).textStyle(Theme.of(context).textTheme.titleLarge!),
2024-11-09 19:32:21 +08:00
const Gap(4),
2024-12-08 01:12:45 +08:00
Text('@${ua.user!.name}').textStyle(Theme.of(context).textTheme.bodySmall!),
2024-11-09 19:32:21 +08:00
],
),
2024-12-08 01:12:45 +08:00
Text(ua.user!.description).textStyle(Theme.of(context).textTheme.bodyMedium!),
2024-11-09 19:32:21 +08:00
],
),
);
}).padding(all: 20),
).padding(horizontal: 8, top: 16, bottom: 4),
ListTile(
title: Text('accountPublishers').tr(),
subtitle: Text('accountPublishersSubtitle').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.face),
2024-11-10 18:37:34 +08:00
trailing: const Icon(Symbols.chevron_right),
onTap: () {
GoRouter.of(context).pushNamed('accountPublishers');
},
2024-11-09 19:32:21 +08:00
),
2024-12-08 01:01:20 +08:00
ListTile(
title: Text('abuseReport').tr(),
subtitle: Text('abuseReportActionDescription').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.flag),
trailing: const Icon(Symbols.chevron_right),
onTap: () {
2024-12-08 14:44:55 +08:00
GoRouter.of(context).pushNamed('abuseReport');
2024-12-08 01:01:20 +08:00
},
),
2025-01-28 00:52:44 +08:00
ListTile(
title: Text('factorSettings').tr(),
subtitle: Text('factorSettingsSubtitle').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.lock),
trailing: const Icon(Symbols.chevron_right),
onTap: () {
GoRouter.of(context).pushNamed('factorSettings');
},
),
2025-01-29 15:18:35 +08:00
ListTile(
title: Text('accountWallet').tr(),
subtitle: Text('accountWalletSubtitle').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.wallet),
trailing: const Icon(Symbols.chevron_right),
onTap: () {
GoRouter.of(context).pushNamed('accountWallet');
},
),
ListTile(
title: Text('accountSettings').tr(),
subtitle: Text('accountSettingsSubtitle').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.manage_accounts),
trailing: const Icon(Symbols.chevron_right),
onTap: () {
GoRouter.of(context).pushNamed('accountSettings');
},
),
2024-11-09 19:32:21 +08:00
ListTile(
title: Text('accountLogout').tr(),
subtitle: Text('accountLogoutSubtitle').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.logout),
2024-11-10 18:37:34 +08:00
trailing: const Icon(Symbols.chevron_right),
2024-12-11 22:49:55 +08:00
onTap: () async {
final confirm = await context.showConfirmDialog(
2024-11-09 19:32:21 +08:00
'accountLogoutConfirmTitle'.tr(),
'accountLogoutConfirm'.tr(),
2024-12-11 22:49:55 +08:00
);
if (!confirm) return;
if (!context.mounted) return;
ua.logoutUser();
final ws = context.read<WebSocketProvider>();
ws.disconnect();
await Hive.deleteFromDisk();
await Hive.initFlutter();
2024-11-09 19:32:21 +08:00
},
),
],
);
}
}
class _UnauthorizedAccountScreen extends StatelessWidget {
const _UnauthorizedAccountScreen();
2024-11-09 19:32:21 +08:00
@override
Widget build(BuildContext context) {
return Column(
children: [
Card(
child: SizedBox(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const CircleAvatar(
radius: 28,
child: Icon(Symbols.waving_hand, size: 28),
),
2024-11-09 19:32:21 +08:00
const Gap(8),
2024-12-08 01:12:45 +08:00
Text('accountIntroTitle').tr().textStyle(Theme.of(context).textTheme.titleLarge!),
2024-11-09 19:32:21 +08:00
Text('accountIntroSubtitle').tr(),
],
).padding(all: 20),
),
).padding(horizontal: 8, top: 16, bottom: 4),
ListTile(
title: Text('screenAuthLogin').tr(),
subtitle: Text('screenAuthLoginSubtitle').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.login),
2024-11-10 18:37:34 +08:00
trailing: const Icon(Symbols.chevron_right),
2024-11-09 19:32:21 +08:00
onTap: () {
GoRouter.of(context).pushNamed('authLogin').then((value) {
if (value == true && context.mounted) {
final ua = context.read<UserProvider>();
context.showSnackbar('loginSuccess'.tr(args: [
'@${ua.user?.name} (${ua.user?.nick})',
]));
}
});
2024-11-09 19:32:21 +08:00
},
),
ListTile(
title: Text('screenAuthRegister').tr(),
subtitle: Text('screenAuthRegisterSubtitle').tr(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Symbols.person_add),
2024-11-10 18:37:34 +08:00
trailing: const Icon(Symbols.chevron_right),
2024-11-09 19:32:21 +08:00
onTap: () {
GoRouter.of(context).pushNamed('authRegister');
},
),
],
);
}
}