Skip to content

Instantly share code, notes, and snippets.

@Zujaj
Created May 19, 2022 10:48
Show Gist options
  • Save Zujaj/cd4fd0d21ff37f80683d10b2dee0000f to your computer and use it in GitHub Desktop.
Save Zujaj/cd4fd0d21ff37f80683d10b2dee0000f to your computer and use it in GitHub Desktop.
The Util Class.
import 'dart:convert';
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;
import 'dart:ui';
import 'package:flutter/services.dart';
/// A class that handles basic utility functions.
class Util {
/// Prevents From Object Instantiation.
Util._();
/// Formats the given [input] to hex value & copies it to the [Clipboard].
/// print(copyToClipboard('#f7ebcb')); //F7EBCB
static Future<void> copyToClipboard(String input) async {
String textToCopy = input.replaceFirst('#', '').toUpperCase();
if (textToCopy.startsWith('FF') && textToCopy.length == 8) {
textToCopy = textToCopy.replaceFirst('FF', '');
}
await Clipboard.setData(ClipboardData(text: '#$textToCopy'));
}
/// Converts the [Color] object to Hex Formatted String.
/// const color = Color(0xFFFF00FF);
/// print(colorToHexString(color)); //'#FF00FF'
static String colorToHexString(Color color) =>
'#${color.value.toRadixString(16).substring(2)}';
/// Works for `Web` only.
static Future<void> saveAsSVG(String svgCode) async {
html.AnchorElement()
..href =
'${Uri.dataFromString(svgCode, mimeType: 'image/svg+xml', encoding: utf8)}'
..download = 'result.svg'
..style.display = 'none'
..click();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment