From fad2c2e73b591478e50269b731da53ca85f43c3c Mon Sep 17 00:00:00 2001 From: reinjens Date: Tue, 12 Nov 2024 08:41:39 +0100 Subject: [PATCH] Added int, gmaps and context extensions --- lib/open_exts.dart | 3 ++ lib/src/color_extensions.dart | 2 +- lib/src/context_extensions.dart | 43 +++++++++++++++++++++++++++++ lib/src/datetime_extensions.dart | 15 +++++----- lib/src/google_maps_extensions.dart | 38 +++++++++++++++++++++++++ lib/src/int_extensions.dart | 13 +++++++++ lib/src/string_extensions.dart | 2 +- pubspec.yaml | 3 +- 8 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 lib/src/context_extensions.dart create mode 100644 lib/src/google_maps_extensions.dart create mode 100644 lib/src/int_extensions.dart diff --git a/lib/open_exts.dart b/lib/open_exts.dart index 8d5c283..fa65968 100644 --- a/lib/open_exts.dart +++ b/lib/open_exts.dart @@ -1,6 +1,9 @@ library open_exts; export 'src/color_extensions.dart'; +export 'src/context_extensions.dart'; export 'src/datetime_extensions.dart'; export 'src/duration_extensions.dart'; +export 'src/google_maps_extensions.dart'; +export 'src/int_extensions.dart'; export 'src/string_extensions.dart'; diff --git a/lib/src/color_extensions.dart b/lib/src/color_extensions.dart index ceaa6c2..0397043 100644 --- a/lib/src/color_extensions.dart +++ b/lib/src/color_extensions.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; extension OpenColorExtensions on Color { - Color fromHex(String hexString) { + static Color fromHex(String hexString) { final buffer = StringBuffer(); if (hexString.length == 6 || hexString.length == 7) buffer.write('ff'); buffer.write(hexString.replaceFirst('#', '')); diff --git a/lib/src/context_extensions.dart b/lib/src/context_extensions.dart new file mode 100644 index 0000000..eb3021c --- /dev/null +++ b/lib/src/context_extensions.dart @@ -0,0 +1,43 @@ +import 'package:flutter/material.dart'; + +extension OpenContextExtensions on BuildContext { + bool get isMobile => MediaQuery.of(this).size.width <= 500.0; + + bool get isTablet => + MediaQuery.of(this).size.width < 1024.0 && + MediaQuery.of(this).size.width >= 650.0; + + bool get isSmallTablet => + MediaQuery.of(this).size.width < 650.0 && + MediaQuery.of(this).size.width > 500.0; + + bool get isDesktop => MediaQuery.of(this).size.width >= 1024.0; + + bool get isSmall => + MediaQuery.of(this).size.width < 850.0 && + MediaQuery.of(this).size.width >= 560.0; + + double get width => MediaQuery.of(this).size.width; + + double get height => MediaQuery.of(this).size.height; + + Size get size => MediaQuery.of(this).size; + + ThemeData get theme => Theme.of(this); + + TextTheme get textTheme => theme.textTheme; + + ColorScheme get colorScheme => theme.colorScheme; + + DefaultTextStyle get defaultTextStyle => DefaultTextStyle.of(this); + + MediaQueryData get mediaQuery => MediaQuery.of(this); + + NavigatorState get navigator => Navigator.of(this); + + FocusScopeNode get focusScope => FocusScope.of(this); + + ScaffoldState get scaffold => Scaffold.of(this); + + ScaffoldMessengerState get scaffoldMessenger => ScaffoldMessenger.of(this); +} diff --git a/lib/src/datetime_extensions.dart b/lib/src/datetime_extensions.dart index cffdb91..7f0e6cd 100644 --- a/lib/src/datetime_extensions.dart +++ b/lib/src/datetime_extensions.dart @@ -1,5 +1,5 @@ extension OpenDatetimeExtensions on DateTime { - static DateTime get _now => DateTime.now(); + static final DateTime _now = DateTime.now(); String nowString() => _now.toUtc().toIso8601String(); @@ -14,15 +14,15 @@ extension OpenDatetimeExtensions on DateTime { bool isAfternoon() => hour >= 12 && hour < 18; bool isEvening() => hour < 4 || hour >= 18; - String getTimeOfDay() { + String get timeOfDay { if (isMorning()) return 'morning'; if (isAfternoon()) return 'afternoon'; return 'evening'; } - DateTime findFirstDateOfTheWeek() => subtract(Duration(days: weekday - 1)); + DateTime get firstDateOfTheWeek => subtract(Duration(days: weekday - 1)); - DateTime findLastDateOfTheWeek() => + DateTime get lastDateOfTheWeek => add(Duration(days: DateTime.daysPerWeek - weekday)); String formatSeconds(int value) { @@ -34,13 +34,12 @@ extension OpenDatetimeExtensions on DateTime { String formatMinutes(int value, {bool allowZero = false}) { final hours = (value ~/ 60).toString().padLeft(2, '0'); - var minutes = (value % 60).toString().padLeft(2, '0'); - if (minutes == '00' && !allowZero) minutes = '01'; - return "$hours:$minutes"; + final minutes = (value % 60).toString().padLeft(2, '0'); + return minutes == '00' && !allowZero ? "$hours:01" : "$hours:$minutes"; } DateTime get lastDayOfMonth => - month < 12 ? DateTime(year, month + 1, 0) : DateTime(year + 1, 1, 0); + (month < 12) ? DateTime(year, month + 1, 0) : DateTime(year + 1, 1, 0); static bool _isSameDay(DateTime date1, DateTime date2) { return date1.year == date2.year && diff --git a/lib/src/google_maps_extensions.dart b/lib/src/google_maps_extensions.dart new file mode 100644 index 0000000..792b1a0 --- /dev/null +++ b/lib/src/google_maps_extensions.dart @@ -0,0 +1,38 @@ +import 'dart:math'; + +import 'package:google_maps_flutter/google_maps_flutter.dart'; + +extension OpenGoogleMapsExtensions on GoogleMap { + LatLngBounds getBoundsFromLatLngList(List list) { + assert(list.isNotEmpty, 'The list cannot be empty'); + + // Initialize bounds to the first point + double x0 = list[0].latitude; + double x1 = x0; + double y0 = list[0].longitude; + double y1 = y0; + + // Update bounds based on each point in the list + for (var point in list.skip(1)) { + x0 = min(x0, point.latitude); + x1 = max(x1, point.latitude); + y0 = min(y0, point.longitude); + y1 = max(y1, point.longitude); + } + + return LatLngBounds(northeast: LatLng(x1, y1), southwest: LatLng(x0, y0)); + } + + LatLng calculateCenter(List points) { + assert(points.isNotEmpty, 'The points list cannot be empty.'); + + final total = points.fold( + const LatLng(0.0, 0.0), + (LatLng acc, LatLng point) => LatLng( + acc.latitude + point.latitude, acc.longitude + point.longitude), + ); + + return LatLng( + total.latitude / points.length, total.longitude / points.length); + } +} diff --git a/lib/src/int_extensions.dart b/lib/src/int_extensions.dart new file mode 100644 index 0000000..b269fe1 --- /dev/null +++ b/lib/src/int_extensions.dart @@ -0,0 +1,13 @@ +import 'package:open_exts/open_exts.dart'; + +extension OpenIntExtensions on int { + String toBinary( + int len, { + int separateAtLength = 4, + String separator = ',', + }) => + toRadixString(2) + .padLeft(len, '0') + .splitByLength(separateAtLength) + .join(separator); +} diff --git a/lib/src/string_extensions.dart b/lib/src/string_extensions.dart index f6ffd3f..7fddb25 100644 --- a/lib/src/string_extensions.dart +++ b/lib/src/string_extensions.dart @@ -1,4 +1,4 @@ -extension StringExtension on String { +extension OpenStringExtension on String { String operator -(String rhs) => replaceAll(rhs, ''); String truncateTo(int maxLength) => diff --git a/pubspec.yaml b/pubspec.yaml index 6905778..4ad0d9a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: open_exts description: "Extensions. Its that simple..." -version: 0.0.1 +version: 0.0.3 homepage: http://reinemuth.rocks:3000/jens/open_exts publish_to: @@ -12,6 +12,7 @@ environment: dependencies: flutter: sdk: flutter + google_maps_flutter: ^2.9.0 dev_dependencies: flutter_test: