Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View av's full-sized avatar
💻
🌚

Ivan Charapanau av

💻
🌚
View GitHub Profile
@av
av / main.dart
Created August 14, 2019 12:34
Async app init
// ...
class _RootState extends State<RootWidget> {
TenderApiProvider tenderApi = TenderApiProvider();
AppState state = AppState();
Future init() async {
return Future.wait(<Future>[
tenderApi.init(), // fetch and store token
Future.delayed(const Duration(milliseconds: 2000)) // Let user see the splash for couple of seconds
@av
av / record-package-names.js
Created September 8, 2019 13:42
Pub.dev package name distribution
const puppeteer = require('puppeteer');
const alphabet = 'poiuytrewqlkjhgfdsamnbvcxz'.split('');
(async () => {
let results = {};
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
const page = await browser.newPage();
await page.setViewport({ width: 1024, height: 768 });
@av
av / main.dart
Created September 18, 2019 14:14
Grid gap rendering
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Grid gaps',
theme: ThemeData(
@av
av / main.dart
Created September 23, 2019 08:30
Dart: instantiate Image object
// ...
var codec = await instantiateImageCodec(byteData);
var frameInfo = await codec.getNextFrame();
var img = frameInfo.image;
// ...
@av
av / main.dart
Created September 26, 2019 18:35
Flutter: texture generator, uv coordinate space
/// Very useful package which is part of Flutter.
/// Also contains a useful functions, such as [mix] and [smoothStep].
import 'package:vector_math/vector_math.dart';
/// ...
/// Main area of interest, this function will
/// return color for each particular color on our [ui.Image]
int generatePixel(int x, int y, Size size) {
/// Compute unified vector, values of its components
@av
av / main.dart
Created September 26, 2019 19:33
Flutter: texture generator, white dots
// ...
/// White dots each 20 pixels
if (x % 20 == 0 && y % 20 == 0) {
return 0xffffffff;
}
// ...
@av
av / main.dart
Created September 29, 2019 10:21
Flutter: x-axis gradient
/// Main area of interest, this function will
/// return color for each particular color on our [ui.Image]
int generatePixel(int x, int y, Size size) {
/// Compute unified vector, values of its components
/// will be between 0 and 1
var uv = Vector2(x / size.width, y / size.height);
/// [Vector2.xxx] is a getter which returns [Vector3]
/// with first component of given [Vector2], uv in our case.
return toColorInt(uv.xxx);
@av
av / main.dart
Last active September 29, 2019 10:37
Flutter: vector3 to rgba int color
/// Takes a unit Vector3 (all values from 0 to 1)
/// and returns an int representing color in RGBA format
/// Vector3(0, 1, 0) -> 0xff00ff00
int toColorInt(Vector3 vec) {
int r = (vec.r * 255).toInt();
int g = (vec.g * 255).toInt();
int b = (vec.b * 255).toInt();
return (b << 0) | (g << 8) | (r << 16) | (255 << 32);
}
@av
av / main.dart
Created September 29, 2019 10:54
Flutter: gradient stripes over x-axis
/// Main area of interest, this function will
/// return color for each particular color on our [ui.Image]
int generatePixel(int x, int y, Size size) {
/// Compute unified vector, values of its components
/// will be between 0 and 1
var uv = Vector2(x / size.width, y / size.height);
var xTiles = 10.0;
/// First of all, we scale uv.xxx ten times:
/// 0..1 * 10 -> 0..10
@av
av / main.dart
Last active September 29, 2019 11:03
Flutter: frac for Vector3
/// Returns a fractional part of given [Vector3]
/// Vector3(1.2, 1.5, 0.6) => Vector3(.2, .5, .6);
Vector3 frac3(Vector3 vec) {
return Vector3(
vec.x - vec.x.floor(),
vec.y - vec.y.floor(),
vec.z - vec.z.floor(),
);
}