mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-12 00:40:28 +00:00
feat: Add drawing functionality with line and rectangle support, including toolbar and layer integration
This commit is contained in:
160
lib/widgets/map/drawing_layer.dart
Normal file
160
lib/widgets/map/drawing_layer.dart
Normal file
@@ -0,0 +1,160 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import '../../models/map_drawing.dart';
|
||||
|
||||
/// Widget that renders map drawings as polylines
|
||||
class DrawingLayer extends StatelessWidget {
|
||||
final List<MapDrawing> drawings;
|
||||
final MapDrawing? previewDrawing;
|
||||
|
||||
const DrawingLayer({
|
||||
super.key,
|
||||
required this.drawings,
|
||||
this.previewDrawing,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final List<Polyline> polylines = [];
|
||||
|
||||
// Add completed drawings
|
||||
for (final drawing in drawings) {
|
||||
polylines.add(_createPolyline(drawing, isPreview: false));
|
||||
}
|
||||
|
||||
// Add preview drawing (if any)
|
||||
if (previewDrawing != null) {
|
||||
polylines.add(_createPolyline(previewDrawing!, isPreview: true));
|
||||
}
|
||||
|
||||
return PolylineLayer(polylines: polylines);
|
||||
}
|
||||
|
||||
/// Create a polyline from a drawing
|
||||
Polyline _createPolyline(MapDrawing drawing, {required bool isPreview}) {
|
||||
final points = _getPoints(drawing);
|
||||
final opacity = isPreview ? 0.6 : 1.0;
|
||||
|
||||
return Polyline(
|
||||
points: points,
|
||||
color: drawing.color.withOpacity(opacity),
|
||||
strokeWidth: 4.0,
|
||||
borderColor: Colors.white.withOpacity(opacity * 0.8),
|
||||
borderStrokeWidth: 1.0,
|
||||
);
|
||||
}
|
||||
|
||||
/// Get points from a drawing based on its type
|
||||
List<LatLng> _getPoints(MapDrawing drawing) {
|
||||
if (drawing is LineDrawing) {
|
||||
return drawing.points;
|
||||
} else if (drawing is RectangleDrawing) {
|
||||
return drawing.corners;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// Widget that shows drawing markers (start/end points)
|
||||
class DrawingMarkersLayer extends StatelessWidget {
|
||||
final List<MapDrawing> drawings;
|
||||
final Function(String drawingId)? onDeleteDrawing;
|
||||
|
||||
const DrawingMarkersLayer({
|
||||
super.key,
|
||||
required this.drawings,
|
||||
this.onDeleteDrawing,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final List<Marker> markers = [];
|
||||
|
||||
// Add delete markers for each drawing (at the center point)
|
||||
for (final drawing in drawings) {
|
||||
final centerPoint = _getCenterPoint(drawing);
|
||||
if (centerPoint != null) {
|
||||
markers.add(
|
||||
Marker(
|
||||
point: centerPoint,
|
||||
width: 40,
|
||||
height: 40,
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
if (onDeleteDrawing != null) {
|
||||
_showDeleteDialog(context, drawing);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: drawing.color.withOpacity(0.9),
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: Colors.white, width: 2),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.3),
|
||||
blurRadius: 4,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.close,
|
||||
color: Colors.white,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return MarkerLayer(markers: markers);
|
||||
}
|
||||
|
||||
/// Get the center point of a drawing
|
||||
LatLng? _getCenterPoint(MapDrawing drawing) {
|
||||
if (drawing is LineDrawing && drawing.points.isNotEmpty) {
|
||||
// Use the middle point of the line
|
||||
final midIndex = drawing.points.length ~/ 2;
|
||||
return drawing.points[midIndex];
|
||||
} else if (drawing is RectangleDrawing) {
|
||||
// Use the center of the rectangle
|
||||
return LatLng(
|
||||
(drawing.topLeft.latitude + drawing.bottomRight.latitude) / 2,
|
||||
(drawing.topLeft.longitude + drawing.bottomRight.longitude) / 2,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Show delete confirmation dialog
|
||||
void _showDeleteDialog(BuildContext context, MapDrawing drawing) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Delete Drawing'),
|
||||
content: Text(
|
||||
'Delete this ${drawing.type.name}?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
onDeleteDrawing?.call(drawing.id);
|
||||
},
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: Colors.red,
|
||||
),
|
||||
child: const Text('Delete'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
281
lib/widgets/map/drawing_toolbar.dart
Normal file
281
lib/widgets/map/drawing_toolbar.dart
Normal file
@@ -0,0 +1,281 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/drawing_provider.dart';
|
||||
import '../../models/map_drawing.dart';
|
||||
|
||||
/// Toolbar for drawing controls on the map
|
||||
class DrawingToolbar extends StatelessWidget {
|
||||
const DrawingToolbar({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<DrawingProvider>(
|
||||
builder: (context, drawingProvider, _) {
|
||||
if (!drawingProvider.isDrawing) {
|
||||
// Show compact floating button when not in drawing mode
|
||||
return FloatingActionButton.small(
|
||||
heroTag: 'drawing_tool',
|
||||
onPressed: () => _showDrawingMenu(context, drawingProvider),
|
||||
child: const Icon(Icons.edit),
|
||||
);
|
||||
}
|
||||
|
||||
// Show full toolbar when in drawing mode
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.2),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Title bar with close button
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
_getIconForMode(drawingProvider.drawingMode),
|
||||
size: 20,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
_getTitleForMode(drawingProvider.drawingMode),
|
||||
style: Theme.of(context).textTheme.titleSmall?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close, size: 20),
|
||||
onPressed: () => drawingProvider.exitDrawingMode(),
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
// Color picker
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
children: DrawingColors.palette.map((color) {
|
||||
final isSelected = drawingProvider.selectedColor == color;
|
||||
return GestureDetector(
|
||||
onTap: () => drawingProvider.setColor(color),
|
||||
child: Container(
|
||||
width: 32,
|
||||
height: 32,
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(
|
||||
color: isSelected ? Colors.white : Colors.grey.shade300,
|
||||
width: isSelected ? 3 : 2,
|
||||
),
|
||||
boxShadow: [
|
||||
if (isSelected)
|
||||
BoxShadow(
|
||||
color: color.withOpacity(0.5),
|
||||
blurRadius: 8,
|
||||
spreadRadius: 2,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: isSelected
|
||||
? const Icon(
|
||||
Icons.check,
|
||||
color: Colors.white,
|
||||
size: 16,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
// Action buttons
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Cancel current drawing
|
||||
if (drawingProvider.currentLinePoints.isNotEmpty ||
|
||||
drawingProvider.rectangleStartPoint != null)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.undo),
|
||||
onPressed: () => drawingProvider.cancelCurrentDrawing(),
|
||||
tooltip: 'Cancel',
|
||||
),
|
||||
// Complete line drawing
|
||||
if (drawingProvider.drawingMode == DrawingMode.line &&
|
||||
drawingProvider.currentLinePoints.length >= 2)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.check),
|
||||
onPressed: () => drawingProvider.completeLine(),
|
||||
tooltip: 'Complete Line',
|
||||
color: Colors.green,
|
||||
),
|
||||
// Clear all drawings
|
||||
if (drawingProvider.drawings.isNotEmpty)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_sweep),
|
||||
onPressed: () => _showClearAllDialog(context, drawingProvider),
|
||||
tooltip: 'Clear All',
|
||||
color: Colors.red,
|
||||
),
|
||||
],
|
||||
),
|
||||
// Instructions
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
_getInstructions(drawingProvider.drawingMode),
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Show drawing mode selection menu
|
||||
void _showDrawingMenu(BuildContext context, DrawingProvider drawingProvider) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.edit),
|
||||
const SizedBox(width: 12),
|
||||
Text(
|
||||
'Drawing Tools',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Divider(),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.show_chart),
|
||||
title: const Text('Draw Line'),
|
||||
subtitle: const Text('Draw a freehand line on the map'),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
drawingProvider.setDrawingMode(DrawingMode.line);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.crop_square),
|
||||
title: const Text('Draw Rectangle'),
|
||||
subtitle: const Text('Draw a rectangular area on the map'),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
drawingProvider.setDrawingMode(DrawingMode.rectangle);
|
||||
},
|
||||
),
|
||||
if (drawingProvider.drawings.isNotEmpty) ...[
|
||||
const Divider(),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.delete_sweep, color: Colors.red),
|
||||
title: const Text('Clear All Drawings'),
|
||||
subtitle: Text('Remove all ${drawingProvider.drawings.length} drawings'),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
_showClearAllDialog(context, drawingProvider);
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Show clear all confirmation dialog
|
||||
void _showClearAllDialog(BuildContext context, DrawingProvider drawingProvider) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Clear All Drawings'),
|
||||
content: Text(
|
||||
'Delete all ${drawingProvider.drawings.length} drawings from the map?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
drawingProvider.clearAllDrawings();
|
||||
},
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: Colors.red,
|
||||
),
|
||||
child: const Text('Clear All'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Get icon for drawing mode
|
||||
IconData _getIconForMode(DrawingMode mode) {
|
||||
switch (mode) {
|
||||
case DrawingMode.line:
|
||||
return Icons.show_chart;
|
||||
case DrawingMode.rectangle:
|
||||
return Icons.crop_square;
|
||||
case DrawingMode.none:
|
||||
return Icons.edit;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get title for drawing mode
|
||||
String _getTitleForMode(DrawingMode mode) {
|
||||
switch (mode) {
|
||||
case DrawingMode.line:
|
||||
return 'Draw Line';
|
||||
case DrawingMode.rectangle:
|
||||
return 'Draw Rectangle';
|
||||
case DrawingMode.none:
|
||||
return 'Drawing';
|
||||
}
|
||||
}
|
||||
|
||||
/// Get instructions for drawing mode
|
||||
String _getInstructions(DrawingMode mode) {
|
||||
switch (mode) {
|
||||
case DrawingMode.line:
|
||||
return 'Tap map to add points\nTap ✓ to finish';
|
||||
case DrawingMode.rectangle:
|
||||
return 'Tap start point, then end point';
|
||||
case DrawingMode.none:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user