loris/lib/pages/settings/account.dart

76 lines
1.9 KiB
Dart
Raw Normal View History

2022-07-31 17:59:25 +00:00
import 'package:flutter/material.dart';
import 'package:localization/localization.dart';
2022-08-12 17:00:09 +00:00
import '../../global.dart' as global;
2022-07-31 17:59:25 +00:00
class AccountSettings extends StatelessWidget {
const AccountSettings({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
List<Widget> children = [];
for (int i = 0; i < global.settings!.identities.length; i++) {
children.add(
LogoutButton(identity: global.settings!.identities.keys.toList()[i]));
}
children.add(const NewAccountButton());
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
2022-07-31 17:59:25 +00:00
);
}
}
class LogoutButton extends StatelessWidget {
const LogoutButton({
required this.identity,
Key? key,
}) : super(key: key);
final String identity;
2022-07-31 17:59:25 +00:00
@override
Widget build(BuildContext context) {
return Row(
children: [
2022-08-14 11:32:26 +00:00
SelectableText(identity),
2022-07-31 17:59:25 +00:00
TextButton.icon(
2022-08-14 14:34:28 +00:00
onPressed: () async {
await logout(context, identity);
},
icon: const Icon(Icons.logout),
label: Text(
"logout".i18n(),
),
),
2022-07-31 17:59:25 +00:00
],
);
}
}
class NewAccountButton extends StatelessWidget {
const NewAccountButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return TextButton.icon(
onPressed: (() => addNewIdentity(context)),
icon: const Icon(Icons.person_add),
label: Text(
"add-account".i18n(),
),
);
}
}
2022-08-14 11:32:26 +00:00
void addNewIdentity(context) {
Navigator.of(context).pushReplacementNamed("/login");
}
2022-08-14 14:34:28 +00:00
Future<void> logout(context, String identity) async {
2022-08-14 11:32:26 +00:00
await global.settings!.removeIdentity(identity);
if (global.settings!.identities.isEmpty) {
Navigator.of(context).pushReplacementNamed("/login");
2022-08-14 14:34:28 +00:00
} else {
(Navigator.of(context).pushReplacementNamed("/"));
2022-08-14 11:32:26 +00:00
}
2022-07-31 17:59:25 +00:00
}