我正在尝试为我使用 Flutter 创建的游戏实现 onPressed 函数。如果我使用 RaisedButton 然后 onPressed 工作,但因为我想要一个图像然后我必须使用 Material:
child: Material(
elevation: 4.0,
onPressed: buttonsList[i].enabled
? () => playGame(buttonsList[i])
: null,
color: Colors.green,
child: Image.asset(buttonsList[i].icon),
),
这给了我以下错误:
The parameter onPressed isn't defined.
整个方法构建:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text("Cat Attack"),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: GridView.builder(
padding: const EdgeInsets.all(10.0),
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4, // 4 columns of board buttons
childAspectRatio: 1.0,
crossAxisSpacing: 9.0,
mainAxisSpacing: 9.0),
// Game board loop
itemCount: buttonsList.length,
itemBuilder: (context, i) => SizedBox(
width: 100.0,
height: 100.0,
child: Material(
elevation: 4.0,
onPressed: buttonsList[i].enabled
? () => playGame(buttonsList[i])
: null,
color: Colors.green,
child: Image.asset(buttonsList[i].icon),
),
),
),
),
RaisedButton(
child: new Text(
"Reset",
style: new TextStyle(color: Colors.white, fontSize: 20.0),
),
color: Colors.red,
padding: const EdgeInsets.all(20.0),
onPressed: resetGame,
)
],
));
}
如何在 Material 上实现 onPressed 功能?
最佳答案
您可以在 Material 小部件中使用 GestureDetector
或 InkWell
小部件。
Material(
color: Colors.green,
child: GestureDetector(
onTap: buttonsList[i].enabled
? () => playGame(buttonsList[i])
: null,
child: Image.asset(buttonsList[i].icon),
),
)
更多信息在这里:
关于Flutter onPressed Material,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55435423/