import 'package:flutter/material.dart'; import 'package:QuickSSH/screens/edit_client.dart'; import '../widgets/server_status_tile.dart'; import 'add_client.dart'; import 'package:QuickSSH/classes/ServerCommand.dart'; import 'package:QuickSSH/services/storage_service.dart'; class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State { List allCommands = []; @override void initState() { super.initState(); _loadData(); } void _loadData() async { allCommands = await SecureStorageService.loadCommands(); setState(() {}); } @override Widget build(BuildContext context) { final theme = Theme.of(context).colorScheme; return Scaffold( appBar: AppBar( backgroundColor: theme.surface, leading: Builder( builder: (context) { return IconButton( icon: Icon(Icons.menu, color: theme.onSurface), onPressed: () { Scaffold.of(context).openDrawer(); }, ); }, ), ), drawer: Drawer( backgroundColor: theme.primaryContainer, child: ListView( children: [ ListTile( title: Text( "Settings", style: TextStyle( color: theme.onSurface, fontWeight: FontWeight.bold, fontSize: 20, ), ), ), ListTile( title: Text( "Help", style: TextStyle( color: theme.onSurface, fontWeight: FontWeight.bold, fontSize: 20, ), ), ), ], ), ), body: ListView.builder( itemCount: allCommands.length, itemBuilder: (context, index) { // You can keep this as 'context' or rename it return ServerStatusTile( command: allCommands[index], onEdit: () => _editCommand(allCommands[index], index), ); }, ), floatingActionButton: Builder( builder: (context) { return FloatingActionButton( backgroundColor: theme.primary, shape: const CircleBorder(), child: const Icon( Icons.add, color: Color.fromARGB(255, 0, 0, 0), size: 32, weight: 50, ), onPressed: () { _addCommand(); }, ); }, ), backgroundColor: theme.surface, ); } void _addCommand() async { // 1. Must be async print("Opening Add Screen..."); final result = await Navigator.push( // 2. Must have await context, MaterialPageRoute(builder: (context) => const AddClient()), ); // This part only runs AFTER you come back from AddClient if (result != null) { print("Received data: ${result.name}"); setState(() { allCommands.add(result); }); SecureStorageService.saveCommands(allCommands); } else { print("Result was null - user might have just hit 'Back'"); } } _editCommand(ServerCommand cmdToEdit, int index) async { final result = await Navigator.push( context, MaterialPageRoute( builder: (context) => EditClient(existingCommand: cmdToEdit), ), ); if (result != null) { if (result == "DELETE") { setState(() { allCommands.removeAt(index); }); SecureStorageService.saveCommands(allCommands); return; } print("Received edited data: ${result.name}"); setState(() { allCommands[index] = result; }); SecureStorageService.saveCommands(allCommands); } else { print("Edit result was null - user might have just hit 'Back'"); } } }