flutter - 如何仅在调用 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');
},
),
解决方案
如果您只想调整突出显示按钮颜色的不透明度,可以更改pressedOpacity
属性:
CupertinoButton(
pressedOpacity: 0.4,
...
默认pressedOpacity
值为0.1
,非常低。我发现0.4
原生 iOS 的外观更自然。
推荐阅读
- c# - 使用 Onion 架构设计的 .net 核心应用程序可以托管在 3 层(3 个物理服务器 - Web、应用程序、数据库)中吗?
- apache-spark - 当我们尝试将 Spark DataFrame 写入 Cassandra 时,Cassandra 类型如何在内部转换数据类型?
- python - 使用多个时间戳对多个用户数据进行分组
- wpf - 如果用户输入了无效的电子邮件地址,则禁用按钮 - WPF
- javascript - AJAX JSON 加载的表单按钮刷新页面而不是再次使用 AJAX
- php - PHP 致命错误:未捕获的异常:错误:1408F10B:SSL 例程:ssl3_get_record:/api.genius.com 的版本号错误
- javascript - 更新 Dynamodb 中的项目?
- python - 使用shutil和重命名时出错
- rust - 如何使 #![no_main] 属性有条件?
- javascript - 需要为多个搜索文件夹创建搜索值为 null