1 import 'package:flutter/material.dart';
2 import 'package:built_collection/built_collection.dart';
3 import 'package:proxmox_login_manager/proxmox_general_settings_form.dart';
4 import 'package:proxmox_login_manager/proxmox_login_form.dart';
5 import 'package:proxmox_login_manager/proxmox_login_model.dart';
6 import 'package:proxmox_dart_api_client/proxmox_dart_api_client.dart'
8 import 'package:proxmox_login_manager/extension.dart';
9 import 'package:proxmox_login_manager/proxmox_password_store.dart';
11 typedef OnLoginCallback = Function(proxclient.ProxmoxApiClient client);
13 class ProxmoxLoginSelector extends StatefulWidget {
14 final OnLoginCallback? onLogin;
16 const ProxmoxLoginSelector({super.key, this.onLogin});
19 State<ProxmoxLoginSelector> createState() => _ProxmoxLoginSelectorState();
22 class _ProxmoxLoginSelectorState extends State<ProxmoxLoginSelector> {
23 Future<ProxmoxLoginStorage?>? loginStorage;
27 loginStorage = ProxmoxLoginStorage.fromLocalStorage();
31 Widget build(BuildContext context) {
34 backgroundColor: Theme.of(context).colorScheme.background,
37 crossAxisAlignment: CrossAxisAlignment.start,
46 'Virtual Environment',
55 icon: const Icon(Icons.settings),
57 Navigator.of(context).push(MaterialPageRoute(
58 builder: (context) => const ProxmoxGeneralSettingsForm(),
63 body: FutureBuilder<ProxmoxLoginStorage?>(
65 builder: (context, snapshot) {
66 if (!snapshot.hasData) {
68 child: CircularProgressIndicator(),
71 if (snapshot.hasData &&
72 (snapshot.data!.logins?.isEmpty ?? true)) {
74 child: Text('Add an account'),
77 var items = <Widget>[];
78 final BuiltList<ProxmoxLoginModel> logins =
79 snapshot.data?.logins ?? BuiltList<ProxmoxLoginModel>();
81 final activeSessions =
82 logins.rebuild((b) => b.where((b) => b.activeSession));
84 if (activeSessions.isNotEmpty) {
87 padding: EdgeInsets.all(12.0),
92 fontWeight: FontWeight.bold,
96 ...activeSessions.map((s) => ListTile(
97 title: Text(s.fullHostname),
98 subtitle: Text(s.fullUsername),
99 trailing: const Icon(Icons.navigate_next),
100 leading: PopupMenuButton(
101 icon: const Icon(Icons.more_vert,
102 color: Colors.green),
103 itemBuilder: (context) => [
107 leading: const Icon(Icons.logout),
108 title: const Text('Logout'),
111 .rebuild((b) => b.logins
112 .rebuildWhere((m) => s == m,
113 (b) => b..ticket = ''))
115 refreshFromStorage();
116 if (context.mounted) {
117 Navigator.of(context).pop();
123 onTap: () => _login(user: s),
129 padding: EdgeInsets.all(12.0),
134 fontWeight: FontWeight.bold,
138 ...logins.where((b) => !b.activeSession).map((login) =>
140 title: Text(login.fullHostname),
141 subtitle: Text(login.fullUsername),
142 trailing: const Icon(Icons.navigate_next),
143 leading: PopupMenuButton(
144 itemBuilder: (context) => [
145 if (login.passwordSaved ?? false)
149 leading: const Icon(Icons.key_off),
150 title: const Text('Delete Password'),
152 await deletePassword(login.identifier!);
156 ..logins.rebuildWhere(
159 b..passwordSaved = false))
161 refreshFromStorage();
162 if (context.mounted) {
163 Navigator.of(context).pop();
171 leading: const Icon(Icons.delete),
172 title: const Text('Delete'),
174 await deletePassword(login.identifier!);
177 (b) => b.logins.remove(login))
179 refreshFromStorage();
180 if (context.mounted) {
181 Navigator.of(context).pop();
187 onTap: () => _login(user: login),
194 floatingActionButton: FloatingActionButton.extended(
195 onPressed: () => _login(isCreate: true),
196 label: const Text('Add'),
197 icon: const Icon(Icons.account_circle),
203 Future<void> _login({ProxmoxLoginModel? user, bool isCreate = false}) async {
205 bool activeSession = user?.activeSession ?? false;
206 String ticket = (activeSession ? user?.ticket : null) ?? '';
207 bool passwordSaved = user != null && (user.passwordSaved ?? false);
209 if (ticket == '' && passwordSaved) {
210 password = await getPassword(user.identifier!);
214 final client = await Navigator.of(context).push(MaterialPageRoute(
215 builder: (context) => ProxmoxLoginPage(
221 refreshFromStorage();
222 if (client != null) {
223 widget.onLogin!(client);
228 void refreshFromStorage() {
230 loginStorage = ProxmoxLoginStorage.fromLocalStorage();