Skip to content

Instantly share code, notes, and snippets.

View vovahost's full-sized avatar

Vlad vovahost

  • London
View GitHub Profile
@vovahost
vovahost / gist:b1444227632ab9b4427497490a2e92ab
Created November 19, 2022 18:18
Clickable Compose Text made simple
@Composable
fun AnnotatedClickableText() {
val termsUrl = "https://example.com/terms"
val privacyUrl = "https://example.com/privacy"
val annotatedText = buildAnnotatedString {
append("You agree to our ")
withStyle(style = SpanStyle(color = Color.Blue, fontWeight = FontWeight.Bold)) {
appendLink("Terms of Use", termsUrl)
}
append(" and ")
import java.util.*
class LRUCache<K, T>(private val capacity: Int) {
private val map: LinkedHashMap<K, T> = LinkedHashMap()
operator fun get(key: K): T? {
val value = map[key]
if (value != null) {
this[key] = value
}
@vovahost
vovahost / DimensionDouble.kt
Created November 10, 2020 23:58
Java Dimension2D with constructor with double parameters
import java.awt.geom.Dimension2D
data class DimensionDouble(private var width: Double, private var height: Double) : Dimension2D() {
override fun getWidth(): Double {
return width
}
override fun getHeight(): Double {
return height
object HttpStatus {
const val CONTINUE = 100
const val SWITCHING_PROTOCOLS = 101
const val PROCESSING = 102
const val OK = 200
const val CREATED = 201
const val ACCEPTED = 202
const val NON_AUTHORITATIVE_INFORMATION = 203
const val NO_CONTENT = 204
@vovahost
vovahost / tcp-vertx-eventbus.dart
Created March 2, 2020 11:56
tcp-vertx-eventbus dart port [Work in progress]
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'package:web_socket_channel/io.dart';
import 'package:uuid/uuid.dart';
final uuid = Uuid();
//var net = require('net');
@vovahost
vovahost / main.dart
Created January 11, 2020 06:30
Listen for widget size changes.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
/// There's an alternative and better way to achieve this by using RenderObject / CustomMultiChildLayout but I will not cover it here.
class MyTextInput extends StatefulWidget {
@override
_MyTextInputState createState() => _MyTextInputState();
@vovahost
vovahost / main.dart
Created December 11, 2019 22:56
Example of a TextField without any padding.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'TextField without padding',
debugShowCheckedModeBanner: false,
@vovahost
vovahost / main.dart
Created November 14, 2019 10:45
Get widget's absolute coordinates on a screen in Flutter
// You can use this extension I wrote (requires Dart 2.6):
extension GlobalKeyEx on GlobalKey {
Rect get globalPaintBounds {
final renderObject = currentContext?.findRenderObject();
var translation = renderObject?.getTransformTo(null)?.getTranslation();
if (translation != null && renderObject.paintBounds != null) {
return renderObject.paintBounds
.shift(Offset(translation.x, translation.y));
} else {
@vovahost
vovahost / main.dart
Last active October 14, 2021 06:13
Example of Enum with fields and methods implemented using Extensions. This example uses a constant list instead of switch.
main(List<String> arguments) {
final cat = Cat.white;
print('cat name: ${cat.name}');
cat.talk();
}
enum Cat {
black,
white,
@vovahost
vovahost / main.dart
Created February 27, 2019 14:20
CancelableOperation and CancelableCompleter usage example
import 'package:async/async.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test("CancelableOperation with future", () async {
var cancellableOperation = CancelableOperation.fromFuture(
Future.value('future result'),
onCancel: () => {print('onCancel')},
);