Added int, gmaps and context extensions
This commit is contained in:
parent
b982cfe7fd
commit
fad2c2e73b
8 changed files with 108 additions and 11 deletions
|
@ -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';
|
||||
|
|
|
@ -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('#', ''));
|
||||
|
|
43
lib/src/context_extensions.dart
Normal file
43
lib/src/context_extensions.dart
Normal file
|
@ -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);
|
||||
}
|
|
@ -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 &&
|
||||
|
|
38
lib/src/google_maps_extensions.dart
Normal file
38
lib/src/google_maps_extensions.dart
Normal file
|
@ -0,0 +1,38 @@
|
|||
import 'dart:math';
|
||||
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
|
||||
extension OpenGoogleMapsExtensions on GoogleMap {
|
||||
LatLngBounds getBoundsFromLatLngList(List<LatLng> 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<LatLng> 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);
|
||||
}
|
||||
}
|
13
lib/src/int_extensions.dart
Normal file
13
lib/src/int_extensions.dart
Normal file
|
@ -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);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
extension StringExtension on String {
|
||||
extension OpenStringExtension on String {
|
||||
String operator -(String rhs) => replaceAll(rhs, '');
|
||||
|
||||
String truncateTo(int maxLength) =>
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue