responsive<T> method
T
responsive<T>({
- required T mobile,
- T? tablet,
- T? desktop,
Picks a value by device class, with fallback chaining toward the smaller class.
mobile is required; tablet and desktop are optional and inherit
from the smaller class when omitted:
- DeviceSize.desktop →
desktop ?? tablet ?? mobile - DeviceSize.tablet →
tablet ?? mobile - DeviceSize.mobile →
mobile
This intentionally replaces most ResponsiveValue "larger than" usage:
"the tablet value applies to tablet and larger." For example,
context.responsive(mobile: a, desktop: c) returns a on a tablet (the
omitted tablet inherits from mobile — it does not skip ahead to
desktop) and c on a desktop.
Caveat (nullable T): because an omitted tablet / desktop inherits
from the smaller class, do not use a nullable T expecting an
explicit null to be returned for an omitted class — there is no way to
distinguish "omitted" from "explicitly null."
Implementation
T responsive<T>({required T mobile, T? tablet, T? desktop}) {
switch (deviceSize) {
case DeviceSize.desktop:
return desktop ?? tablet ?? mobile;
case DeviceSize.tablet:
return tablet ?? mobile;
case DeviceSize.mobile:
return mobile;
}
}