Skip to content

Instantly share code, notes, and snippets.

View lukepighetti's full-sized avatar
🦞

Luke Pighetti lukepighetti

🦞
View GitHub Profile
@lukepighetti
lukepighetti / extensions.dart
Created February 12, 2020 18:35
Quick example of what an extensions file might look like in one of my projects
import 'package:email_validator/email_validator.dart';
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as material;
import 'package:intl/intl.dart';
import 'package:rxdart/rxdart.dart';
extension BuildContextExtensions on BuildContext {
dynamic get arguments => ModalRoute.of(this).settings.arguments;
@lukepighetti
lukepighetti / styles.dart
Last active March 4, 2020 01:58
Extensions to get a project started
import 'package:flutter/material.dart';
class AppStyle {
static double get paddingUnit => 16;
static TextStyle get text28 => TextStyle(fontSize: 28).withTightSpacing;
static TextStyle get text20 => TextStyle(fontSize: 20).withTightSpacing;
static TextStyle get text16 => TextStyle(fontSize: 16).withNormalSpacing;
static TextStyle get text14 => TextStyle(fontSize: 14).withNormalSpacing;
static TextStyle get text12 => TextStyle(fontSize: 12).withWideSpacing;
@lukepighetti
lukepighetti / blockchain.dart
Created March 5, 2020 19:37
A dead simple blockchain written in Dart in 93 lines
import 'dart:collection';
import 'dart:convert' as convert;
import 'package:crypto/crypto.dart' as crypto;
import 'package:flutter_test/flutter_test.dart';
main() {
testWidgets('Create, add, pass validate, tamper, fail validation', (_) async {
final blockchain = Blockchain();
expect(blockchain.isValid, isTrue);
@lukepighetti
lukepighetti / loading_notification_listener_route.dart
Last active March 8, 2020 01:31
A declarative™ way to handle loading notifications with Widgets™
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:material_theme/loading_notification/loading_image.dart';
import 'package:material_theme/loading_notification/loading_notification_listener.dart';
import 'package:material_theme/widgets/layout/default_scaffold.dart';
import 'loading_future_builder.dart';
class LoadingNotificationsRoute extends StatefulWidget {
@lukepighetti
lukepighetti / main.dart
Last active March 16, 2020 19:33
Extensions don't fuck anything up
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
@lukepighetti
lukepighetti / freezed_model.json
Created April 28, 2020 22:10
VSCode snippets
{
"Freezed model": {
"prefix": "frz",
"body": [
"import 'package:freezed_annotation/freezed_annotation.dart';",
"",
"part '$TM_FILENAME_BASE.freezed.dart';",
"",
"@freezed",
"abstract class ${1:${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g}} with _$${1} {",
@lukepighetti
lukepighetti / main.dart
Created May 15, 2020 01:40
Async constructor arguments, sequential or parallel?
main() async {
print("Start");
var s = Stopwatch()..start();
/// It would be nice if getFoo() and getBar() were evaluated in parallel,
/// as if they were performed with Future.wait()
final result = Foo(await getFoo(), await getBar());
print("Stop: ${s.elapsed}");
}
@lukepighetti
lukepighetti / init_state.dart
Created June 7, 2020 15:54
Why does initState/dispose need to have a super call when they could be called by an internal method instead?
/// Alternative architecture for Flutter lifecycle methods.
///
/// Currently the framework uses @mustCallSuper to ensure that
/// important operations are called when lifecycle methods are
/// overridden. However, it is a common source of confusion for
/// developers about if the super should be called before or after
/// their own code.
///
/// This is a proposed alternative which has an internal `handleLifecycleMethod()`
/// method for each lifecycle event which calls `lifecycleMethod()` in the appropriate
@lukepighetti
lukepighetti / ffs_clipping.dart
Created June 20, 2020 16:33
Just another day fighting flutter
import 'package:flutter/material.dart';
import 'package:flutter_sandbox/widgets/layout/default_scaffold.dart';
import 'package:flutter_sandbox/themes/text_themes.dart';
class SandboxRoute extends StatefulWidget {
@override
_SandboxRouteState createState() => _SandboxRouteState();
}
class _SandboxRouteState extends State<SandboxRoute> {
@lukepighetti
lukepighetti / table.dart
Last active September 9, 2020 18:23
Table formatting helper for console outputs https://twitter.com/luke_pighetti/status/1303736288315572224
import 'dart:math';
class Table {
/// Example output
///
/// ```
/// Range (yd) 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
/// Drop (in) -0.51, 0.06, 0.23, -0.03, -0.71, -1.84, -3.42, -5.46, -7.98, -10.99
/// Drop (moa) 4.78, -0.31, -0.73, 0.06, 1.35, 2.92, 4.65, 6.51, 8.45, 10.48
/// Windage (in) 0.03, 0.05, 0.09, 0.15, 0.22, 0.31, 0.41, 0.53, 0.66, 0.81