Snap geometry edits

View on GitHub

Use the Geometry Editor to edit a geometry and align it to existing geometries on a map.

Image of snap geometry edits

Use case

A field worker can create new features by editing and snapping the vertices of a geometry to existing features on a map. In a water distribution network, service line features can be represented with the polyline geometry type. By snapping the vertices of a proposed service line to existing features in the network, an exact footprint can be identified to show the path of the service line and what features in the network it connects to. The feature layer containing the service lines can then be accurately modified to include the proposed line.

How to use the sample

To create a geometry, choose the geometry type you want to create (i.e. points, multipoints, polyline, or polygon) and interactively tap and drag on the map view to create the geometry.

Snap settings can be configured by enabling and disabling snapping, feature snapping, geometry guides and snap sources.

To interactively snap a vertex to a feature or graphic, ensure that snapping is enabled for the relevant snap source and drag a vertex to nearby an existing feature or graphic. When the vertex is close to that existing geoelement, the edit position will be adjusted to coincide with (or snap to), edges and vertices of its geometry. Release to place the vertex at the snapped location.

To edit a geometry, tap the geometry to be edited in the map to select it and then edit the geometry by tapping and dragging its vertices and snapping them to nearby features or graphics.

To undo changes made to the geometry, tap the undo button.

To delete a geometry or a vertex, tap the geometry or vertex to select it and then tap the delete button.

To save your edits, tap the save button.

How it works

  1. Create an ArcGISMap from the URL and connect it to the ArcGISMapView via the ArcGISMapViewController.
  2. Set the map's loadSettings.featureTilingMode to enabledWithFullResolutionWhenSupported.
  3. Create a GeometryEditor and connect it to the map view controller.
  4. Call syncSourceSettings() after the map's operational layers are loaded and the geometry editor has connected.
  5. Set snapSettings.isEnabled and snapSourceSettings.isEnabled to true for the SnapSource of interest.
  6. Toggle geometry guides using snapSettings.isGeometryGuidesEnabled and feature snapping using snapSettings.isFeatureSnappingEnabled.
  7. Start the geometry editor with a GeometryType.

Relevant API

  • ArcGISMapView
  • FeatureLayer
  • Geometry
  • GeometryEditor
  • GeometryEditorStyle
  • GraphicsOverlay
  • SnapSettings
  • SnapSource
  • SnapSourceSettings

About the data

The Naperville water distribution network is based on ArcGIS Solutions for Water Utilities and provides a realistic depiction of a theoretical stormwater network.

Additional information

Snapping is used to maintain data integrity between different sources of data when editing, so it is important that each SnapSource provides full resolution geometries to be valid for snapping. This means that some of the default optimizations used to improve the efficiency of data transfer and display of polygon and polyline layers based on feature services are not appropriate for use with snapping.

To snap to polygon and polyline layers, the recommended approach is to set the FeatureLayer's feature tiling mode to FeatureTilingMode.enabledWithFullResolutionWhenSupported and use the default ServiceFeatureTable feature request mode FeatureRequestMode.onInteractionCache. Local data sources, such as geodatabases, always provide full resolution geometries. Point and multipoint feature layers are also always full resolution.

Snapping can be used during interactive edits that move existing vertices using the VertexTool or ReticleVertexTool. Using the ReticleVertexTool to add and move vertices allows users of touch screen devices to clearly see the visual cues for snapping.

Geometry guides are enabled by default when snapping is enabled. These allow for snapping to a point coinciding with, parallel to, perpendicular to or extending an existing geometry.

On supported platforms haptic feedback on SnapState.snappedToFeature and SnapState.snappedToGeometryGuide is enabled by default when snapping is enabled. Custom haptic feedback can be configured by setting SnapSettings.isHapticFeedbackEnabled to false and listening to GeometryEditor.onSnapChanged events to provide specific feedback depending on the SnapState.

Tags

edit, feature, geometry editor, graphics, layers, map, snapping

Sample Code

snap_geometry_edits.dart
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// Copyright 2024 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import 'dart:math';
import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/utils/sample_state_support.dart';
import 'package:flutter/material.dart';

class SnapGeometryEdits extends StatefulWidget {
  const SnapGeometryEdits({super.key});

  @override
  State<SnapGeometryEdits> createState() => _SnapGeometryEditsState();
}

class _SnapGeometryEditsState extends State<SnapGeometryEdits>
    with SampleStateSupport {
  // Create a controller for the map view.
  final _mapViewController = ArcGISMapView.createController();
  // Create a graphics overlay.
  final _graphicsOverlay = GraphicsOverlay();
  // Create a geometry editor.
  final _geometryEditor = GeometryEditor();
  // Create a geometry editor style for accessing symbol styles.
  final _geometryEditorStyle = GeometryEditorStyle();
  // A flag for when the map view is ready and controls can be used.
  var _ready = false;

  // Create a list of menu items for each geometry type.
  final _geometryTypeMenuItems = <DropdownMenuItem<GeometryType>>[];

  // Create a selection of tools to make available to the geometry editor.
  final _vertexTool = VertexTool();
  final _reticleVertexTool = ReticleVertexTool();
  final _toolMenuItems = <DropdownMenuItem<GeometryEditorTool>>[];

  // Create lists to hold different types of snap source settings to make available to the geometry editor.
  final _pointLayerSnapSources = <SnapSourceSettings>[];
  final _polylineLayerSnapSources = <SnapSourceSettings>[];
  final _graphicsOverlaySnapSources = <SnapSourceSettings>[];

  // Create variables for holding state relating to the geometry editor for controlling the UI.
  GeometryType? _selectedGeometryType;
  GeometryEditorTool? _selectedTool;
  Graphic? _selectedGraphic;
  // Initial values are based on defaults.
  var _geometryEditorCanUndo = false;
  var _geometryEditorIsStarted = false;
  var _geometryEditorHasSelectedElement = false;
  var _snappingEnabled = false;
  var _geometryGuidesEnabled = false;
  var _featureSnappingEnabled = true;

  // A flag for controlling the visibility of the editing toolbar.
  var _showEditToolbar = true;
  // A flag for controlling the visibility of the snap settings.
  var _snapSettingsVisible = false;

  // A custom style for when the editing toolbar buttons are not enabled.
  final _buttonStyle = ElevatedButton.styleFrom(
    disabledBackgroundColor: Colors.white.withOpacity(0.6),
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: false,
        child: Stack(
          children: [
            Column(
              children: [
                Expanded(
                  // Add a map view to the widget tree and set a controller.
                  child: ArcGISMapView(
                    controllerProvider: () => _mapViewController,
                    onMapViewReady: onMapViewReady,
                    // Only select existing graphics to edit if the geometry editor is not started
                    // i.e. editing is not already in progress.
                    onTap: !_geometryEditorIsStarted ? onTap : null,
                  ),
                ),
                // Build the bottom menu.
                buildBottomMenu(),
              ],
            ),
            Visibility(
              visible: _showEditToolbar,
              // Build the editing toolbar.
              child: buildEditingToolbar(),
            ),
            // Display a progress indicator and prevent interaction until state is ready.
            Visibility(
              visible: !_ready,
              child: const SizedBox.expand(
                child: ColoredBox(
                  color: Colors.white30,
                  child: Center(child: CircularProgressIndicator()),
                ),
              ),
            ),
          ],
        ),
      ),
      // The snap settings bottom sheet.
      bottomSheet: _snapSettingsVisible ? buildSnapSettings(context) : null,
    );
  }

  void onMapViewReady() async {
    // Create a map with a URL to a web map.
    const webMapUri =
        'https://www.arcgis.com/home/item.html?id=b95fe18073bc4f7788f0375af2bb445e';
    final map = ArcGISMap.withUri(Uri.parse(webMapUri));
    if (map != null) {
      // Set the feature tiling mode on the map.
      // Snapping is used to maintain data integrity between different sources of data when editing,
      // so full resolution is needed for valid snapping.
      map.loadSettings.featureTilingMode =
          FeatureTilingMode.enabledWithFullResolutionWhenSupported;

      // Set the map to the map view controller.
      _mapViewController.arcGISMap = map;

      // Add the graphics overlay to the map view.
      _mapViewController.graphicsOverlays.add(_graphicsOverlay);

      // Do some initial configuration of the geometry editor.
      // Initially set the created reticle vertex tool as the current tool.
      // Note that the reticle vertex tool makes visibility of snapping easier on touchscreen devices.
      setState(() => _selectedTool = _reticleVertexTool);
      _geometryEditor.tool = _reticleVertexTool;
      // Listen to changes in canUndo in order to enable/disable the UI.
      _geometryEditor.onCanUndoChanged.listen(
        (canUndo) => setState(() => _geometryEditorCanUndo = canUndo),
      );
      // Listen to changes in isStarted in order to enable/disable the UI.
      _geometryEditor.onIsStartedChanged.listen(
        (isStarted) => setState(() => _geometryEditorIsStarted = isStarted),
      );
      // Listen to changes in the selected element in order to enable/disable the UI.
      _geometryEditor.onSelectedElementChanged.listen(
        (selectedElement) => setState(
          () => _geometryEditorHasSelectedElement = selectedElement != null,
        ),
      );

      // Set the geometry editor to the map view controller.
      _mapViewController.geometryEditor = _geometryEditor;

      // Ensure the map and each layer loads in order to synchronize snap settings.
      await map.load();
      await Future.wait(map.operationalLayers.map((layer) => layer.load()));

      // Sync snap settings.
      synchronizeSnapSettings();

      // Configure menu items for selecting tools and geometry types.
      _toolMenuItems.addAll(configureToolMenuItems());
      _geometryTypeMenuItems.addAll(configureGeometryTypeMenuItems());

      // Set the ready state variable to true to enable the sample UI.
      setState(() => _ready = true);
    }
  }

  void onTap(Offset localPosition) async {
    // Perform an identify operation on the graphics overlay at the tapped location.
    final identifyResult = await _mapViewController.identifyGraphicsOverlay(
      _graphicsOverlay,
      screenPoint: localPosition,
      tolerance: 12.0,
    );

    // Get the graphics from the identify result.
    final graphics = identifyResult.graphics;
    if (graphics.isNotEmpty) {
      final graphic = graphics.first;
      if (graphic.geometry != null) {
        final geometry = graphic.geometry!;
        // Hide the selected graphic so that only the version of the graphic that is being edited is visible.
        graphic.isVisible = false;
        // Set the graphic as the selected graphic and also set the selected geometry type to update the UI.
        _selectedGraphic = graphic;
        setState(() => _selectedGeometryType = geometry.geometryType);
        // Start the geometry editor using the geometry of the graphic.
        _geometryEditor.startWithGeometry(geometry);
      }
    }
  }

  void synchronizeSnapSettings() {
    // Synchronize the snap source collection with the map's operational layers.
    _geometryEditor.snapSettings.syncSourceSettings();
    // Enable snapping on the geometry editor.
    _geometryEditor.snapSettings.isEnabled = true;
    setState(() => _snappingEnabled = true);
    // Enable geometry guides on the geometry editor.
    _geometryEditor.snapSettings.isGeometryGuidesEnabled = true;
    setState(() => _geometryGuidesEnabled = true);
    // Create a list of snap source settings for each geometry type and graphics overlay.
    for (final sourceSettings in _geometryEditor.snapSettings.sourceSettings) {
      // Enable all the source settings initially.
      setState(() => sourceSettings.isEnabled = true);
      if (sourceSettings.source is FeatureLayer) {
        final featureLayer = sourceSettings.source as FeatureLayer;
        if (featureLayer.featureTable != null) {
          final geometryType = featureLayer.featureTable!.geometryType;
          if (geometryType == GeometryType.point) {
            _pointLayerSnapSources.add(sourceSettings);
          } else if (geometryType == GeometryType.polyline) {
            _polylineLayerSnapSources.add(sourceSettings);
          }
        }
      } else if (sourceSettings.source is GraphicsOverlay) {
        _graphicsOverlaySnapSources.add(sourceSettings);
      }
    }
  }

  void startEditingWithGeometryType(GeometryType geometryType) {
    // Set the selected geometry type and start editing.
    setState(() => _selectedGeometryType = geometryType);
    _geometryEditor.startWithGeometryType(geometryType);
  }

  void stopAndSave() {
    // Get the geometry from the geometry editor.
    final geometry = _geometryEditor.stop();

    if (geometry != null) {
      if (_selectedGraphic != null) {
        // If there was a selected graphic being edited, update it.
        _selectedGraphic!.geometry = geometry;
        _selectedGraphic!.isVisible = true;
        // Reset the selected graphic to null.
        _selectedGraphic = null;
      } else {
        // If there was no existing graphic, create a new one and add to the graphics overlay.
        final graphic = Graphic(geometry: geometry);
        // Apply a symbol to the graphic from the geometry editor style depending on the geometry type.
        final geometryType = geometry.geometryType;
        if (geometryType == GeometryType.point ||
            geometryType == GeometryType.multipoint) {
          graphic.symbol = _geometryEditorStyle.vertexSymbol;
        } else if (geometryType == GeometryType.polyline) {
          graphic.symbol = _geometryEditorStyle.lineSymbol;
        } else if (geometryType == GeometryType.polygon) {
          graphic.symbol = _geometryEditorStyle.fillSymbol;
        }
        _graphicsOverlay.graphics.add(graphic);
      }
    }

    // Reset the selected geometry type to null.
    setState(() => _selectedGeometryType = null);
  }

  void stopAndDiscardEdits() {
    // Stop the geometry editor. No need to capture the geometry as we are discarding.
    _geometryEditor.stop();
    if (_selectedGraphic != null) {
      // If editing a previously existing geometry, reset the selectedGraphic.
      _selectedGraphic!.isVisible = true;
      _selectedGraphic = null;
    }
    // Reset the selected geometry type.
    setState(() => _selectedGeometryType = null);
  }

  Widget buildBottomMenu() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        // A drop down button for selecting geometry type.
        DropdownButton(
          alignment: Alignment.center,
          hint: const Text(
            'Geometry Type',
            style: TextStyle(
              color: Colors.deepPurple,
            ),
          ),
          icon: const Icon(Icons.arrow_drop_down),
          iconEnabledColor: Colors.deepPurple,
          iconDisabledColor: Colors.grey,
          style: const TextStyle(color: Colors.deepPurple),
          value: _selectedGeometryType,
          items: _geometryTypeMenuItems,
          // If the geometry editor is already started then we fully disable the DropDownButton and prevent editing with another geometry type.
          onChanged: !_geometryEditorIsStarted
              ? (GeometryType? geometryType) {
                  if (geometryType != null) {
                    startEditingWithGeometryType(geometryType);
                  }
                }
              : null,
        ),
        // A drop down button for selecting a tool.
        DropdownButton(
          alignment: Alignment.center,
          hint: const Text(
            'Tool',
            style: TextStyle(color: Colors.deepPurple),
          ),
          iconEnabledColor: Colors.deepPurple,
          style: const TextStyle(color: Colors.deepPurple),
          value: _selectedTool,
          items: _toolMenuItems,
          onChanged: (tool) {
            if (tool != null) {
              setState(() => _selectedTool = tool);
              _geometryEditor.tool = tool;
            }
          },
        ),
        // A button to toggle the visibility of the editing toolbar.
        IconButton(
          onPressed: () => setState(() => _showEditToolbar = !_showEditToolbar),
          icon: const Icon(Icons.edit, color: Colors.deepPurple),
        ),
      ],
    );
  }

  Widget buildEditingToolbar() {
    // A toolbar of buttons with icons for editing functions. Tooltips are used to aid the user experience.
    return Padding(
      padding: const EdgeInsets.only(bottom: 100, right: 5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // A button to toggle the visibility of the snap settings.
              ElevatedButton(
                style: _buttonStyle,
                onPressed: () => setState(() => _snapSettingsVisible = true),
                child: const Text('Show snap settings'),
              ),
              Row(
                children: [
                  // A button to call undo on the geometry editor, if enabled.
                  Tooltip(
                    message: 'Undo',
                    child: ElevatedButton(
                      style: _buttonStyle,
                      onPressed:
                          _geometryEditorIsStarted && _geometryEditorCanUndo
                              ? () => _geometryEditor.undo()
                              : null,
                      child: const Icon(Icons.undo),
                    ),
                  ),
                  const SizedBox(width: 12),
                  // A button to delete the selected element on the geometry editor.
                  Tooltip(
                    message: 'Delete selected element',
                    child: ElevatedButton(
                      style: _buttonStyle,
                      onPressed: _geometryEditorIsStarted &&
                              _geometryEditorHasSelectedElement &&
                              _geometryEditor.selectedElement != null &&
                              _geometryEditor.selectedElement!.canDelete
                          ? () => _geometryEditor.deleteSelectedElement()
                          : null,
                      child: const Icon(Icons.clear),
                    ),
                  ),
                ],
              ),
              Row(
                children: [
                  // A button to stop and save edits.
                  Tooltip(
                    message: 'Stop and save edits',
                    child: ElevatedButton(
                      style: _buttonStyle,
                      onPressed: _geometryEditorIsStarted ? stopAndSave : null,
                      child: const Icon(Icons.save),
                    ),
                  ),
                  const SizedBox(width: 12),
                  // A button to stop the geometry editor and discard all edits.
                  Tooltip(
                    message: 'Stop and discard edits',
                    child: ElevatedButton(
                      style: _buttonStyle,
                      onPressed:
                          _geometryEditorIsStarted ? stopAndDiscardEdits : null,
                      child: const Icon(Icons.not_interested_sharp),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget buildSnapSettings(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(
        20.0,
        0.0,
        20.0,
        max(
          20.0,
          View.of(context).viewPadding.bottom /
              View.of(context).devicePixelRatio,
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              Text(
                'Settings',
                style: Theme.of(context).textTheme.titleLarge,
              ),
              const Spacer(),
              IconButton(
                icon: const Icon(Icons.close),
                onPressed: () => setState(() => _snapSettingsVisible = false),
              ),
            ],
          ),
          Container(
            constraints: BoxConstraints(
              maxHeight: MediaQuery.sizeOf(context).height * 0.4,
              maxWidth: MediaQuery.sizeOf(context).height * 0.8,
            ),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        'Snap Settings',
                        style: Theme.of(context).textTheme.titleMedium,
                      ),
                      // Add a checkbox to toggle all snapping options.
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          const Text('Enable all'),
                          Checkbox(
                            value: _snappingEnabled &&
                                _geometryGuidesEnabled &&
                                _featureSnappingEnabled,
                            onChanged: (allEnabled) {
                              if (allEnabled != null) {
                                _geometryEditor.snapSettings.isEnabled =
                                    allEnabled;
                                _geometryEditor.snapSettings
                                    .isGeometryGuidesEnabled = allEnabled;
                                _geometryEditor.snapSettings
                                    .isFeatureSnappingEnabled = allEnabled;
                                setState(() {
                                  _snappingEnabled = allEnabled;
                                  _geometryGuidesEnabled = allEnabled;
                                  _featureSnappingEnabled = allEnabled;
                                });
                              }
                            },
                          ),
                        ],
                      ),
                    ],
                  ),
                  // Add a checkbox to toggle whether snapping is enabled.
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Snapping enabled'),
                      Checkbox(
                        value: _snappingEnabled,
                        onChanged: (snappingEnabled) {
                          if (snappingEnabled != null) {
                            _geometryEditor.snapSettings.isEnabled =
                                snappingEnabled;
                            setState(() => _snappingEnabled = snappingEnabled);
                          }
                        },
                      ),
                    ],
                  ),
                  // Add a checkbox to toggle whether geometry guides are enabled.
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Geometry guides'),
                      Checkbox(
                        value: _geometryGuidesEnabled,
                        onChanged: (geometryGuidesEnabled) {
                          if (geometryGuidesEnabled != null) {
                            _geometryEditor
                                    .snapSettings.isGeometryGuidesEnabled =
                                geometryGuidesEnabled;
                            setState(
                              () => _geometryGuidesEnabled =
                                  geometryGuidesEnabled,
                            );
                          }
                        },
                      ),
                    ],
                  ),
                  // Add a checkbox to toggle whether feature snapping is enabled.
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Feature snapping'),
                      Checkbox(
                        value: _featureSnappingEnabled,
                        onChanged: (featureSnappingEnabled) {
                          if (featureSnappingEnabled != null) {
                            _geometryEditor
                                    .snapSettings.isFeatureSnappingEnabled =
                                featureSnappingEnabled;
                            setState(
                              () => _featureSnappingEnabled =
                                  featureSnappingEnabled,
                            );
                          }
                        },
                      ),
                    ],
                  ),
                  const SizedBox(height: 20),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Text(
                        'Select snap sources',
                        style: Theme.of(context).textTheme.titleLarge,
                      ),
                    ],
                  ),
                  const SizedBox(height: 20),
                  // Add checkboxes for enabling the point layers as snap sources.
                  buildSnapSourcesSelection(
                    'Point layers',
                    _pointLayerSnapSources,
                  ),
                  // Add checkboxes for the polyline layers as snap sources.
                  buildSnapSourcesSelection(
                    'Polyline layers',
                    _polylineLayerSnapSources,
                  ),
                  // Add checkboxes for the graphics overlay as snap sources.
                  buildSnapSourcesSelection(
                    'Graphics Overlay',
                    _graphicsOverlaySnapSources,
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget buildSnapSourcesSelection(
    String label,
    List<SnapSourceSettings> allSourceSettings,
  ) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              label,
              style: Theme.of(context).textTheme.titleMedium,
            ),
            Row(
              children: [
                const Text('Enable all'),
                // A checkbox to enable all source settings in the category.
                Checkbox(
                  value: allSourceSettings.every(
                    (snapSourceSettings) => snapSourceSettings.isEnabled,
                  ),
                  onChanged: (allEnabled) {
                    if (allEnabled != null) {
                      allSourceSettings
                          .map(
                            (snapSourceSettings) => setState(
                              () => snapSourceSettings.isEnabled = allEnabled,
                            ),
                          )
                          .toList();
                    }
                  },
                ),
              ],
            ),
          ],
        ),
        Column(
          children: allSourceSettings.map((sourceSetting) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                // Display the layer name, or set default text for graphics overlay.
                Text(
                  allSourceSettings == _pointLayerSnapSources ||
                          allSourceSettings == _polylineLayerSnapSources
                      ? (sourceSetting.source as FeatureLayer).name
                      : 'Editor Graphics Overlay',
                ),
                // A checkbox to toggle whether this source setting is enabled.
                Checkbox(
                  value: sourceSetting.isEnabled,
                  onChanged: (isEnabled) {
                    if (isEnabled != null) {
                      setState(() => sourceSetting.isEnabled = isEnabled);
                    }
                  },
                ),
              ],
            );
          }).toList(),
        ),
        const SizedBox(height: 20),
      ],
    );
  }

  List<DropdownMenuItem<GeometryType>> configureGeometryTypeMenuItems() {
    // Create a list of geometry types to make available for editing.
    final geometryTypes = [
      GeometryType.point,
      GeometryType.multipoint,
      GeometryType.polyline,
      GeometryType.polygon,
    ];
    // Returns a list of drop down menu items for each geometry type.
    return geometryTypes
        .map(
          (type) => DropdownMenuItem(
            value: type,
            child: Text(type.name.capitalize()),
          ),
        )
        .toList();
  }

  List<DropdownMenuItem<GeometryEditorTool>> configureToolMenuItems() {
    // Returns a list of drop down menu items for the required tools.
    return [
      DropdownMenuItem(
        value: _vertexTool,
        child: const Text('Vertex Tool'),
      ),
      DropdownMenuItem(
        value: _reticleVertexTool,
        child: const Text('Reticle Vertex Tool'),
      ),
    ];
  }
}

extension on String {
  // An extension on String to capitalize the first character of the String.
  String capitalize() {
    return '${this[0].toUpperCase()}${substring(1).toLowerCase()}';
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.