discontinuedPackage responsive_config has been discontinued.

Class.fromElement constructor

Class.fromElement(
  1. ClassElement element
)

Factory fromElement constructor for creating a Class from a ClassElement.

Implementation

factory Class.fromElement(ClassElement element) {
  final name = element.name;

  const annotationChecker = TypeChecker.fromRuntime(ResponsiveConfig);
  final configAnnotation = annotationChecker.firstAnnotationOf(element);

  final reader = ConstantReader(configAnnotation);
  final desktopReader = reader.read('desktopBreakpoint');
  final tabletReader = reader.read('tabletBreakpoint');
  final laptopReader = reader.read('laptopBreakpoint');
  final mobileReader = reader.read('mobileBreakpoint');
  final widescreenReader = reader.read('widescreenBreakpoint');

  final desktopBreakpoint = desktopReader.isNull
      ? DefaultBreakpoints.desktop
      : desktopReader.doubleValue;
  final tabletBreakpoint = tabletReader.isNull
      ? DefaultBreakpoints.tablet
      : tabletReader.doubleValue;
  final laptopBreakpoint = laptopReader.isNull
      ? DefaultBreakpoints.laptop
      : laptopReader.doubleValue;
  final mobileBreakpoint = mobileReader.isNull
      ? DefaultBreakpoints.mobile
      : mobileReader.doubleValue;
  final widescreenBreakpoint = widescreenReader.isNull
      ? DefaultBreakpoints.widescreen
      : widescreenReader.doubleValue;

  Iterable<Variable> variables() sync* {
    for (final fieldElement in element.fields) {
      if (fieldElement.isStatic || !fieldElement.isFinal) continue;
      yield Variable.fromElement(fieldElement);
    }
  }

  return Class(
    name: name,
    desktopBreakpoint: desktopBreakpoint,
    tabletBreakpoint: tabletBreakpoint,
    laptopBreakpoint: laptopBreakpoint,
    mobileBreakpoint: mobileBreakpoint,
    widescreenBreakpoint: widescreenBreakpoint,
    variables: variables(),
  );
}