gpt4 book ai didi

function - 如何将函数变量传递给Dart Colors

转载 作者:行者123 更新时间:2023-12-03 04:35:16 24 4
gpt4 key购买 nike

我想创建一个dart函数,该函数将构建一个将播放音符的dart函数,但在函数标注中使用vars来设置键颜色。以下是我的镜头,但会产生异常:
:18:21:错误:找不到 setter/getter :'$ kcolor'。
颜色:颜色。$ kcolor,
^^^^^^^^

  FlatButton buildKey({int notenum, String kcolor, double hedgeInset, String ttitle}) {
onPressed() {playSound(notenum);}
child: Card(
color: Colors.kcolor,
margin: EdgeInsets.symmetric(vertical: 10.0,
horizontal: hedgeInset),
child: ListTile(
title: Text(
ttitle,
)
)

);
}

最佳答案

您想查找给定包含其名称的字符串的Color的静态成员。
Dart不支持。使用运行时String(或Symbol)来引用源名称是反射。您只能使用反射库dart:mirrors做到这一点,Flutter不提供该库(大多数其他Dart平台也不提供)。
如果需要将字符串转换为颜色,则需要首先构建转换表。就像是:

const colorMap = {
"accents": Colors.accents,
"amber": Colors.amber,
"amberAccent": Colors.amberAccent,
"black": Colors.black,
"black12": Colors.black12,
"black26": Colors.black26,
"black38": Colors.black38,
"black45": Colors.black45,
"black54": Colors.black54,
"black87": Colors.black87,
"blue": Colors.blue,
"blueAccent": Colors.blueAccent,
"blueGrey": Colors.blueGrey,
"brown": Colors.brown,
"cyan": Colors.cyan,
"cyanAccent": Colors.cyanAccent,
"deepOrange": Colors.deepOrange,
"deepOrangeAccent": Colors.deepOrangeAccent,
"deepPurple": Colors.deepPurple,
"deepPurpleAccent": Colors.deepPurpleAccent,
"green": Colors.green,
"greenAccent": Colors.greenAccent,
"grey": Colors.grey,
"indigo": Colors.indigo,
"indigoAccent": Colors.indigoAccent,
"lightBlue": Colors.lightBlue,
"lightBlueAccent": Colors.lightBlueAccent,
"lightGreen": Colors.lightGreen,
"lightGreenAccent": Colors.lightGreenAccent,
"lime": Colors.lime,
"limeAccent": Colors.limeAccent,
"orange": Colors.orange,
"orangeAccent": Colors.orangeAccent,
"pink": Colors.pink,
"pinkAccent": Colors.pinkAccent,
"primaries": Colors.primaries,
"purple": Colors.purple,
"purpleAccent": Colors.purpleAccent,
"red": Colors.red,
"redAccent": Colors.redAccent,
"teal": Colors.teal,
"tealAccent": Colors.tealAccent,
"transparent": Colors.transparent,
"white": Colors.white,
"white10": Colors.white10,
"white12": Colors.white12,
"white24": Colors.white24,
"white30": Colors.white30,
"white38": Colors.white38,
"white54": Colors.white54,
"white60": Colors.white60,
"white70": Colors.white70,
"yellow": Colors.yellow,
"yellowAccent": Colors.yellowAccent,
};
然后您可以编写:
  colorMap[kcolor]

关于function - 如何将函数变量传递给Dart Colors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64237174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com