我是 Flutter 的新手,我上周开始使用 Flutter,现在我想做一个简单的 Xylophone 应用程序。我成功地创建了 UI 并创建了一个函数playSound(int soundNumber)
,但是当我调用这个函数来播放声音时,它给了我这个错误。
**The following _TypeError was thrown building Body(dirty, state: _BodyState#051c2):
type '_MaterialStatePropertyAll<dynamic>' is not a subtype of type 'MaterialStateProperty<Color?>?'**
这是我为函数编写的代码playSound(int soundNumber)
。
void playSound(int soundNumber) {
final player = AudioCache();
player.play('note$soundNumber.wav');}
Expanded buildPlayButton({MaterialStateProperty color, int soundNumber}){
return Expanded(
child: ElevatedButton(
onPressed: () {
playSound(soundNumber);
},
style: ButtonStyle(
backgroundColor: color,
),
),
);}
这是我调用这个函数的地方。
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
buildPlayButton(color: MaterialStateProperty.all(Colors.red), soundNumber: 1),
buildPlayButton(color: MaterialStateProperty.all(Colors.orangeAccent), soundNumber: 2),
buildPlayButton(color: MaterialStateProperty.all(Colors.yellow), soundNumber: 3),
buildPlayButton(color: MaterialStateProperty.all(Colors.indigo), soundNumber: 4),
buildPlayButton(color: MaterialStateProperty.all(Colors.blue), soundNumber: 5),
buildPlayButton(color: MaterialStateProperty.all(Colors.lightGreenAccent), soundNumber: 6),
buildPlayButton(color: MaterialStateProperty.all(Colors.green), soundNumber: 7),
],
);
}
如何调用此函数,因为它给了我上述错误?