consumeWith method
double
consumeWith(
- double available,
- NestedScrollPosition position,
- NestedScrollConsumer selfConsumer,
- NestedScrollConsumer ancestorConsumer,
Implementation
double consumeWith(
double available,
NestedScrollPosition position,
NestedScrollConsumer selfConsumer,
NestedScrollConsumer ancestorConsumer,
) {
final List<NestedScrollConnection> delegators = findSelfAndAncestorWidgets();
// Current consumed amount from available.
double consumed = 0;
// Returns the remaining offset after consumption.
double remained() => available - consumed;
for (final delegator in delegators) {
if (delegator.predicate?.call(available, position) ?? false) {
consumed += selfConsumer(delegator)?.call(remained(), position) ?? 0.0;
// When the available offset has been fully consumed.
if ((consumed - available).abs() < precisionErrorTolerance) {
return consumed;
}
}
}
if (widget.propagation == .selfFirst) {
consumed += selfConsumer(widget)?.call(remained(), position) ?? 0.0;
// When the available offset is not fully consumed,
// delegate the remainder to the ancestor.
if ((consumed - available).abs() > precisionErrorTolerance) {
consumed += ancestorConsumer(widget)?.call(remained(), position) ?? 0.0;
}
} else if (widget.propagation == .deferToAncestor) {
consumed += ancestorConsumer(widget)?.call(remained(), position) ?? 0.0;
// When the available offset is not fully consumed,
// delegate the remainder to the ancestor.
if ((consumed - available).abs() > precisionErrorTolerance) {
consumed += selfConsumer(widget)?.call(remained(), position) ?? 0.0;
}
} else {
final prioritized = List<NestedScrollConnection>.from(delegators)
..sort((a, b) {
int priority(NestedScrollConnection c) {
return switch (c.propagation) {
.selfFirst => 0,
.directional => 1,
.deferToAncestor => 2,
};
}
return priority(a).compareTo(priority(b));
});
final targets = available > 0 ? prioritized : prioritized.reversed;
for (final it in targets) {
consumed += selfConsumer(it)?.call(remained(), position) ?? 0;
// If when all consumed, stops the travel.
if ((consumed - available).abs() < precisionErrorTolerance) {
break;
}
}
}
return consumed;
}