Loading...
Searching...
No Matches
default_navigation_view.dart
Go to the documentation of this file.
1import 'package:flutter/material.dart';
2import 'package:navigine_sdk/error.dart';
3import 'package:navigine_sdk/com/navigine/idl/location.dart';
4import 'package:navigine_sdk/com/navigine/idl/location_listener.dart';
5import 'package:navigine_sdk/com/navigine/idl/location_manager.dart';
6import 'package:navigine_sdk/com/navigine/idl/location_window.dart';
7import 'package:navigine_sdk/com/navigine/idl/sublocation_change_listener.dart';
8import 'package:navigine_sdk/com/navigine/idl/navigine_sdk.dart';
9import 'package:navigine_sdk/com/navigine/idl/user_location_layer.dart';
10import 'package:navigine_sdk/com/navigine/idl/camera_listener.dart';
11import 'package:navigine_sdk/com/navigine/idl/camera_update_reason.dart';
12import 'package:navigine_sdk/location_view.dart';
13import 'dart:math' as math;
14import 'widgets/zoom_controls.dart';
15import 'widgets/floor_selector_view.dart';
16import 'widgets/follow_me_button.dart';
17
28class DefaultNavigationView extends StatefulWidget {
29 const DefaultNavigationView({
30 Key? key,
31 required this.onViewCreated,
32 this.textDirection,
33 }) : super(key: key);
34
35 final void Function(LocationWindow) onViewCreated;
36 final TextDirection? textDirection;
37
38 @override
39 State<DefaultNavigationView> createState() => _DefaultNavigationViewState();
40}
41
42class _DefaultNavigationViewState extends State<DefaultNavigationView> implements LocationListener, SublocationChangeListener, CameraListener {
43 LocationWindow? _locationWindow;
44 late LocationManager _locationManager;
45 UserLocationLayer? _userLocationLayer;
46 final GlobalKey<FloorSelectorViewState> _floorSelectorKey = GlobalKey<FloorSelectorViewState>();
47 bool _isFollowing = false;
48
49 @override
50 void initState() {
51 super.initState();
52 _locationManager = NavigineSdk.getInstance().getLocationManager();
53 }
54
55 @override
56 void dispose() {
57 _locationManager.removeLocationListener(this);
58 _locationWindow?.removeSublocationChangeListener(this);
59 _locationWindow?.removeCameraListener(this);
60 super.dispose();
61 }
62
63 void _handleViewCreated(LocationWindow window) {
64 setState(() {
65 _locationWindow = window;
66 });
68 window.addCameraListener(this);
69 _locationManager.addLocationListener(this);
70 _userLocationLayer = NavigineSdk.getInstance().getUserLocationLayer(window);
71 _userLocationLayer!.setVisible(true);
72 widget.onViewCreated(window);
73 }
74
75 @override
76 Widget build(BuildContext context) {
77 return Stack(
78 children: [
79 LocationView(
80 onViewCreated: _handleViewCreated,
81 textDirection: widget.textDirection,
82 ),
83
84 ZoomControls(
85 zoomInPressed: () {
86 if (_locationWindow == null) return;
87 double currentZoom = _locationWindow!.zoomFactor;
88 _locationWindow!.zoomFactor = currentZoom * (3.0 / 2.0);
89 },
90 zoomOutPressed: () {
91 if (_locationWindow == null) return;
92 double currentZoom = _locationWindow!.zoomFactor;
93 _locationWindow!.zoomFactor = currentZoom * (2.0 / 3.0);
94 },
95 ),
96 FloorSelectorView(
97 key: _floorSelectorKey,
98 onFloorSelected: (id, name) {
99 _locationWindow?.setSublocationId(id);
100 },
101 ),
102 FollowMeButton(
103 isFollowing: _isFollowing,
104 onPressed: _handleFollowMePressed,
105 ),
106 ],
107 );
108 }
109
110 void _handleFollowMePressed() {
111 if (!mounted || _userLocationLayer == null) return;
112
113 if (!_isFollowing) {
114 final screenSize = MediaQuery.of(context).size;
115 final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
116 _userLocationLayer!.setAnchor(math.Point<double>(
117 screenSize.width * devicePixelRatio / 2,
118 screenSize.height * devicePixelRatio / 2,
119 ));
120 } else {
121 _userLocationLayer!.resetAnchor();
122 }
123 setState(() {
124 _isFollowing = !_isFollowing;
125 });
126 }
127
128 @override
129 void onLocationLoaded(Location location) {
130 final floors = location.sublocations.map((s) => LevelInfo(
131 levelId: s.levelId,
132 sublocationId: s.id,
133 )).toList();
134 _floorSelectorKey.currentState?.setFloors(floors);
135 }
136
137 @override
138 void onLocationUploaded(int locationId) {}
139
140 @override
141 void onLocationFailed(int locationId, Error error) {}
142
143 @override
144 void onActiveSublocationChanged(int sublocationId) {
145 _floorSelectorKey.currentState?.setSublocationId(sublocationId);
146 }
147
148 @override
149 void onCameraPositionChanged(CameraUpdateReason reason, bool finished) {
150 if (reason == CameraUpdateReason.APPLICATION) {
151 return;
152 }
153
154 if (_isFollowing && _userLocationLayer != null) {
155 _userLocationLayer!.resetAnchor();
156 setState(() {
157 _isFollowing = false;
158 });
159 }
160 }
161}