Skip to content

Instantly share code, notes, and snippets.

@luo3house
luo3house / multi_taps_to_pan_gesture_recognizer.dart
Last active November 15, 2023 01:48
Flutter: Gesture recognizer hanldes multiple taps, then draggable, useful for map dragging, image scaling (with example)
class MultiTapsToPanGestureRecognizer extends TapAndPanGestureRecognizer {
var minTapsToDrag = 1;
var timeoutToTaps = const Duration(milliseconds: 200);
@protected
var canDrag = false;
@protected
var firedDrag = false;
@protected
@luo3house
luo3house / snap_value_with_min_max_step.dart
Last active November 15, 2023 01:38
snap value by min, max, step
import 'package:flutter/foundation.dart';
// V0.85, S0.1 -> 0.9
// V0.84, S0.1 -> 0.8
// V0.48, S1.0 -> 0
// V4.80, S5.0 -> 5
// V6.10, S3.0 -> 6
double snapValue(double value, double min, double max, double step) {
final wideValue = () {
if (step == 0) return value; // no restricted
@luo3house
luo3house / token_store.dart
Created April 26, 2023 13:01
Easy to manage access tokens and refresh tokens
class VariableTokenStore<Token> {
/// validate token is valid
/// - true = ok
/// - false = not ok
/// - neverthrow
final Future<bool> Function(Token token) checkToken;
/// fetch a new token
/// - null = require authenticate
/// - not null = ok
@luo3house
luo3house / interceptor.dart
Last active February 21, 2024 01:48
Interceptor Pattern
typedef Middleware<C> = dynamic Function(C context, Function() next);
class Interceptor<C> {
static _noop() {}
final List<Middleware<C>> middlewares = List.empty(growable: true);
Future<void> call<T>(C context, [Function() fn = _noop]) async {
if (middlewares.isEmpty) return fn();
return middlewares.reduce((pre, cur) {
@luo3house
luo3house / lru_linked_map.dart
Last active April 20, 2023 10:19
LRU cache: Circular linked map
/// if true, will pop the last
typedef LRUPolicyTest<K, V> = bool Function(LRULinkedMap lru, _Node<K, V> item);
class _Node<K, V> {
late K key;
late V value;
_Node<K, V>? prev;
_Node<K, V>? next;
}
@luo3house
luo3house / i18n-translation.dart
Last active April 26, 2023 06:45
Simple i18n strings mapper
class Translation {
// "zh-a-b-c" -> m"-c"
static final _localeSuffixRE = RegExp(r"-[^-]+$");
final bool localeCaseSensitive;
var _currentLocale = "en";
var _fallbackLocale = "en";
Map<String, Map<String, String>> dict = Map();
Translation({
@luo3house
luo3house / TouchOpacity.dart
Last active April 12, 2023 08:17
Flutter Widget: TouchOpacity
import 'package:flutter/widgets.dart';
class TouchOpacity extends StatefulWidget {
final Widget child;
final void Function()? onTap;
const TouchOpacity({
super.key,
this.child = const SizedBox(),
this.onTap,
@luo3house
luo3house / avd.yaml
Created March 21, 2023 14:03
Clash Proxy within AVD
port: 7890
socks-port: 7891
allow-lan: true
mode: Global
log-level: info
external-controller: 127.0.0.1:9090
# https://developer.android.com/studio/run/emulator-networking?hl=zh-cn#networkaddresses
proxies:
- {name: "AVD Host", desc: "(10.0.2.2)", server: 10.0.2.2, port: 7890, type: socks5 }
@luo3house
luo3house / useObserver.tsx
Created October 10, 2022 03:36
mobx-react-lite: useObserver completely follow component effects
import { getDependencyTree, Reaction } from "mobx"
import React, { useRef, useState, useLayoutEffect } from "react"
import { isUsingStaticRendering } from "./staticRendering"
import { printDebugValue } from "./utils/printDebugValue"
function observerComponentNameFor(baseComponentName: string) {
return `observer${baseComponentName}`
}
@luo3house
luo3house / server.js
Created August 2, 2022 09:26
HTTPS 反代本地开发环境,避免线上后端 CORS
// Step 1: 修改 /etc/hosts,把 CORS 合法域名指向本机
// Step 2: 当前目录准备该域名的证书 tls.crt
// Step 3: 当前目录准备该域名的证书 tls.key
// Step 4: 修改 TARGET 为本地开发环境的 URL
// Step 5: node server.js
var http = require('http'), https = require('https'), fs = require('fs')
var TARGET = 'http://localhost:8080'
var PORT = 443