okane/lib/ui/utils.dart

82 lines
1.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:okane/screen.dart';
Future<T?> showDialogOrModal<T>({
required BuildContext context,
required WidgetBuilder builder,
bool showDragHandle = true,
}) {
final screenSize = getScreenSize(context);
final width = MediaQuery.sizeOf(context).shortestSide;
return switch (screenSize) {
ScreenSize.small => showModalBottomSheet<T>(
context: context,
showDragHandle: showDragHandle,
builder:
(context) => Padding(
padding: EdgeInsets.only(bottom: 32),
child: builder(context),
),
),
ScreenSize.normal => showDialog<T>(
context: context,
builder:
(context) => Dialog(
child: Container(
constraints: BoxConstraints(maxWidth: width * 0.7),
child: Padding(
padding: EdgeInsets.only(bottom: 32),
child: builder(context),
),
),
),
),
};
}
DateTime toMidnight(DateTime t) {
return DateTime(t.year, t.month, t.day);
}
String zeroPad(int i) {
if (i <= 9) {
return "0$i";
}
return i.toString();
}
String formatDateTime(DateTime dt, {bool formatYear = true}) {
if (!formatYear) {
return "${zeroPad(dt.day)}.${zeroPad(dt.month)}";
}
return "${zeroPad(dt.day)}.${zeroPad(dt.month)}.${zeroPad(dt.year)}";
}
Color colorHash(String text) {
final hue =
text.characters
.map((c) => c.codeUnitAt(0).toDouble())
.reduce((acc, c) => c + ((acc.toInt() << 5) - acc)) %
360;
return HSVColor.fromAHSV(1, hue, 0.5, 0.5).toColor();
}
String formatCurrency(double amount, {bool precise = true}) {
if (!precise) {
return "${amount.toInt()}";
}
return "${amount.toStringAsFixed(2)}";
}
DateTime monthEnding(DateTime now) {
return DateTime(
now.year,
now.month,
32,
23,
59,
59,
);
}