250 lines
9.2 KiB
Dart
250 lines
9.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_map/flutter_map.dart';
|
|
import 'package:latlong2/latlong.dart';
|
|
import 'package:u_vineyard/services/polygon.area.calculator.service.dart';
|
|
import 'package:u_vineyard/template/src/widgets/card.widget.dart';
|
|
import 'package:u_vineyard/template/src/widgets/responsive_widgets/repesponsive_grid_item.widget.dart';
|
|
import 'package:u_vineyard/template/src/widgets/responsive_widgets/responsive_grid.widget.dart';
|
|
import 'package:u_vineyard/widgets/map.widget.dart';
|
|
|
|
class NewVineyardScreen extends StatefulWidget {
|
|
const NewVineyardScreen({super.key});
|
|
|
|
@override
|
|
State<NewVineyardScreen> createState() => _NewVineyardScreenState(); // Correzione: nome della classe State
|
|
}
|
|
|
|
class _NewVineyardScreenState extends State<NewVineyardScreen> { // Correzione: nome della classe State
|
|
// Aggiungi un TextEditingController per il TextFormField
|
|
final TextEditingController _nomeVignaController = TextEditingController();
|
|
final TextEditingController _superficieController = TextEditingController();
|
|
|
|
List<LatLng> _polygonPoints = [];
|
|
double _calculatedArea = 0.0; // Area calcolata, inizialmente impostata a 0
|
|
final TextEditingController _localitaController = TextEditingController();
|
|
@override
|
|
void dispose() {
|
|
_nomeVignaController.dispose(); // Libera il controller quando il widget viene eliminato
|
|
_superficieController.dispose(); // Libera il controller quando il widget viene eliminato
|
|
_localitaController.dispose(); // Libera il controller quando il widget viene eliminato
|
|
super.dispose();
|
|
}
|
|
|
|
void _handleMapTap(TapPosition tapPosition, LatLng latLng) {
|
|
setState(() {
|
|
_polygonPoints.add(latLng);
|
|
_calculatedArea = PolygonAreaCalculator.calculateAreaInHectares(_polygonPoints);
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Punto aggiunto: ${latLng.latitude.toStringAsFixed(4)}, ${latLng.longitude.toStringAsFixed(4)}'),
|
|
duration: const Duration(milliseconds: 800),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _clearPolygon() {
|
|
setState(() {
|
|
_polygonPoints.clear();
|
|
_calculatedArea = 0.0;
|
|
_superficieController.clear();
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Poligono e superficie resettati.'),
|
|
duration: Duration(milliseconds: 800),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showAreaConfirmationDialog() {
|
|
if (_calculatedArea > 0) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: const Text('Conferma Superficie'),
|
|
content: Text(
|
|
'L\'area calcolata del poligono è di ${_calculatedArea.toStringAsFixed(2)} ettari.\n\nVuoi utilizzare questo valore o inserirlo manualmente?'),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: const Text('Inserisci Manualmente'),
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
_superficieController.clear();
|
|
},
|
|
),
|
|
FilledButton(
|
|
child: const Text('Usa Valore Calcolato'),
|
|
onPressed: () {
|
|
setState(() {
|
|
_superficieController.text = _calculatedArea.toStringAsFixed(2);
|
|
});
|
|
Navigator.of(context).pop();
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
void _submitForm() {
|
|
//if (_formKey.currentState!.validate()) {
|
|
if (true) {
|
|
if (_polygonPoints.length < 3) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Disegna almeno 3 punti per il poligono della vigna.')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
print('--- Dati Vigna ---');
|
|
print('Nome Vigna: ${_nomeVignaController.text}');
|
|
print('Località: ${_localitaController.text}');
|
|
print('Superficie (ha): ${_superficieController.text}');
|
|
print('Coordinate Poligono:');
|
|
_polygonPoints.forEach((point) {
|
|
print(' Lat: ${point.latitude}, Lng: ${point.longitude}');
|
|
});
|
|
print('-----------------');
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Dati inviati (simulato)! Controlla la console.')),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ResponsiveGrid(
|
|
childrenBuilder: (context, screen) {
|
|
return [
|
|
ResponsiveGridItem(
|
|
crossAxisCellCount: screen.setAxis(12, 12, 12, 12),
|
|
child: Container(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
CardWidget(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Text(
|
|
'Aggiungi Nuova Vigna',
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16.0), // Spazio dopo il CardWidget
|
|
// *** INIZIO DELLA CORREZIONE ***
|
|
Row(
|
|
children: [
|
|
Expanded( // <--- AVVOLGI IL TextFormField IN UN Expanded
|
|
child: TextFormField(
|
|
controller: _nomeVignaController, // Collega il controller
|
|
decoration: const InputDecoration(
|
|
labelText: 'Nome Vigna',
|
|
border: OutlineInputBorder(),
|
|
prefixIcon: Icon(Icons.grass),
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Inserisci il nome della vigna';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _localitaController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Località',
|
|
border: OutlineInputBorder(),
|
|
prefixIcon: Icon(Icons.location_on),
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Inserisci la località';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
//controller: superficieController,
|
|
keyboardType: TextInputType.number,
|
|
decoration: InputDecoration(
|
|
labelText: 'Superficie (ettari)',
|
|
border: const OutlineInputBorder(),
|
|
prefixIcon: const Icon(Icons.area_chart),
|
|
suffixIcon: _calculatedArea > 0
|
|
? IconButton(
|
|
icon: const Icon(Icons.auto_mode, color: Colors.green),
|
|
onPressed: _showAreaConfirmationDialog, // Mostra il dialogo di conferma
|
|
tooltip: 'Usa area calcolata',
|
|
)
|
|
: null,
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Inserisci la superficie';
|
|
}
|
|
if (double.tryParse(value) == null) {
|
|
return 'Inserisci un numero valido';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
// *** FINE DELLA CORREZIONE ***
|
|
// Add your form or content here
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: VignaMapWidget(
|
|
polygonPoints: _polygonPoints,
|
|
onMapTap: _handleMapTap,
|
|
onClearPolygon: _clearPolygon,
|
|
calculatedArea: _calculatedArea,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: SizedBox(
|
|
width: double.infinity,
|
|
child: ElevatedButton.icon(
|
|
onPressed: _submitForm,
|
|
icon: const Icon(Icons.send),
|
|
label: const Text(
|
|
'Invia Dati Vigna',
|
|
style: TextStyle(fontSize: 18),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.green[800],
|
|
foregroundColor: Colors.white,
|
|
padding: const EdgeInsets.symmetric(vertical: 15),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
];
|
|
},
|
|
);
|
|
}
|
|
} |