Skip to content

Instantly share code, notes, and snippets.

Avatar
🦞
Back at the home office

Luke Pighetti lukepighetti

🦞
Back at the home office
View GitHub Profile
@lukepighetti
lukepighetti / text_editing_controller_builder.dart
Last active Sep 14, 2020
Wrap any TextField with TextEditingControllerBuilder to make it declarative
View text_editing_controller_builder.dart
import 'package:flutter/widgets.dart';
class TextEditingControllerBuilder extends StatefulWidget {
/// Exposes a [TextEditingController] to the child, which allows
/// us to convert any [TextField] into a declarative version.
///
/// Typically used for wiring up many state fields to form inputs
/// and making sure everything stays in sync.
///
/// If [text] is updated, the consuming [TextField] will also be updated.
View table.dart
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
@lukepighetti
lukepighetti / unused_dependencies.js
Created Sep 4, 2020
Search a Flutter project for unused dependencies
View unused_dependencies.js
/// Finds unused dependencies from pubspec.yaml
///
/// Achieves this by parsing pubspec.yaml and recursively
/// searching the lib folder for an import statement that
/// contains the name of each package. Prints out the results.
const fs = require("fs");
const YAML = require("yaml");
const { execSync } = require("child_process");
/// Read pubspec.yaml
@lukepighetti
lukepighetti / ffs_clipping.dart
Created Jun 20, 2020
Just another day fighting flutter
View ffs_clipping.dart
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 / init_state.dart
Created Jun 7, 2020
Why does initState/dispose need to have a super call when they could be called by an internal method instead?
View init_state.dart
/// 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
View implicitly_animated_builder.dart
import 'package:flutter/material.dart';
/// An implicitly animated builder that tweens from 0.0 to 1.0 based on `isActive` property
class ImplicitlyAnimatedBuilder extends ImplicitlyAnimatedWidget {
ImplicitlyAnimatedBuilder({
Key key,
@required Curve curve,
@required Duration duration,
@required this.isActive,
@required this.builder,
@lukepighetti
lukepighetti / main.dart
Created May 15, 2020
Async constructor arguments, sequential or parallel?
View main.dart
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}");
}
View freezed_model.json
{
"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 / custom_loggers.dart
Last active Jun 20, 2020
Logging service using extensions for channels with multiple outputs
View custom_loggers.dart
part of 'logging_service.dart';
/// Log messages to the dart console
class ConsoleLogger implements LogConsumer {
@override
void log(LogMessage e) {
print("[${e.channel.toUpperCase()}]: ${e.message}");
}
}
@lukepighetti
lukepighetti / main.dart
Last active Mar 16, 2020
Extensions don't fuck anything up
View main.dart
// 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
You can’t perform that action at this time.