|
| 1 | +// Copyright 2025 Esri |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +import 'package:arcgis_maps/arcgis_maps.dart'; |
| 17 | +import 'package:arcgis_maps_sdk_flutter_samples/common/common.dart'; |
| 18 | +import 'package:flutter/material.dart'; |
| 19 | + |
| 20 | +class AddBuildingSceneLayer extends StatefulWidget { |
| 21 | + const AddBuildingSceneLayer({super.key}); |
| 22 | + |
| 23 | + @override |
| 24 | + State<AddBuildingSceneLayer> createState() => _AddBuildingSceneLayerState(); |
| 25 | +} |
| 26 | + |
| 27 | +class _AddBuildingSceneLayerState extends State<AddBuildingSceneLayer> |
| 28 | + with SampleStateSupport { |
| 29 | + // Create a controller for the local scene view. |
| 30 | + final _localSceneViewController = ArcGISLocalSceneView.createController(); |
| 31 | + |
| 32 | + // A flag for when the local scene view is ready and controls can be used. |
| 33 | + var _ready = false; |
| 34 | + |
| 35 | + // The overview sublayer. This contains a simplified, exterior-only model of |
| 36 | + // the building. |
| 37 | + late final BuildingSublayer _overviewSublayer; |
| 38 | + |
| 39 | + // The full model sublayer. This contains the fully detailed model of the |
| 40 | + // building including all exterior and interior features. |
| 41 | + late final BuildingSublayer _fullModelSublayer; |
| 42 | + |
| 43 | + // Flag indicating if the app is displaying the full model or overview sublayer. |
| 44 | + var _showFullModel = false; |
| 45 | + |
| 46 | + @override |
| 47 | + Widget build(BuildContext context) { |
| 48 | + return Scaffold( |
| 49 | + body: SafeArea( |
| 50 | + top: false, |
| 51 | + left: false, |
| 52 | + right: false, |
| 53 | + child: Stack( |
| 54 | + children: [ |
| 55 | + Column( |
| 56 | + children: [ |
| 57 | + Expanded( |
| 58 | + // Add a local scene view to the widget tree and set the controller. |
| 59 | + child: ArcGISLocalSceneView( |
| 60 | + controllerProvider: () => _localSceneViewController, |
| 61 | + onLocalSceneViewReady: onLocalSceneViewReady, |
| 62 | + ), |
| 63 | + ), |
| 64 | + Row( |
| 65 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, |
| 66 | + children: [ |
| 67 | + // Button to toggle overview or full model sublayers. |
| 68 | + ElevatedButton( |
| 69 | + onPressed: toggleModelView, |
| 70 | + child: Text(_showFullModel ? 'Overview' : 'Full Model'), |
| 71 | + ), |
| 72 | + ], |
| 73 | + ), |
| 74 | + ], |
| 75 | + ), |
| 76 | + // Display a progress indicator and prevent interaction until state is ready. |
| 77 | + LoadingIndicator(visible: !_ready), |
| 78 | + ], |
| 79 | + ), |
| 80 | + ), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + Future<void> onLocalSceneViewReady() async { |
| 85 | + // Initialize the Scene. |
| 86 | + final scene = _initializeScene(); |
| 87 | + |
| 88 | + // Initialize the BuildingSceneLayer. |
| 89 | + final buildingSceneLayer = await _initializeBuildingSceneLayer(); |
| 90 | + |
| 91 | + // Add the BuildingSceneLayer to the Scene. |
| 92 | + scene.operationalLayers.add(buildingSceneLayer); |
| 93 | + |
| 94 | + // Add the Scene to the LocalSceneViewController. |
| 95 | + _localSceneViewController.arcGISScene = scene; |
| 96 | + |
| 97 | + // Set an initial viewpoint camera. |
| 98 | + final viewpointCamera = Camera.withLocation( |
| 99 | + location: ArcGISPoint( |
| 100 | + x: -13045109, |
| 101 | + y: 4036614, |
| 102 | + z: 511, |
| 103 | + spatialReference: SpatialReference.webMercator, |
| 104 | + ), |
| 105 | + heading: 343, |
| 106 | + pitch: 64, |
| 107 | + roll: 0, |
| 108 | + ); |
| 109 | + _localSceneViewController.setViewpointCamera(viewpointCamera); |
| 110 | + |
| 111 | + // Set the ready state variable to true to enable the sample UI. |
| 112 | + setState(() => _ready = true); |
| 113 | + } |
| 114 | + |
| 115 | + ArcGISScene _initializeScene() { |
| 116 | + // Create a Scene with a topographic basemap style. |
| 117 | + final scene = ArcGISScene.withBasemapStyle( |
| 118 | + BasemapStyle.arcGISTopographic, |
| 119 | + viewingMode: SceneViewingMode.local, |
| 120 | + ); |
| 121 | + |
| 122 | + // Add an ElevationSource to the Scene. |
| 123 | + final elevationSource = ArcGISTiledElevationSource.withUri( |
| 124 | + Uri.parse( |
| 125 | + 'https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer', |
| 126 | + ), |
| 127 | + ); |
| 128 | + scene.baseSurface.elevationSources.add(elevationSource); |
| 129 | + |
| 130 | + return scene; |
| 131 | + } |
| 132 | + |
| 133 | + Future<BuildingSceneLayer> _initializeBuildingSceneLayer() async { |
| 134 | + // Load the BuildingSceneLayer. |
| 135 | + final buildingSceneLayer = BuildingSceneLayer.withUri( |
| 136 | + Uri.parse( |
| 137 | + 'https://www.arcgis.com/home/item.html?id=669f6279c579486eb4a0acc7eb59d7ca', |
| 138 | + ), |
| 139 | + ); |
| 140 | + await buildingSceneLayer.load(); |
| 141 | + |
| 142 | + // Extract the overview and full model sublayers. |
| 143 | + _overviewSublayer = buildingSceneLayer.sublayers.firstWhere( |
| 144 | + (sublayer) => sublayer.name == 'Overview', |
| 145 | + ); |
| 146 | + _fullModelSublayer = buildingSceneLayer.sublayers.firstWhere( |
| 147 | + (sublayer) => sublayer.name == 'Full Model', |
| 148 | + ); |
| 149 | + |
| 150 | + return buildingSceneLayer; |
| 151 | + } |
| 152 | + |
| 153 | + void toggleModelView() { |
| 154 | + // Toggle the visibilities for the sublayers. |
| 155 | + _overviewSublayer.isVisible = _showFullModel; |
| 156 | + _fullModelSublayer.isVisible = !_showFullModel; |
| 157 | + |
| 158 | + // Set the state for the new full model visibility. |
| 159 | + setState(() => _showFullModel = !_showFullModel); |
| 160 | + } |
| 161 | +} |
0 commit comments