dart_conversion_utils

A lightweight Dart/Flutter package providing utility functions for conversions. It helps developers easily handle colors, dates, numbers, strings, and screen size conversions, making mobile app design faster and more consistent.


Features

  • Color conversions: hexToColor, colorToHex
  • Date conversions: formatDate, timestampToDate, dateToTimestamp
  • Number & string utilities: formatNumber, formatCurrency, capitalize
  • Screen size conversions: dpToPx, percentWidth, percentHeight

Installation

Add this to your pubspec.yaml:

dependencies:
  dart_conversion_utils:
    git:
      url: https://github.com/YOUR_USERNAME/dart_conversion_utils.git

Then fetch dependencies:

dart pub get

Or for Flutter:

flutter pub get

Usage

Import the package

import 'package:dart_conversion_utils/dart_conversion_utils.dart';

Color Utilities

hexToColor

Color myColor = hexToColor("#5CBF6D"); // Color(0xFF5CBF6D)
  • Converts a hex string to a Color object.
  • Adds full opacity if alpha is missing.

colorToHex

String hex = colorToHex(Colors.blue); // "#0000FF"
String hexWithAlpha = colorToHex(Colors.blue, includeAlpha: true); // "#FF0000FF"
  • Converts a Color object to a hex string.
  • Optional: include alpha channel.

Date Utilities

formatDate

String date = formatDate(DateTime.now()); // "2025-10-09"
String customDate = formatDate(DateTime.now(), separator: '/'); // "2025/10/09"

timestampToDate

DateTime date = timestampToDate(1739020000000);

dateToTimestamp

int timestamp = dateToTimestamp(DateTime.now());

Number & String Utilities

formatNumber

String formatted = formatNumber(1234567); // "1,234,567"

formatCurrency

String price = formatCurrency(19.99, symbol: '\$'); // "$19.99"

capitalize

String name = capitalize("flutter"); // "Flutter"

Screen Size Utilities

dpToPx

double buttonWidth = dpToPx(context, 120, baseWidth: 375);
  • Converts design pixels to logical pixels relative to current screen width.
  • baseWidth defaults to 375 (common iPhone design width).

percentWidth

double boxWidth = percentWidth(context, 50); // 50% of screen width

percentHeight

double boxHeight = percentHeight(context, 30); // 30% of screen height

Running Tests

dart test

Or for Flutter tests:

flutter test

Ensure all conversion methods work correctly across different screen sizes and values.


License

MIT © Arno Massart

Libraries

dart_conversion_utils
A Dart package providing common conversion utilities for Flutter app design, including size, color, date, and text/number format conversions.