Skip to content

Instantly share code, notes, and snippets.

@crizant
crizant / theme.dart
Last active May 12, 2021 09:27
Flutter: handle custom theme properties in different brightness by extension method
import 'package:flutter/material.dart';
extension CustomColorScheme on ColorScheme {
Color get success => brightness == Brightness.light ? const Color(0xFF28a745) : const Color(0x2228a745);
}
@crizant
crizant / analysis_options.yaml
Created July 22, 2020 07:08
Flutter linting rules
analyzer:
errors:
# treat missing required parameters as a warning (not a hint)
missing_required_param: warning
# treat missing returns as a warning (not a hint)
missing_return: warning
linter:
rules:
- always_declare_return_types
@crizant
crizant / lefthook.yml
Last active November 16, 2020 02:08
Lefthook config file for flutter projects
pre-commit:
piped: true
commands:
linter:
run: flutter analyze lib
sort-imports:
glob: "*.dart"
run: flutter pub run import_sorter:main {staged_files} && git add {staged_files}
pretty:
glob: "*.dart"
@crizant
crizant / example.dart
Created April 27, 2020 12:32
dart_map_enhancer example
import 'package:map_enhancer/map_enhancer.dart';
void main() {
final Map peter = {
'name': {
'firstName': 'Peter',
'lastName': 'Petrelli',
},
'age': 29,
};
@crizant
crizant / result.csv
Created March 2, 2020 12:37
Flutter const constructor analysis (memory usage)
Metric(Memory usage)\No. of logo 10 100 1000
Capacity(Use const constructor) 7.7MB 9.96MB 27.3MB
Used(Use const constructor) 4.4MB 4.3MB 16.8MB
Capacity(Use non-const constructor) 7.7MB 10.9MB 29.4MB
Used(Use non-const constructor) 4.3MB 5.4MB 22.1MB
@crizant
crizant / result.csv
Created March 2, 2020 12:32
Flutter const constructor analysis (time to draw each frame)
Metric(Time to draw each frame)\No. of logo 10 100 1000
GPU (Use const constructor) 7.4ms 9.6ms 50ms
UI (Use const constructor) 4.8ms 10.6ms 78ms
GPU (Use non-const constructor) 7.4ms 9.6ms 56ms
UI (Use non-const constructor) 5.4ms 11.7ms 84.1ms
@crizant
crizant / main.dart
Created February 19, 2020 06:30
Flutter listen for pointer scroll events
Listener(
onPointerSignal: (PointerSignalEvent event) {
if (event is PointerScrollEvent) {
print('x: ${event.position.dx}, y: ${event.position.dy}');
print('scroll delta: ${event.scrollDelta}');
}
},
child: child,
)
@crizant
crizant / main.dart
Last active February 19, 2020 06:30
Flutter listen for mouse movement events
MouseRegion(
onEnter: (PointerEnterEvent event) {
print('x: ${event.position.dx}, y: ${event.position.dy}');
},
onHover: (PointerHoverEvent event) {
print('x: ${event.position.dx}, y: ${event.position.dy}');
},
onExit: (PointerExitEvent event) {
print('x: ${event.position.dx}, y: ${event.position.dy}');
},
@crizant
crizant / index.js
Created February 7, 2020 17:31
Node.js handle dynamoDb batch write limit
const R = require('ramda')
const aws = require('aws-sdk')
const batchWriteToDb = async (tableName, data) => {
const dynamoDb = new aws.DynamoDB({
maxRetries: 999
})
const docClient = new aws.DynamoDB.DocumentClient({
service: dynamoDb,
convertEmptyValues: true
})
@crizant
crizant / main.dart
Created February 7, 2020 17:27
Flutter conditional rendering - good practice
// Good
class MyWidget extends StatelessWidget {
final List<int> numbers = [];
@override
Widget build(BuildContext context) {
return Container(
child: Conditional.single(
context: context,
conditionBuilder: (BuildContext context) {