Home > other >  How to define arguments and describe them enums with Flutter?
How to define arguments and describe them enums with Flutter?

Time:03-25

Is it possible to define enum in Flutter in the way to define specific values like color and type. Regarding to this article I do not see such things:

https://www.educative.io/blog/dart-2-language-features

code from android where this is possible:

    enum class AcceptedItemStatus(@ColorRes val color: Int, @StringRes val type: Int) {
        @SerializedName("New")
        NEW(R.color.green, R.string.accepted_chip_new),

        @SerializedName("Standby")
        STAND_BY(
            R.color.orange,
            R.string.accepted_chip_stand_by
        )
    }

CodePudding user response:

Dart doesn't have out-of-the-box support for adding custom properties to Enums. It's a shame because I really like that feature of Kotlin and other languages and use it a lot. However you can work around it using some packages.

  1. Add the built_value package to your dependencies in pubspec.yaml

  2. Add build_runner and built_value_generator to dev_dependencies

  3. Create accepted_item_status.dart

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'accepted_item_status.g.dart';

class AcceptedItemStatus extends EnumClass {

  // @BuiltValueEnumConst is optional, if you want to use the built_value serialization and want to control
  // what value each enum item serialises to:
  @BuiltValueEnumConst(wireNumber: 1)
  static const AcceptedItemStatus newItem = _$newItem;
  @BuiltValueEnumConst(wireNumber: 2)
  static const AcceptedItemStatus standby = _$standby;

  const AcceptedItemStatus._(String name) : super(name);

  static BuiltSet<AcceptedItemStatus> get values => _$values;
  static AcceptedItemStatus valueOf(String name) => _$valueOf(name);
  // This is optional, if you want to use the automatic serializer generation:
  static Serializer<AcceptedItemStatus> get serializer => _$acceptedItemStatusSerializer;

  Color get colour => _colour[this] ?? (throw StateError("No colour found for $this"));

  static const _colour = {
    newItem: Color(0xFF123456),
    standby: Color(0xFF654321),
  };
}
  1. Run the code generator to automatically generate the companion file for your enum: flutter pub run build_runner build

Et voila! Now you can use AcceptedItemStatus just like an enum, and it will have your custom properies:

final status = AcceptedItemStatus.newItem;
Color statusColour = status.colour;

CodePudding user response:

Perhaps, you can use extension for enum.

import 'package:flutter/material.dart';

enum AcceptedItemStatus {
  isNew, standBy
}

extension AcceptedItemStatusExtension on AcceptedItemStatus {
  Color get color {
    switch (this) {
      case AcceptedItemStatus.isNew:
        return Colors.green;
      case AcceptedItemStatus.standBy:
        return Colors.orange;
    }
  }

  String get title {
    switch (this) {
      case AcceptedItemStatus.isNew:
        return "Is New";
      case AcceptedItemStatus.standBy:
        return "Is Stand By";
    }
  }
}

Usage:

debugPrint("${AcceptedItemStatus.isNew.color}");
debugPrint("${AcceptedItemStatus.isNew.title}");
  • Related