Skip to content

Instantly share code, notes, and snippets.

View tomaszpolanski's full-sized avatar
💭
Fluttering

Tomek Polański tomaszpolanski

💭
Fluttering
View GitHub Profile
@tomaszpolanski
tomaszpolanski / sliver_floating_header.dart
Created November 20, 2019 10:21
Sliver that scrolls off like SliverPersistentHeader but takes it size from it's child
import 'dart:math' as math;
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class SliverFloatingHeader extends SingleChildRenderObjectWidget {
const SliverFloatingHeader({
Key key,
Widget child,
}) : super(key: key, child: child);
@tomaszpolanski
tomaszpolanski / analysis_options.yaml
Last active November 1, 2023 00:04
Strict Flutter lint rules
# Check https://dart-lang.github.io/linter/lints/ for lint rules documentation
# Update from http://dart-lang.github.io/linter/lints/options/options.html
analyzer:
errors:
missing_required_param: error
missing_return: error
todo: ignore
language:
strict-inference: true
@tomaszpolanski
tomaszpolanski / performance-test.dart
Created February 9, 2019 09:38
Elements, Keys and Flutter's performance
import 'package:flutter/material.dart';
void main() {
// Displays build timeline
debugProfileBuildsEnabled = true;
runApp(GlobalKeyWidget());
}
class WidgetsWithKeysAreJustUpdated extends StatefulWidget {
@tomaszpolanski
tomaszpolanski / Variance.kt
Created July 3, 2017 16:07
Covariance vs Contravariance
package com.tomek
abstract class Animal(val size: Int)
class Dog(val cuteness: Int): Animal(100)
class Spider(val terrorFactor: Int): Animal(1)
// Covariance
@tomaszpolanski
tomaszpolanski / Comparison.txt
Last active October 25, 2022 21:29
Kotlin Standard comparison
╔══════════╦═════════════════╦═══════════════╦═══════════════╗
║ Function ║ Receiver (this) ║ Argument (it) ║ Result ║
╠══════════╬═════════════════╬═══════════════╬═══════════════╣
║ let ║ this@MyClass ║ String("...") ║ Int(42) ║
║ run ║ String("...") ║ N\A ║ Int(42) ║
║ run* ║ this@MyClass ║ N\A ║ Int(42) ║
║ with* ║ String("...") ║ N\A ║ Int(42) ║
║ apply ║ String("...") ║ N\A ║ String("...") ║
║ also ║ this@MyClass ║ String("...") ║ String("...") ║
╚══════════╩═════════════════╩═══════════════╩═══════════════╝
@tomaszpolanski
tomaszpolanski / Movie.kt
Last active June 28, 2022 21:04
Parcelize testing
import android.annotation.SuppressLint
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@SuppressLint("ParcelCreator") // IntelliJ Issue https://youtrack.jetbrains.com/issue/KT-19300
@Parcelize
data class Movie(val title: String) : Parcelable
@tomaszpolanski
tomaszpolanski / main_safe.dart
Last active November 9, 2021 17:57
Null safe
final String? text;
@override
Widget build(BuildContext context) {
final _text = text; // Assign field to local final variable
return Column(
children: [
if (_text != null) Text(_text), // The `!` is not longer needed
],
);
@tomaszpolanski
tomaszpolanski / fields_error.dart
Last active November 7, 2021 11:31
Error when accessing
void main() {
final A example = B();
if (example.text != null) {
print(example.text!.length); // Throws null reference exception
}
}
@tomaszpolanski
tomaszpolanski / fields.dart
Created November 7, 2021 11:13
Not truly final fields
class A {
A(this.text);
final String? text; // Final field
}
class B implements A {
bool _first = true;
@override
@tomaszpolanski
tomaszpolanski / main_non_safe.dart
Created November 7, 2021 11:11
Not null safe code
final String? text;
@override
Widget build(BuildContext context) {
return Column(
children: [
if (text != null) Text(text!), // Won't compile without `!` in `text!`
],
);
}