Skip to content

Instantly share code, notes, and snippets.

@Kavantix
Kavantix / simple_isolate.dart
Last active February 3, 2022 13:38
Simple dart 2.15 compute implementation
import 'dart:async';
import 'dart:isolate';
Future<R> compute<I, R>(R Function(I) f, I input,
{String debugName = ''}) async {
final resultPort = ReceivePort();
final args = _ComputeArgs<I, R>(resultPort.sendPort, f, input);
final isolate = await Isolate.spawn<_ComputeArgs<I, R>>(
_compute,
args,
@Kavantix
Kavantix / mapped_list_view.dart
Created October 29, 2021 09:13
Dart view of a list with a map function
import 'dart:collection';
abstract class MappedListView<T, R> implements List<R> {
factory MappedListView(
List<T> wrapped,
R Function(T element) mapper,
) = _MappedListView<T, R>;
}
class _MappedListView<T, R> with ListMixin<R> implements List<R>, MappedListView<T, R> {
@Kavantix
Kavantix / limiting_scroll_physics.dart
Created July 29, 2020 20:12
Scroll page physics that only scrolls to next pages
class LimitingScrollPhysics extends PageScrollPhysics {
LimitingScrollPhysics({ScrollPhysics parent, @required this.controller})
: super(parent: parent);
final ScrollController controller;
@override
LimitingScrollPhysics applyTo(ScrollPhysics ancestor) {
return LimitingScrollPhysics(
parent: buildParent(ancestor), controller: controller);
@Kavantix
Kavantix / page_aligned_scroll_physics.dart
Last active May 7, 2020 15:12
Flutter Scroll Physics that always ends on a whole page
class PageAlignedScrollPhysics extends ScrollPhysics {
final double pageSize;
PageAlignedScrollPhysics({@required this.pageSize, ScrollPhysics parent}) : super(parent: parent);
@override
PageAlignedScrollPhysics applyTo(ScrollPhysics ancestor) {
return PageAlignedScrollPhysics(pageSize: pageSize, parent: buildParent(ancestor));
}