116 lines
3.0 KiB
Dart

import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:flutter_remix/flutter_remix.dart';
import 'package:furman_now/src/services/restaurants/restaurant_service.dart';
@immutable
class MapCategory {
final String name;
final IconData icon;
final Function activator;
const MapCategory({
required this.name,
required this.icon,
required this.activator,
});
}
class MapPageState extends ChangeNotifier {
MapPageState({
required TickerProvider vsync
}): _animationController = AnimationController(
duration: const Duration(milliseconds: 300),
vsync: vsync,
) {
_initState();
}
void _initState() {
mapController.mapEventStream.listen((event) {
if (event is MapEventRotate) {
rotation = mapController.rotation * (2 * pi) / 360;
}
});
centerOnLocationUpdate = CenterOnLocationUpdate.always;
centerCurrentLocationStreamController = StreamController<double?>();
}
final MapController mapController = MapController();
final AnimationController _animationController;
late CenterOnLocationUpdate _centerOnLocationUpdate;
CenterOnLocationUpdate get centerOnLocationUpdate => _centerOnLocationUpdate;
set centerOnLocationUpdate (CenterOnLocationUpdate value) {
_centerOnLocationUpdate = value;
notifyListeners();
}
late StreamController<double?> centerCurrentLocationStreamController;
var _rotation = 0.0;
double get rotation => _rotation;
set rotation (double value) {
_rotation = value;
notifyListeners();
}
List<Marker> _markers = <Marker>[];
List<Marker> get markers => _markers;
set markers (List<Marker> value) {
_markers = value;
notifyListeners();
}
void resetRotation() async {
// take the shortest rotation path
var end = mapController.rotation > 180 ? 360.0 : 0.0;
var animation = Tween<double>(
begin: mapController.rotation,
end: end,
).animate(CurvedAnimation(
parent: _animationController,
curve: Curves.easeInOut,
));
animationListener() {
mapController.rotate(animation.value);
}
animation.addListener(animationListener);
await _animationController.forward();
animation.removeListener(animationListener);
_animationController.reset();
mapController.rotate(0);
}
late final List<MapCategory> categories = [
MapCategory(
name: "Restaurants",
icon: FlutterRemix.restaurant_line,
activator: showRestaurants,
),
];
Future<void> showRestaurants() async {
var restaurants = await RestaurantService.fetchRestaurants();
var newMarkers = restaurants.map((restaurant) => Marker(
point: restaurant.mapLocation,
width: 40,
height: 40,
builder: (context) => GestureDetector(
onTapDown: (e) => print("tapped"),
child: const FlutterLogo()
),
));
markers = newMarkers.toList();
}
}