initial upload
This commit is contained in:
142
lib/widgets/package_dialog.dart
Normal file
142
lib/widgets/package_dialog.dart
Normal file
@ -0,0 +1,142 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../models/udp_package.dart';
|
||||
|
||||
class PackageDialog extends StatefulWidget {
|
||||
final UdpPackage? package;
|
||||
final Function(UdpPackage) onSave;
|
||||
|
||||
const PackageDialog({
|
||||
super.key,
|
||||
this.package,
|
||||
required this.onSave,
|
||||
});
|
||||
|
||||
@override
|
||||
State<PackageDialog> createState() => _PackageDialogState();
|
||||
}
|
||||
|
||||
class _PackageDialogState extends State<PackageDialog> {
|
||||
late TextEditingController _nameController;
|
||||
late TextEditingController _ipController;
|
||||
late TextEditingController _dataController;
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_nameController = TextEditingController(text: widget.package?.name ?? '');
|
||||
_ipController = TextEditingController(
|
||||
text: widget.package?.ipAddress ?? '127.0.0.1',
|
||||
);
|
||||
_dataController = TextEditingController(text: widget.package?.data ?? '');
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameController.dispose();
|
||||
_ipController.dispose();
|
||||
_dataController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool _isValidIp(String ip) {
|
||||
final parts = ip.split('.');
|
||||
if (parts.length != 4) return false;
|
||||
for (final part in parts) {
|
||||
final num = int.tryParse(part);
|
||||
if (num == null || num < 0 || num > 255) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void _save() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
final package = UdpPackage(
|
||||
id: widget.package?.id ?? DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
name: _nameController.text.trim(),
|
||||
ipAddress: _ipController.text.trim(),
|
||||
data: _dataController.text,
|
||||
);
|
||||
widget.onSave(package);
|
||||
Navigator.pop(context);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(widget.package == null ? 'New Package' : 'Edit Package'),
|
||||
content: SingleChildScrollView(
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Package Name',
|
||||
prefixIcon: Icon(Icons.label),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Please enter a package name';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
autofocus: true,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _ipController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'IP Address',
|
||||
prefixIcon: Icon(Icons.computer),
|
||||
hintText: '192.168.1.100',
|
||||
),
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Please enter an IP address';
|
||||
}
|
||||
if (!_isValidIp(value.trim())) {
|
||||
return 'Invalid IP address';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _dataController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Data',
|
||||
prefixIcon: Icon(Icons.data_object),
|
||||
hintText: 'Enter the data to send',
|
||||
),
|
||||
maxLines: 4,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter data to send';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: _save,
|
||||
child: const Text('Save'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user