import 'package:flutter/material.dart'; import 'package:QuickSSH/classes/ServerCommand.dart'; class EditClient extends StatefulWidget { final ServerCommand? existingCommand; // Null if adding, not null if editing final int? index; const EditClient({super.key, this.existingCommand, this.index}); @override State createState() => _EditClientState(); } class _EditClientState extends State { final nameController = TextEditingController(); final ipController = TextEditingController(); final userController = TextEditingController(); final cmdController = TextEditingController(); final passController = TextEditingController(); @override void dispose() { nameController.dispose(); ipController.dispose(); userController.dispose(); cmdController.dispose(); passController.dispose(); super.dispose(); } @override void initState() { super.initState(); if (widget.existingCommand != null) { nameController.text = widget.existingCommand!.name; ipController.text = widget.existingCommand!.ip; userController.text = widget.existingCommand!.username; passController.text = widget.existingCommand!.password; cmdController.text = widget.existingCommand!.command; } } @override Widget build(BuildContext context) { final theme = Theme.of(context).colorScheme; return Scaffold( backgroundColor: theme.surface, resizeToAvoidBottomInset: false, appBar: AppBar( backgroundColor: theme.surface, title: Text("Edit", style: TextStyle(color: theme.onSurface)), leading: Builder( builder: (context) { return IconButton( icon: Icon(Icons.arrow_back_ios_rounded, color: theme.onSurface), onPressed: () { Navigator.pop(context); }, ); }, ), ), body: Padding( padding: const EdgeInsets.symmetric(horizontal: 8.0), child: Column( children: [ const SizedBox(height: 20), TextField( style: TextStyle(color: theme.onSurface), controller: nameController, decoration: InputDecoration( filled: true, fillColor: theme.primaryContainer, labelText: 'Command name', labelStyle: TextStyle( color: theme.onSurfaceVariant, fontSize: 20, fontWeight: FontWeight.bold, ), border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(20), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), ), ), const SizedBox(height: 20), TextField( style: TextStyle(color: theme.onSurface), controller: ipController, decoration: InputDecoration( filled: true, fillColor: theme.primaryContainer, labelText: 'Host (IP address)', labelStyle: TextStyle( color: theme.onSurfaceVariant, fontSize: 20, fontWeight: FontWeight.bold, ), border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(20), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), ), ), const SizedBox(height: 20), TextField( style: TextStyle(color: theme.onSurface), controller: userController, decoration: InputDecoration( filled: true, fillColor: theme.primaryContainer, labelText: 'Login as (user)', labelStyle: TextStyle( color: theme.onSurfaceVariant, fontSize: 20, fontWeight: FontWeight.bold, ), border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(20), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), ), ), const SizedBox(height: 20), TextField( style: TextStyle(color: theme.onSurface), controller: passController, obscureText: true, decoration: InputDecoration( filled: true, fillColor: theme.primaryContainer, labelText: 'Password', labelStyle: TextStyle( color: theme.onSurfaceVariant, fontSize: 20, fontWeight: FontWeight.bold, ), border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(20), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), ), ), const SizedBox(height: 20), TextField( style: TextStyle(color: theme.onSurface), controller: cmdController, minLines: 3, maxLines: 100, decoration: InputDecoration( filled: true, fillColor: theme.primaryContainer, labelText: 'Command', labelStyle: TextStyle( color: theme.onSurfaceVariant, fontSize: 20, fontWeight: FontWeight.bold, ), border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(20), borderSide: BorderSide(color: Color.fromARGB(0, 0, 0, 0)), ), ), ), Spacer(), Padding( padding: const EdgeInsets.symmetric( horizontal: 20.0, vertical: 20, ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ ElevatedButton( onPressed: () { showModalBottomSheet( context: context, shape: const RoundedRectangleBorder( borderRadius: BorderRadius.vertical( top: Radius.circular(25.0), ), ), builder: (BuildContext context) { return SizedBox( height: 400, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( 'Remove this command?', style: TextStyle(fontSize: 20), ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ ElevatedButton( onPressed: () { Navigator.pop(context); Navigator.pop(context, "DELETE"); }, style: ElevatedButton.styleFrom( backgroundColor: theme.error, fixedSize: const Size(140, 40), ), child: Text( 'Yes, remove', style: TextStyle( color: theme.surface, fontWeight: FontWeight.bold, ), ), ), ElevatedButton( onPressed: () => Navigator.pop(context), style: ElevatedButton.styleFrom( backgroundColor: theme.onSurface, fixedSize: const Size(140, 40), ), child: Text( 'No', style: TextStyle( color: theme.surface, fontWeight: FontWeight.bold, ), ), ), ], ), ], ), ), ); }, ); }, style: ElevatedButton.styleFrom( backgroundColor: theme.error, fixedSize: const Size(140, 40), ), child: Text( 'Remove', style: TextStyle( color: const Color.fromARGB(255, 0, 0, 0), fontWeight: FontWeight.bold, ), ), ), ElevatedButton( onPressed: _save, style: ElevatedButton.styleFrom( backgroundColor: theme.onSurface, fixedSize: const Size(140, 40), ), child: Text( 'Save', style: TextStyle( color: theme.surface, fontWeight: FontWeight.bold, ), ), ), ], ), ), ], ), ), ); } void _save() { final updatedCommand = ServerCommand( name: nameController.text, ip: ipController.text, username: userController.text, command: cmdController.text, password: passController.text, ); Navigator.pop(context, updatedCommand); } }