【发布时间】:2018-02-13 05:28:11
【问题描述】:
我有一个小时候有图像的按钮。我想要它,以便在按下按钮时,图像更改为不同的图像,并且当用户停止按下按钮时,它会返回到其原始图像。
基本上我希望它像一个凸起的按钮,但带有用于凸起和按下状态的自定义图像。
以下是相关代码:
class LoginButton extends StatefulWidget {
@override
_LoginButtonState createState() => new _LoginButtonState();
}
class _LoginButtonState extends State<LoginButton> {
void _onClicked() {
setState(() {
//I don't know what I should put here to cause the image to redraw
//only on button press
});
}
@override
Widget build(BuildContext context) {
var assetImage = new AssetImage("assets/loginscreen/btn.png");
var image = new Image(image: assetImage, height: 50.0, width: 330.0);
return new Container(
height: image.height,
width: image.width,
child: new FlatButton(
onPressed: _onClicked,
child: new ConstrainedBox(
constraints: new BoxConstraints.expand(),
child: image,
),
),
);
}
}
【问题讨论】:
-
我已经用更相关的代码更新了我的答案,以实现您想要实现的目标。