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 / home.dart
Created August 14, 2019 13:09
Singletone services and provider
import 'service.dart';
class HomeScreen extends StatelessWidget {
@override
build(BuildContext context) {
// When service will notify its listeners, we'll be automatically
// rebuilt
List<Model> items = Provider.of<Service>(context).items;
return Scaffold(
@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
Last active July 17, 2024 21:26
Flutter: texture generator playground
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
void main() async => runApp(MaterialApp(home: Root()));
/// Waits till [ui.Image] is generated and renders
/// it using [CustomPaint] to render it. Allows use of [MediaQuery]
class Root extends StatelessWidget {
@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
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: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);