This repository has been archived on 2024-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
app/lib/view/home.dart

128 lines
3.8 KiB
Dart

import 'package:app/extensions/obtainProviderExtension.dart';
import 'package:app/state/mapState.dart';
import 'package:app/state/timeStatusState.dart';
import 'package:app/util/loadingContainer.dart';
import 'package:app/util/watchState.dart';
import 'package:app/view/appInfo.dart';
import 'package:app/view/locationSearch.dart';
import 'package:app/view/status.dart';
import 'package:app/view/map.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:latlong2/latlong.dart';
import 'package:provider/provider.dart';
class HomeView extends StatefulWidget {
const HomeView({super.key});
@override
State<HomeView> createState() => _HomePageState();
}
class _HomePageState extends State<HomeView> {
bool locationEnabled = false;
Future<LatLng?> getLatLng() async {
LocationPermission permissionStatus = await Geolocator.checkPermission();
if (permissionStatus == LocationPermission.denied) {
permissionStatus = await Geolocator.requestPermission();
if (permissionStatus == LocationPermission.denied || permissionStatus == LocationPermission.deniedForever) {
return null;
}
}
setState(() {
locationEnabled = true;
});
Position? position;
try {
position = await Geolocator.getCurrentPosition();
} catch (e) {
position = await Geolocator.getLastKnownPosition();
}
if (position != null) {
return LatLng(position.latitude, position.longitude);
}
return null;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text("Bubatzkarte"),
actions: [
IconButton(
icon: const Icon(Icons.search),
onPressed: () async {
LatLng? latLng = await showSearch<LatLng?>(
context: context,
delegate: LocationSearchDelegate(),
);
MapState mapState = context.obtainState<MapState>();
mapState.setActiveMarker(latLng);
if (latLng != null) {
mapState.getMapController.move(latLng, 16);
}
},
),
IconButton(
icon: const Icon(Icons.info_outline),
onPressed: () {
showDialog(
context: context,
builder: (context) => const AppInfoView(),
);
},
),
],
),
floatingActionButton: FloatingActionButton(
child: WatchState<MapState>((context, state) => Icon(
locationEnabled
? state.isCurrentlyLoading
? Icons.location_searching
: Icons.my_location
: Icons.location_disabled
)),
onPressed: () async {
MapState mapState = context.obtainState<MapState>();
mapState.setLoading(true);
LatLng? latLng = await getLatLng();
if (latLng != null) {
mapState.getMapController.move(latLng, 16);
}
mapState.setLoading(false);
},
),
body: Column(
children: [
WatchState<TimeStatusState>((context, state) {
if(state.isCurrentlyLegal) return const SizedBox.shrink();
return const SizedBox(
height: 100,
child: StatusView(),
);
}),
Expanded(
child: Consumer<MapState>(
builder: (context, state, child) {
return LoadingContainer(
loading: child == null,
fetching: state.isCurrentlyLoading,
child: const MapView(),
);
},
child: const MapView(),
),
)
],
),
);
}
}