loris/lib/pages/settings/account.dart

76 lines
1.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:localization/localization.dart';
import '../../global.dart' as global;
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,
);
}
}
class LogoutButton extends StatelessWidget {
const LogoutButton({
required this.identity,
Key? key,
}) : super(key: key);
final String identity;
@override
Widget build(BuildContext context) {
return Row(
children: [
SelectableText(identity),
TextButton.icon(
onPressed: () async {
await logout(context, identity);
},
icon: const Icon(Icons.logout),
label: Text(
"logout".i18n(),
),
),
],
);
}
}
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(),
),
);
}
}
void addNewIdentity(context) {
Navigator.of(context).pushReplacementNamed("/login");
}
Future<void> logout(context, String identity) async {
await global.settings!.removeIdentity(identity);
if (global.settings!.identities.isEmpty) {
Navigator.of(context).pushReplacementNamed("/login");
} else {
(Navigator.of(context).pushReplacementNamed("/"));
}
}