flutter - 在 Flutter 中切换容器
问题描述
如何在附图中实现自定义切换类型的背景。我想更改背景颜色和图标更改 onTap。在此处输入图像描述
GestureDetector(
onTap: () {
if(white == Colors.blue)
white = Colors.white;
else
white = Colors.white;
setState(() {
});
},
child: Container(
width: 100,
height: 100,
color: white,
),
),
解决方案
您可以轻松地为颜色和图标设置一个变量,以便当您发生 onTap 时,它会更改该变量,然后更改 setState。下面我包含了一小段代码,以进一步说明我要解释的内容。如果您在使用您已经编写的代码实现我的概念时遇到任何问题,请告诉我并向我展示您的一些代码,我可以提供帮助!
Widget theCard() {
Color theColor;
IconData theIcon;
return GestureDetector (
onTap: () {
if(theColor == Colors.blue) {
theColor = Color.white;
theIcon = Icons.check;
}
else {
theColor = Colors.blue;
theIcon = Icons.remove;
}
setState(() {
})
},
child: Container(
color: theColor,
child: Icon(theIcon)
)
)
}
推荐阅读
- java - 在两个 REST API 之间映射查询参数
- java - Spring AOP 看不到方法
- react-native - React Native 显示动画 SVG
- nestjs - Nest 无法解析 BookingService 的依赖关系
- algorithm - 按包含进行文件聚类 - 聚类有向图问题
- image - 如何在处理中制作清晰的像素艺术图像?
- python - 从 Python 中的数组结构中的二进制文件中读取二进制数据段
- python - gunicorn 在运行 apache 气流网络服务器时出错
- sql - 这个 SQL 查询中的“i”代表什么?
- html - 使用电子邮件 HTML 防弹背景图片,但得到一个奇怪的空框