-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(neon): Make account selection reusable
Signed-off-by: jld3103 <jld3103yt@gmail.com>
- Loading branch information
1 parent
775825a
commit 1117f5d
Showing
4 changed files
with
121 additions
and
93 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
packages/neon/neon/lib/src/widgets/account_selection_dialog.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:neon/src/blocs/accounts.dart'; | ||
import 'package:neon/src/theme/dialog.dart'; | ||
import 'package:neon/src/widgets/account_tile.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
@internal | ||
class NeonAccountSelectionDialog extends StatelessWidget { | ||
const NeonAccountSelectionDialog({ | ||
this.highlightActiveAccount = false, | ||
this.children, | ||
super.key, | ||
}); | ||
|
||
final bool highlightActiveAccount; | ||
final List<Widget>? children; | ||
|
||
@override | ||
Widget build(final BuildContext context) { | ||
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false); | ||
final accounts = accountsBloc.accounts.value; | ||
final activeAccount = accountsBloc.activeAccount.value!; | ||
|
||
final sortedAccounts = List.of(accounts) | ||
..removeWhere((final account) => account.id == activeAccount.id) | ||
..insert(0, activeAccount); | ||
|
||
final tiles = sortedAccounts | ||
.map<Widget>( | ||
(final account) => NeonAccountTile( | ||
account: account, | ||
trailing: highlightActiveAccount && account.id == activeAccount.id ? const Icon(Icons.check_circle) : null, | ||
onTap: () { | ||
Navigator.of(context).pop(account); | ||
}, | ||
), | ||
) | ||
.toList(); | ||
if (highlightActiveAccount) { | ||
tiles.insert(1, const Divider()); | ||
} | ||
|
||
final body = SingleChildScrollView( | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
...tiles, | ||
...?children, | ||
], | ||
), | ||
); | ||
|
||
return Dialog( | ||
child: IntrinsicHeight( | ||
child: Container( | ||
padding: const EdgeInsets.all(24), | ||
constraints: NeonDialogTheme.of(context).constraints, | ||
child: body, | ||
), | ||
), | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
packages/neon/neon/lib/src/widgets/account_switcher_button.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:neon/l10n/localizations.dart'; | ||
import 'package:neon/src/blocs/accounts.dart'; | ||
import 'package:neon/src/models/account.dart'; | ||
import 'package:neon/src/pages/settings.dart'; | ||
import 'package:neon/src/router.dart'; | ||
import 'package:neon/src/widgets/account_selection_dialog.dart'; | ||
import 'package:neon/src/widgets/user_avatar.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
@internal | ||
class AccountSwitcherButton extends StatelessWidget { | ||
const AccountSwitcherButton({ | ||
super.key, | ||
}); | ||
|
||
Future<void> _onPressed(final BuildContext context) async { | ||
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false); | ||
|
||
final account = await showDialog<Account>( | ||
context: context, | ||
builder: (final context) => NeonAccountSelectionDialog( | ||
highlightActiveAccount: true, | ||
children: [ | ||
ListTile( | ||
leading: const Icon(Icons.settings), | ||
title: Text(AppLocalizations.of(context).settingsAccountManage), | ||
onTap: () { | ||
Navigator.of(context).pop(); | ||
const SettingsRoute(initialCategory: SettingsCageories.accounts).push(context); | ||
}, | ||
), | ||
], | ||
), | ||
); | ||
|
||
if (account != null) { | ||
accountsBloc.setActiveAccount(account); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(final BuildContext context) { | ||
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false); | ||
final account = accountsBloc.activeAccount.value!; | ||
|
||
return IconButton( | ||
onPressed: () async => _onPressed(context), | ||
tooltip: AppLocalizations.of(context).settingsAccount, | ||
icon: NeonUserAvatar( | ||
account: account, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters