Skip to content

Instantly share code, notes, and snippets.

@kranfix
kranfix / drawer_with_rotated_body.dart
Last active April 5, 2021 23:03
An animated drawer with rotated body based on https://www.youtube.com/watch?v=ZHRhSFXqHJY
// based on: https://www.youtube.com/watch?v=ZHRhSFXqHJY
// but better modularied
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@kranfix
kranfix / riverpod_widget_reference_migration.dart
Last active June 20, 2021 15:51
Tip for prepare your Flutter app for the new WidgetReference in RiverPod
/// Temporal file for migrating to [WidgetRef] syntax in the
/// becoming `riverpod` version.
///
/// See the new RFC and the implementation (WIP)
/// - RFC: https://github.com/rrousselGit/river_pod/issues/335
/// - PR: https://github.com/rrousselGit/river_pod/pull/462
///
/// Medum article about the usage: https://link.medium.com/4F5gdDISXfb
library temporal_widget_ref_migration;
@kranfix
kranfix / const_vs_final.dart
Last active January 9, 2022 16:07
const memory optimization in Dart
void main() {
final obj1 = MyComplexObject(1, 2.0);
final obj2 = MyComplexObject(1, 2.0);
assert(!identical(obj1, obj2)); // obj1 and obj2 are not the same object
const cobj1 = MyComplexObject(1, 2.0);
const cobj2 = MyComplexObject(1, 2.0);
assert(identical(cobj1, cobj2)); // cobj1 and cobj2 are the same object
assert(!identical(cobj1, obj1)); // cobj1 and obj1 are not the same object
}
@kranfix
kranfix / var_problem.dart
Last active January 9, 2022 17:24
Problem using `var` in Dart
void main() {
final list = <int>[1, 2, 3, 4, 2, -1, 5];
final maxValue = 12;
var sum = 0;
for(final val in list) {
sum += val;
}
if(sum > maxValue) {
sum = maxValue;
@kranfix
kranfix / var_problem_extension_solution.dart
Last active January 9, 2022 17:23
Solution to var problem using extension methods in Dart
void main() {
final list = <int>[1, 2, 3, 4, 2, -1, 5];
final maxValue = 12;
final sum = list.clampedSum(maxValue);
/// ... more code that uses `sum` ...
}
extension ClampedAdder on List<int> {
@kranfix
kranfix / var_problem_closure_solution.dart
Last active January 9, 2022 17:23
Solution to var problem using a closure in Dart
void main() {
final list = <int>[1, 2, 3, 4, 2, -1, 5];
final maxValue = 12;
final sum = (){
var sum = 0;
for(final val in list) {
sum += val;
}
if(sum > maxValue) {
@kranfix
kranfix / var_problem_late_final_solution.dart
Last active January 9, 2022 17:18
late final solution to the var problem in Dart
void main() {
final list = <int>[1, 2, 3, 4, 2, -1, 5];
final maxValue = 12;
late final int sum;
{
var _sum = 0;
for(final val in list) {
_sum += val;
}
@kranfix
kranfix / many_fibos.dart
Created January 11, 2022 01:30
Playing with fibonacci
void main() {
print(fibo(3));
print(fibo(5));
print(fibo(13));
print(fibo(8));
}
// TODO: refactorizar usando cache
int fibox(int n) {
@kranfix
kranfix / riverpod_provider_doesnt_store_state.dart
Created April 6, 2024 16:30
Riverpod's provider doesn't store the state
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() => runApp(ProviderScope(child: const MyApp()));
final counterProvider = StateProvider<int>((ref) {
//final unsub = someStream.subscribe((val) => ref.state = va;);
//ref.onDispose(() => unsub());
return 0;
});
void main() {
final email1 = Email.maybeFrom("frank@moreno");
final email2 = Email.maybeFrom("[email protected]");
print(email1);
print(email2);
}
bool isValidEmail(String email) {
// Regular expression to validate the format of an email