111 lines
3.1 KiB
Dart
111 lines
3.1 KiB
Dart
/// Shared helpers to open Settings from app bars.
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../providers/auth_provider.dart';
|
|
import '../screens/account_screen.dart';
|
|
import '../screens/login_screen.dart';
|
|
import '../screens/settings_screen.dart';
|
|
|
|
Widget authAppBarAction(BuildContext context) {
|
|
final auth = context.watch<AuthController>();
|
|
|
|
if (auth.isLoggedIn) {
|
|
return PopupMenuButton<_AccountMenuAction>(
|
|
tooltip: auth.displayName,
|
|
offset: const Offset(0, 48),
|
|
onSelected: (action) async {
|
|
switch (action) {
|
|
case _AccountMenuAction.account:
|
|
await Navigator.of(context).push(
|
|
MaterialPageRoute<void>(builder: (_) => const AccountScreen()),
|
|
);
|
|
case _AccountMenuAction.logout:
|
|
await auth.logout();
|
|
}
|
|
},
|
|
itemBuilder: (context) {
|
|
final email = auth.email;
|
|
return [
|
|
if (email != null)
|
|
PopupMenuItem<_AccountMenuAction>(
|
|
enabled: false,
|
|
child: Text(
|
|
email,
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
),
|
|
const PopupMenuItem<_AccountMenuAction>(
|
|
value: _AccountMenuAction.account,
|
|
child: ListTile(
|
|
contentPadding: EdgeInsets.zero,
|
|
leading: Icon(Icons.person_outline),
|
|
title: Text('Account'),
|
|
),
|
|
),
|
|
const PopupMenuItem<_AccountMenuAction>(
|
|
value: _AccountMenuAction.logout,
|
|
child: ListTile(
|
|
contentPadding: EdgeInsets.zero,
|
|
leading: Icon(Icons.logout),
|
|
title: Text('Sign out'),
|
|
),
|
|
),
|
|
];
|
|
},
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
child: CircleAvatar(
|
|
radius: 16,
|
|
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
|
|
child: Text(
|
|
auth.initials,
|
|
style: Theme.of(context).textTheme.labelLarge?.copyWith(
|
|
color: Theme.of(context).colorScheme.onPrimaryContainer,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
return IconButton(
|
|
tooltip: 'Sign in',
|
|
onPressed: () {
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute<bool>(builder: (_) => const LoginScreen()),
|
|
);
|
|
},
|
|
icon: const Icon(Icons.person_outline),
|
|
);
|
|
}
|
|
|
|
enum _AccountMenuAction { account, logout }
|
|
|
|
List<Widget> bankAppBarActions({
|
|
required BuildContext context,
|
|
VoidCallback? onRefresh,
|
|
bool refreshing = false,
|
|
}) {
|
|
return [
|
|
if (onRefresh != null)
|
|
IconButton(
|
|
tooltip: 'Refresh',
|
|
onPressed: refreshing ? null : onRefresh,
|
|
icon: const Icon(Icons.refresh),
|
|
),
|
|
authAppBarAction(context),
|
|
IconButton(
|
|
tooltip: 'Settings',
|
|
onPressed: () {
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute<void>(builder: (_) => const SettingsScreen()),
|
|
);
|
|
},
|
|
icon: const Icon(Icons.settings_outlined),
|
|
),
|
|
];
|
|
}
|