我有一个StatefullWidget购买按钮。 按下按钮时,按钮应执行不同的代码,并且根据状态而有所不同。 考虑两个按钮状态: “下载”和 “打开”。 当我单击处于“下载”状态的按钮时,则下载应在另一个类中的某个位置开始(现在按钮的状态应仍未更改),并且在结束后,按钮应将其状态更改为“打开”
Button类的示例:
class BuyButton extends StatefulWidget{
ProductData productData;
@override
State<StatefulWidget> createState() {
return new _BuyButtonState(productData);
}
BuyButton(ProductData productData){
this.productData = productData;
}
}
class _BuyButtonState extends State<BuyButton> {
IconData _buttonIcon = Icons.shop;
String _buttonText = "buy";
Color _buttonColor = Colors.red.shade900;
Function _function;
_BuyButtonState(ProductData productData){
_buttonIcon = productData.getBuyIcon();
_buttonText = productData.getBuyText();
_buttonColor = productData.getBuyColor();
_function = productData.getActualAction(context);
}
void changeButtonState(ProductData productData) {
setState(() {
_buttonIcon = productData.getBuyIcon();
_buttonText = productData.getBuyText();
_buttonColor = productData.getBuyColor();
_function = productData.getActualAction(context);
});
}
@override
Widget build(BuildContext context) {
return new RaisedButton(
onPressed: () => {
function
},
color: _buttonColor,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
_buttonIcon,
color: Colors.white,
),
SizedBox(
width: 4.0,
),
Text(
FlutterI18n.translate(context, _buttonText),
style: TextStyle(color: Colors.white),
),
],
),
),
);
}