ResponsiveItem<R> function

R ResponsiveItem<R>(
  1. BuildContext context, {
  2. ResponsiveExpression<R>? mobile,
  3. ResponsiveExpression<R>? tablet,
  4. ResponsiveExpression<R>? desktop,
  5. R? defaultValue,
})

Implementation

R ResponsiveItem<R>(
  BuildContext context, {
  ResponsiveExpression<R>? mobile,
  ResponsiveExpression<R>? tablet,
  ResponsiveExpression<R>? desktop,
  R? defaultValue,
}) {
  if (mobile == null && desktop == null && tablet == null) {
    return defaultValue as R;
  }

  final layoutInfo = sunny.get<LayoutInfo>(context: context);
  switch (layoutInfo.screenType) {
    case DeviceScreenType.mobile:
      return mobile != null ? mobile(context) : defaultValue as R;
    case DeviceScreenType.tablet:
      final fn = tablet ?? desktop;
      return fn != null ? fn(context) : defaultValue as R;
    case DeviceScreenType.desktop:
      return desktop != null ? desktop(context) : defaultValue as R;
    case DeviceScreenType.watch:
      return defaultValue as R;

    default:
      return throw "Invalid type: ${layoutInfo.screenType}";
  }
}