Skip to content

Instantly share code, notes, and snippets.

View lesnitsky's full-sized avatar
:octocat:
Coding

Andrei Lesnitsky lesnitsky

:octocat:
Coding
View GitHub Profile
final visited = <(int, int)>{};
Iterable<List<(int, int)>> getBiomes(List<List<int>> worldmap) sync* {
visited.clear();
for (int i = 0; i < worldmap.length; i++) {
for (int j = 0; j < worldmap[i].length; j++) {
if (visited.contains((i, j))) {
continue;
}
@lesnitsky
lesnitsky / guess-a-number-patterns.dart
Created July 12, 2023 13:53
Recursive guessing game with patterns
import 'dart:io';
import 'dart:math';
final rng = Random();
sealed class State {
const State();
}
class UninitializedState extends State {
class User {
final String name;
User({required this.name});
}
class SuperchargedUser extends User {
final String ability;
// no duplication of the "final String name;" here
SuperchargedUser({super.key, super.name, required this.ability});
@lesnitsky
lesnitsky / styled.dart
Created May 10, 2022 12:09
Styled widget concept
part 'my_widget.styled.dart'; // generated code here
// below is what user writes
@Style()
// ^^^^ provided by the library
class WidgetStyle {
const WidgetStyle();
final EdgeInsets padding => const EdgeInsets.all(8.0);
class Comparable {
final String content;
Comparable(this.content);
@override
bool operator ==(other) {
print('==');
return other is Comparable && other.hashCode == hashCode;
}
@lesnitsky
lesnitsky / email_form.dart
Created February 2, 2022 15:27
Email form
class EmailForm extends StatelessWidget {
final VoidCallback onSubmitted;
final TextEditingController emailCtrl;
final TextEditingController passwordCtrl;
const EmailForm({
Key? key,
required this.onSubmitted,
required this.emailCtrl,
required this.passwordCtrl,
@lesnitsky
lesnitsky / main.dart
Last active October 29, 2021 16:00
wrappers registration + redux
void main() {
final store = Store<int>(counterReducer, initialState: 0);
Flutter.registerWrapperWidget(StoreProvider(store: store)); // proposed API
runApp();
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
@lesnitsky
lesnitsky / main.dart
Created October 29, 2021 15:58
AppModel + redux example
final store = Store<int>(counterReducer, initialState: 0);
void main() {
runApp();
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class AppModel extends InheritedWidget {
const AppModel({
Key? key,
required Widget child,
}) : super(key: key, child: child);
static V get<K, V>(BuildContext context, K key) {
@lesnitsky
lesnitsky / runtime_type_key.dart
Created September 30, 2021 12:51
runtimeType key
class Storage<T> {
final _internal = <Type, T>{};
void add(T value, [Type? type]) {
_internal[type ?? T] = value;
}
T get<T>() {
return _internal[T] as T;
}