Add account switcher

Fixes #146
This commit is contained in:
Henry Hiles 2026-11-02 17:00:54 -05:00
commit 641341ed90
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs

View file

@ -0,0 +1,41 @@
import "package:flutter_hooks/flutter_hooks.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:material_ui/material_ui.dart";
import "package:nexus/controllers/active_account.dart";
import "package:nexus/controllers/notification_request.dart";
/// Runs [child] as one account, in a [ProviderScope] of its own that's
/// replaced when switching accounts, so no state leaks between them.
class const AccountScope({
required final String initialAccountId,
required final Widget child,
final List<ProviderObserver>? observers,
final Duration? Function(int retryCount, Object error)? retry,
super.key,
}) extends HookWidget {
@override
Widget build(BuildContext context) {
final active = useState<AccountSwitch>((
accountId: initialAccountId,
notificationRowId: null,
));
return ProviderScope(
key: ValueKey(active.value.accountId),
retry: retry,
observers: observers,
overrides: [
ActiveAccountController.provider.overrideWith(
() => ActiveAccountController(
active.value.accountId,
(next) => active.value = next,
),
),
NotificationRequestController.provider.overrideWithBuild(
(_, _) => active.value.notificationRowId,
),
],
child: child,
);
}
}