首页 > 解决方案 > 如何仅在调用 onPressed 时更改 CupertinoButton 背景颜色

问题描述

我尝试过以下代码,但只有在按下按钮时它才会改变按钮的颜色。

//class attribute
Color bgColor = Colors.deepPurpleAccent;

//Widget
CupertinoButton(
  color: bgColor,
  child: Text('LOGIN', style: TextStyle(fontFamily: 'Roboto',)),
  borderRadius: const BorderRadius.all(Radius.circular(80.0)),
  onPressed: () {
  this.setState(() {
    bgColor = Colors.black;  
  });
  print(_emailValue);
  print(_passwordValue);
  Navigator.pushReplacementNamed(context, '/products');
  },
),

标签: flutterdartflutter-cupertino

解决方案


如果您只想调整突出显示按钮颜色的不透明度,可以更改pressedOpacity属性:

CupertinoButton(
  pressedOpacity: 0.4,
  ...

默认pressedOpacity值为0.1,非常低。我发现0.4原生 iOS 的外观更自然。


推荐阅读