wip countdown timer
This commit is contained in:
parent
26380dc55b
commit
4bf05fb655
1
devtools_options.yaml
Normal file
1
devtools_options.yaml
Normal file
@ -0,0 +1 @@
|
||||
extensions:
|
28
lib/extensions/dateOfTimeExtensions.dart
Normal file
28
lib/extensions/dateOfTimeExtensions.dart
Normal file
@ -0,0 +1,28 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
extension TimeExtensions on DateTime {
|
||||
DateTime forwardTo(TimeOfDay timeOfDay) {
|
||||
final now = DateTime.now();
|
||||
final today = DateTime(now.year, now.month, now.day);
|
||||
DateTime result;
|
||||
|
||||
final targetTime = DateTime(now.year, now.month, now.day, timeOfDay.hour, timeOfDay.minute);
|
||||
if (targetTime.isBefore(now) || targetTime.isAtSameMomentAs(now)) {
|
||||
result = today.add(Duration(days: 1));
|
||||
} else {
|
||||
result = today;
|
||||
}
|
||||
|
||||
return result.add(Duration(hours: timeOfDay.hour, minutes: timeOfDay.minute));
|
||||
}
|
||||
|
||||
bool isBetweenTimeOfDay(TimeOfDay start, TimeOfDay end) {
|
||||
final now = this;
|
||||
final today = DateTime(now.year, now.month, now.day);
|
||||
|
||||
final startTime = today.add(Duration(hours: start.hour, minutes: start.minute));
|
||||
final endTime = today.add(Duration(hours: end.hour, minutes: end.minute));
|
||||
|
||||
return now.isAfter(startTime) && now.isBefore(endTime);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import 'package:app/state/legalStatusState.dart';
|
||||
import 'package:app/state/timeStatusState.dart';
|
||||
import 'package:app/state/mapState.dart';
|
||||
import 'package:app/view/home.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@ -8,7 +8,7 @@ void main() {
|
||||
runApp(
|
||||
MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider<LegalStatusState>(create: (context) => LegalStatusState()),
|
||||
ChangeNotifierProvider<TimeStatusState>(create: (context) => TimeStatusState()),
|
||||
ChangeNotifierProvider<MapState>(create: (context) => MapState()),
|
||||
],
|
||||
builder: (context, child) => const MyApp(),
|
||||
|
@ -1,61 +0,0 @@
|
||||
import 'package:app/model/legelState.dart';
|
||||
import 'package:app/model/stateInfo.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LegalStatusState extends ChangeNotifier {
|
||||
LegalState _legalState = LegalState.unknown;
|
||||
|
||||
LegalState get getLegalState => _legalState;
|
||||
|
||||
StateInfo get info {
|
||||
switch(_legalState) {
|
||||
case LegalState.allowed:
|
||||
return StateInfo(
|
||||
icon: Icons.check,
|
||||
color: Colors.lightGreen,
|
||||
title: "Passt",
|
||||
subtitle: "Alle kriterien erfüllt",
|
||||
description: "asd"
|
||||
);
|
||||
|
||||
case LegalState.disallowedTime:
|
||||
return StateInfo(
|
||||
icon: Icons.timer_outlined,
|
||||
color: Colors.amber,
|
||||
title: "Zu früh",
|
||||
subtitle: "Bubazen ist nur zwischn 20 und 6 uhr Abends/ Nachts erlaubt!",
|
||||
description: "asd"
|
||||
);
|
||||
|
||||
case LegalState.disallowedRegion:
|
||||
return StateInfo(
|
||||
icon: Icons.location_off_outlined,
|
||||
color: Colors.red,
|
||||
title: "Zu nah an einem geschützen Gebäude",
|
||||
subtitle: "asd",
|
||||
description: "asd"
|
||||
);
|
||||
|
||||
case LegalState.unknown:
|
||||
default:
|
||||
return StateInfo(
|
||||
icon: Icons.question_mark_outlined,
|
||||
color: Colors.grey,
|
||||
title: "Status nicht bekannt",
|
||||
subtitle: "asd",
|
||||
description: "asd"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
set setInSightOfDisallowedAreas(bool isInSight) {
|
||||
_legalState = LegalState.disallowedRegion;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
set setIsInDisallowedTimeRanges(bool isInDisallowedTime) {
|
||||
_legalState = LegalState.disallowedTime;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
@ -1,11 +1,14 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
|
||||
class MapState extends ChangeNotifier {
|
||||
bool _isLocationLock = false;
|
||||
bool _isCurrentlyFetchin = true;
|
||||
bool _isCurrentlyFetchin = false;
|
||||
final MapController _mapController = MapController();
|
||||
|
||||
bool get followLocation => _isLocationLock;
|
||||
bool get isCurrentlyFetching => _isCurrentlyFetchin;
|
||||
MapController get getMapController => _mapController;
|
||||
|
||||
toggleLocationLock() {
|
||||
_isLocationLock = !_isLocationLock;
|
||||
|
46
lib/state/timeStatusState.dart
Normal file
46
lib/state/timeStatusState.dart
Normal file
@ -0,0 +1,46 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class TimeStatusState extends ChangeNotifier {
|
||||
final TimeOfDay legalStart = const TimeOfDay(hour: 20, minute: 00);
|
||||
final TimeOfDay legalEnd = const TimeOfDay(hour: 07, minute: 00);
|
||||
|
||||
late Timer _refreshTimer;
|
||||
|
||||
TimeStatusState() {
|
||||
_refreshTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
bool get isCurrentlyLegal {
|
||||
final now = TimeOfDay.now();
|
||||
final currentTime = DateTime.now();
|
||||
|
||||
final startDateTime = DateTime(currentTime.year, currentTime.month, currentTime.day, legalStart.hour, legalStart.minute);
|
||||
final currentDateTime = currentTime.subtract(Duration(hours: now.hour, minutes: now.minute));
|
||||
|
||||
return currentDateTime.isBefore(startDateTime);
|
||||
}
|
||||
|
||||
Duration get remainingTimeUntilStart {
|
||||
final now = DateTime.now();
|
||||
final today = DateTime(now.year, now.month, now.day);
|
||||
final targetDateTime = today.add(Duration(hours: legalStart.hour, minutes: legalStart.minute));
|
||||
|
||||
if (now.isBefore(targetDateTime)) {
|
||||
return targetDateTime.difference(now);
|
||||
} else {
|
||||
final nextDay = today.add(const Duration(days: 1));
|
||||
final nextTargetDateTime = nextDay.add(Duration(hours: legalStart.hour, minutes: legalStart.minute));
|
||||
return nextTargetDateTime.difference(now);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_refreshTimer.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
|
||||
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';
|
||||
@ -46,10 +48,14 @@ class _HomePageState extends State<HomeView> {
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
const SizedBox(
|
||||
height: 100,
|
||||
child: StatusView(),
|
||||
),
|
||||
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) {
|
||||
|
@ -1,3 +1,5 @@
|
||||
import 'package:app/extensions/obtainProviderExtension.dart';
|
||||
import 'package:app/state/mapState.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:http/http.dart';
|
||||
@ -15,6 +17,7 @@ class _MapViewState extends State<MapView> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FlutterMap(
|
||||
mapController: context.obtainState<MapState>().getMapController,
|
||||
options: const MapOptions(
|
||||
initialCenter: LatLng(50.354, 7.5845),
|
||||
initialZoom: 16,
|
||||
@ -26,9 +29,9 @@ class _MapViewState extends State<MapView> {
|
||||
TileLayer(
|
||||
urlTemplate: "https://bubatzkarte.de/tiles/radius/100/{z}/{x}/{y}.png",
|
||||
tileProvider: NetworkTileProvider(
|
||||
httpClient: RetryClient(
|
||||
Client(),
|
||||
when: (response) => response.statusCode == 502)
|
||||
httpClient: RetryClient(
|
||||
Client(),
|
||||
when: (response) => response.statusCode == 502)
|
||||
),
|
||||
)
|
||||
],
|
||||
|
@ -1,25 +1,26 @@
|
||||
import 'package:app/util/watchState.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_timer_countdown/flutter_timer_countdown.dart';
|
||||
|
||||
import '../state/legalStatusState.dart';
|
||||
import '../state/timeStatusState.dart';
|
||||
|
||||
class StatusView extends StatelessWidget {
|
||||
const StatusView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return WatchState<LegalStatusState>((context, state) {
|
||||
return WatchState<TimeStatusState>((context, state) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: state.info.color
|
||||
color: Theme.of(context).colorScheme.surface
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Center(
|
||||
const Center(
|
||||
child: Icon(
|
||||
state.info.icon,
|
||||
Icons.timer_outlined,
|
||||
size: 40,
|
||||
),
|
||||
),
|
||||
@ -28,7 +29,7 @@ class StatusView extends StatelessWidget {
|
||||
indent: 20,
|
||||
color: Colors.white,
|
||||
thickness: 3,
|
||||
width: 30,
|
||||
width: 40,
|
||||
),
|
||||
Center(
|
||||
child: Column(
|
||||
@ -36,21 +37,22 @@ class StatusView extends StatelessWidget {
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
state.info.title,
|
||||
style: const TextStyle(fontSize: 20),
|
||||
const Text(
|
||||
"Ab 20 Uhr",
|
||||
style: TextStyle(fontSize: 20),
|
||||
),
|
||||
TimerCountdown(
|
||||
format: CountDownTimerFormat.hoursMinutesSeconds,
|
||||
enableDescriptions: false,
|
||||
spacerWidth: 5,
|
||||
endTime: DateTime.now().add(state.remainingTimeUntilStart),
|
||||
onEnd: () {
|
||||
print("Timer finished");
|
||||
},
|
||||
),
|
||||
Text(state.info.subtitle),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 20),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.info_outline),
|
||||
onPressed: () {
|
||||
|
||||
},
|
||||
)
|
||||
],
|
||||
)
|
||||
);
|
||||
|
@ -163,6 +163,14 @@ packages:
|
||||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
flutter_timer_countdown:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: flutter_timer_countdown
|
||||
sha256: "0c73e1593ad7949c007752199a17e7ed50bb581568743dbc32f061f49873219e"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.0.7"
|
||||
flutter_web_plugins:
|
||||
dependency: transitive
|
||||
description: flutter
|
||||
|
@ -41,6 +41,7 @@ dependencies:
|
||||
flutter_map: ^6.1.0
|
||||
latlong2: ^0.9.0
|
||||
http: ^1.2.1
|
||||
flutter_timer_countdown: ^1.0.7
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
Reference in New Issue
Block a user