|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/src/foundation/key.dart'; |
| 3 | +import 'package:flutter/src/widgets/framework.dart'; |
| 4 | +import 'package:flutter_map/flutter_map.dart'; |
| 5 | +import 'package:geolocator/geolocator.dart'; |
| 6 | +import 'package:latlong2/latlong.dart'; |
| 7 | +import 'package:propview/config.dart'; |
| 8 | +import 'package:propview/models/User.dart'; |
| 9 | + |
| 10 | +class MapScreen extends StatefulWidget { |
| 11 | + final User user; |
| 12 | + const MapScreen({Key key, this.user}) : super(key: key); |
| 13 | + |
| 14 | + @override |
| 15 | + State<MapScreen> createState() => _MapScreenState(); |
| 16 | +} |
| 17 | + |
| 18 | +class _MapScreenState extends State<MapScreen> { |
| 19 | + bool loading = true; |
| 20 | + List<LatLng> points = []; |
| 21 | + Position position = Position(latitude: 0, longitude: 0); |
| 22 | + getLocation() async { |
| 23 | + setState(() { |
| 24 | + loading = true; |
| 25 | + }); |
| 26 | + LocationPermission permission = await Geolocator.checkPermission(); |
| 27 | + if (permission == LocationPermission.denied) { |
| 28 | + permission = await Geolocator.requestPermission(); |
| 29 | + } |
| 30 | + if (permission == LocationPermission.deniedForever) { |
| 31 | + Geolocator.openAppSettings(); |
| 32 | + } |
| 33 | + position = await Geolocator.getCurrentPosition( |
| 34 | + desiredAccuracy: LocationAccuracy.high); |
| 35 | + setState(() { |
| 36 | + print("updated"); |
| 37 | + points = <LatLng>[ |
| 38 | + LatLng(position.latitude, position.longitude), |
| 39 | + LatLng(position.latitude + 1, position.longitude - 1), |
| 40 | + LatLng(position.latitude + 1, position.longitude + 1), |
| 41 | + LatLng(position.latitude - 1, position.longitude + 1), |
| 42 | + ]; |
| 43 | + loading = false; |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + MapController mapController = MapController(); |
| 48 | + |
| 49 | + @override |
| 50 | + void initState() { |
| 51 | + super.initState(); |
| 52 | + getLocation(); |
| 53 | + } |
| 54 | + |
| 55 | + @override |
| 56 | + Widget build(BuildContext context) { |
| 57 | + return Scaffold( |
| 58 | + body: loading |
| 59 | + ? Center( |
| 60 | + child: CircularProgressIndicator(), |
| 61 | + ) |
| 62 | + : FlutterMap( |
| 63 | + options: MapOptions( |
| 64 | + center: LatLng(position.latitude, position.longitude), |
| 65 | + zoom: 5.0, |
| 66 | + onLongPress: (tapPostition, latLang) { |
| 67 | + print(latLang.latitude); |
| 68 | + print(latLang.longitude); |
| 69 | + }, |
| 70 | + // onTap: (tapPosition, point) { |
| 71 | + // setState(() { |
| 72 | + // debugPrint('onTap'); |
| 73 | + // polylines = getPolylines(); |
| 74 | + // }); |
| 75 | + // }, |
| 76 | + ), |
| 77 | + layers: [ |
| 78 | + TileLayerOptions( |
| 79 | + urlTemplate: |
| 80 | + 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
| 81 | + subdomains: ['a', 'b', 'c']), |
| 82 | + PolylineLayerOptions( |
| 83 | + polylines: [ |
| 84 | + Polyline( |
| 85 | + points: points, strokeWidth: 4.0, color: Colors.purple), |
| 86 | + ], |
| 87 | + ), |
| 88 | + MarkerLayerOptions( |
| 89 | + markers: [ |
| 90 | + Marker( |
| 91 | + width: 34.0, |
| 92 | + height: 34.0, |
| 93 | + point: |
| 94 | + LatLng(points.first.latitude, points.first.longitude), |
| 95 | + builder: (ctx) => Container( |
| 96 | + child: Icon( |
| 97 | + Icons.flag, |
| 98 | + color: Colors.red, |
| 99 | + ), |
| 100 | + ), |
| 101 | + ), |
| 102 | + Marker( |
| 103 | + width: 34.0, |
| 104 | + height: 34.0, |
| 105 | + point: |
| 106 | + LatLng(points.last.latitude, points.last.longitude), |
| 107 | + builder: (ctx) => Container( |
| 108 | + child: CircleAvatar( |
| 109 | + backgroundColor: Colors.green, |
| 110 | + radius: 15, |
| 111 | + child: ClipOval( |
| 112 | + child: FadeInImage.assetNetwork( |
| 113 | + height: 30, |
| 114 | + width: 30, |
| 115 | + fit: BoxFit.cover, |
| 116 | + placeholder: "assets/loader.gif", |
| 117 | + image: |
| 118 | + "${Config.STORAGE_ENDPOINT}${widget.user.userId}.jpeg", |
| 119 | + imageErrorBuilder: (BuildContext context, |
| 120 | + Object exception, StackTrace stackTrace) { |
| 121 | + return CircleAvatar( |
| 122 | + backgroundColor: Colors.white, |
| 123 | + radius: 15, |
| 124 | + backgroundImage: AssetImage( |
| 125 | + "assets/dummy.png", |
| 126 | + ), |
| 127 | + ); |
| 128 | + }, |
| 129 | + ), |
| 130 | + ), |
| 131 | + ), |
| 132 | + ), |
| 133 | + ), |
| 134 | + ], |
| 135 | + ), |
| 136 | + ], |
| 137 | + ), |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments