Solian/lib/widgets/navigation/app_navigation_drawer.dart

197 lines
6.7 KiB
Dart
Raw Normal View History

2024-07-12 03:39:44 +00:00
import 'package:flutter/material.dart';
2024-07-12 05:15:46 +00:00
import 'package:get/get.dart';
import 'package:solian/models/account_status.dart';
import 'package:solian/providers/account_status.dart';
import 'package:solian/providers/auth.dart';
2024-08-02 15:15:28 +00:00
import 'package:solian/providers/relation.dart';
2024-07-12 03:39:44 +00:00
import 'package:solian/router.dart';
import 'package:solian/shells/root_shell.dart';
import 'package:solian/theme.dart';
2024-07-12 05:15:46 +00:00
import 'package:solian/widgets/account/account_avatar.dart';
import 'package:solian/widgets/account/account_status_action.dart';
2024-07-12 03:39:44 +00:00
import 'package:solian/widgets/navigation/app_navigation.dart';
2024-07-12 05:15:46 +00:00
import 'package:badges/badges.dart' as badges;
2024-08-07 10:24:16 +00:00
import 'package:solian/widgets/navigation/app_navigation_regions.dart';
2024-07-12 03:39:44 +00:00
class AppNavigationDrawer extends StatefulWidget {
2024-07-12 05:15:46 +00:00
final String? routeName;
const AppNavigationDrawer({super.key, this.routeName});
2024-07-12 03:39:44 +00:00
@override
State<AppNavigationDrawer> createState() => _AppNavigationDrawerState();
}
class _AppNavigationDrawerState extends State<AppNavigationDrawer> {
2024-07-12 05:15:46 +00:00
AccountStatus? _accountStatus;
2024-07-31 05:29:26 +00:00
Future<void> _getStatus() async {
2024-07-12 05:15:46 +00:00
final StatusProvider provider = Get.find();
final resp = await provider.getCurrentStatus();
final status = AccountStatus.fromJson(resp.body);
setState(() {
_accountStatus = status;
});
}
2024-07-31 05:29:26 +00:00
void _closeDrawer() {
2024-07-12 08:19:54 +00:00
rootScaffoldKey.currentState!.closeDrawer();
2024-07-12 05:15:46 +00:00
}
@override
void initState() {
super.initState();
2024-07-31 05:29:26 +00:00
_getStatus();
2024-07-12 05:15:46 +00:00
}
2024-07-12 03:39:44 +00:00
@override
Widget build(BuildContext context) {
2024-07-12 05:15:46 +00:00
final AuthProvider auth = Get.find();
2024-08-07 10:24:16 +00:00
return Drawer(
backgroundColor:
SolianTheme.isLargeScreen(context) ? Colors.transparent : null,
2024-08-07 10:24:16 +00:00
child: SafeArea(
child: Column(
children: [
Obx(() {
if (auth.isAuthorized.isFalse || auth.userProfile.value == null) {
return ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 28),
leading: const Icon(Icons.account_circle),
title: Text('guest'.tr),
subtitle: Text('unsignedIn'.tr),
onTap: () {
AppRouter.instance.goNamed('account');
_closeDrawer();
},
2024-07-24 17:18:47 +00:00
);
2024-08-07 10:24:16 +00:00
}
return ListTile(
2024-08-09 14:59:24 +00:00
contentPadding: const EdgeInsets.only(left: 20, right: 20),
2024-08-07 10:24:16 +00:00
title: Text(
auth.userProfile.value!['nick'],
2024-07-24 17:18:47 +00:00
maxLines: 1,
overflow: TextOverflow.fade,
2024-08-02 15:15:28 +00:00
),
2024-08-07 10:24:16 +00:00
subtitle: Builder(
builder: (context) {
if (_accountStatus == null) {
return Text('loading'.tr);
}
final info = StatusProvider.determineStatus(
_accountStatus!,
);
return Text(
info.$3,
maxLines: 1,
overflow: TextOverflow.fade,
);
},
2024-07-24 17:18:47 +00:00
),
2024-08-07 10:24:16 +00:00
leading: Obx(() {
final statusBadgeColor = _accountStatus != null
? StatusProvider.determineStatus(
_accountStatus!,
).$2
: Colors.grey;
final RelationshipProvider relations = Get.find();
final accountNotifications =
relations.friendRequestCount.value;
return badges.Badge(
badgeContent: Text(
accountNotifications.toString(),
style: const TextStyle(color: Colors.white),
),
showBadge: accountNotifications > 0,
position: badges.BadgePosition.topEnd(
top: -10,
end: -6,
),
child: badges.Badge(
showBadge: _accountStatus != null,
badgeStyle:
badges.BadgeStyle(badgeColor: statusBadgeColor),
position: badges.BadgePosition.bottomEnd(
bottom: 0,
end: -2,
),
child: AccountAvatar(
content: auth.userProfile.value!['avatar'],
),
),
);
}),
onTap: () {
AppRouter.instance.goNamed('account');
_closeDrawer();
},
onLongPress: () {
showModalBottomSheet(
useRootNavigator: true,
context: context,
builder: (context) => AccountStatusAction(
currentStatus: _accountStatus!.status,
),
).then((val) {
if (val == true) _getStatus();
});
},
2024-07-24 17:18:47 +00:00
);
2024-08-09 14:59:24 +00:00
}).paddingSymmetric(vertical: 8),
2024-08-07 10:24:16 +00:00
const Divider(thickness: 0.3, height: 1),
Column(
children: AppNavigation.destinations
.map(
(e) => ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 20,
2024-07-12 13:59:16 +00:00
),
2024-08-09 14:59:24 +00:00
leading: Icon(e.icon, size: 20).paddingAll(2),
2024-08-07 10:24:16 +00:00
title: Text(e.label),
enabled: true,
onTap: () {
AppRouter.instance.goNamed(e.page);
_closeDrawer();
},
2024-07-24 17:18:47 +00:00
),
2024-08-07 10:24:16 +00:00
)
.toList(),
).paddingSymmetric(vertical: 8),
const Divider(thickness: 0.3, height: 1),
Expanded(
child: AppNavigationRegions(
onSelected: (item) {
_closeDrawer();
},
2024-07-24 17:18:47 +00:00
),
2024-08-07 10:24:16 +00:00
),
2024-08-09 16:51:54 +00:00
const Divider(thickness: 0.3, height: 1),
Column(
children: [
ListTile(
minTileHeight: 0,
contentPadding: const EdgeInsets.symmetric(
horizontal: 20,
),
leading: const Icon(Icons.settings, size: 20).paddingAll(2),
title: Text('settings'.tr),
onTap: () {
AppRouter.instance.pushNamed('settings');
_closeDrawer();
},
),
],
).paddingOnly(top: 8)
2024-08-07 10:24:16 +00:00
],
),
),
2024-07-12 03:39:44 +00:00
);
}
}