schultek / super_annotations

Write your code generation functions naturally alongside your normal code. Define and use custom annotations in the same file or project.
https://pub.dev/packages/super_annotations
MIT License
39 stars 5 forks source link

Color can't define in a ClassAnnotation like a variable. #12

Closed E-MRE closed 10 months ago

E-MRE commented 1 year ago

When I try to add Color type variable, generator throws errors. Here is my code:

ClassAnnotation:

import 'package:flutter/material.dart';
import 'package:super_annotations/super_annotations.dart';

class MyAnnotation extends ClassAnnotation {
  final Color color;
  MyAnnotation(this.color);

  @override
  void apply(Class target, LibraryBuilder output) {
    var cl = Class(
      (myClass) => myClass
        ..name = 'MyClass'
        ..constructors.add(Constructor((c) => c
          ..name = 'color'
          ..requiredParameters.add(Parameter((p) => p
            ..name = 'color'
            ..toThis = true
            ..required = true
            ..type = refer('Color'))))),
    );
    output.body.add(cl);
  }
}

Other class:

import 'package:flutter/material.dart';

import 'app_text_annotation.dart';

part 'classes.super.dart';

@MyAnnotation(Colors.red)
class ClassName {}

Build result:

image

../../../flutter/packages/flutter/lib/src/material/animated_icons.dart:10:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../flutter/packages/flutter/lib/src/material/app.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' as ui;
       ^
../../../flutter/packages/flutter/lib/src/material/app_bar_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../flutter/packages/flutter/lib/src/material/arc.dart:6:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../flutter/packages/flutter/lib/src/material/badge_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../flutter/packages/flutter/lib/src/material/banner_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../flutter/packages/flutter/lib/src/material/bottom_app_bar_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
       ^
../../../flutter/packages/flutter/lib/src/material/bottom_navigation_bar_theme.dart:5:8: Error: Dart library 'dart:ui' is not available on this platform.
import 'dart:ui' show lerpDouble;
schultek commented 1 year ago

Unfortunately annotations can't rely on or use any types from flutter or dart:ui. This is because isolates (which are used behind the scenes) don't support importing dart:ui. I'm afraid there is nothing I can do.

The outputted code however can use flutter or dart:ui. So maybe just pass the color as a string.