dart - 单击图像样式应更改或图像应在颤动中更改的图像
问题描述
我在 2 列中有 4 个图像,当我单击一个图像时,它的样式应该像颜色一样改变,阴影应该改变或者该图像应该被其他图像替换。单击该图像后,其他图像应保持不变。它应该像单选按钮一样工作。怎么做?请帮助我,在此先感谢。
final img_rowi= Center(child:
new Container(
color: Colors.transparent,
padding: const EdgeInsets.all(5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(padding: const EdgeInsets.all(3.0),child: Stack(
alignment: Alignment.center,
children: <Widget>[
svgIcon,new GestureDetector(
onTap: (){
setState(() {
pressed = !pressed;
});
},
child:
Container(
child: new Column(
children: <Widget>[
new Container(
child: new Image.asset(
'images/sheep_female.png',
height: 50.0,
fit: BoxFit.cover,
),
),
new Container(
child: new Text('Sheep',style: pressed
? TextStyle(color: const Color(0xFFCDCDCD),fontFamily: 'Montserrat',
)
: TextStyle(color:Colors.black,fontFamily: 'Montserrat',
),),
),
],
),
),
),
],
),),
Padding(padding: const EdgeInsets.all(3.0),child:
Stack(
alignment: Alignment.center,
children: <Widget>[
svgIcon,new GestureDetector(
onTap: (){
setState(() {
pressed1 = !pressed1;
});
},
child:
Container(
child: new Column(
children: <Widget>[
new Container(
child: new Image.asset(
'images/biily_doe.png',
height: 50.0,
fit: BoxFit.cover,
),
),
new Container(
child: new Text('Billy Doe',style: pressed1
? TextStyle(color: const Color(0xFFCDCDCD),fontFamily: 'Montserrat',
)
: TextStyle(color:Colors.black,fontFamily: 'Montserrat',
),),
),
],
),
),
),
],
),),
],
),
),
);
解决方案
将 Image 的初始属性存储在变量中。例如,如果我想设置FlutterLogo
小部件的初始颜色,Colors.blue
然后在类中声明一个状态。然后用小部件包装您的图像GestureDetector
并设置onTap
属性。现在调用setState
方法并更改其中的所有变量(图像的属性)。
下面是一个示例,其中有一个FlutterLogo
小部件,我已将该小部件的初始颜色设置为Colors.blue
,当我点击它时,FlutterLogo
小部件的颜色更改为Colors.green
。如果我再次点击它并且如果颜色是Colors.green
那么它会改变颜色Colors.yellow
等等。您可以对您的图像做类似的事情并更改它的大小、可见性和其他属性。
还有一个imagePath
变量存储初始资产的路径,当用户点击第二个小部件(Image.asset
)时Column
,变量的值imagePath
被改变,build
方法被再次调用,图像被替换。
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool visibility;
Color colorOfFlutterLogo = Colors.blue;
String imagePath1 = "assets/initial-path-of-image-1";
String imagePath2 = "assets/initial-path-of-image-2";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
),
body: Column(
children: <Widget>[
GestureDetector(
onTap: () => setState(() {
if (colorOfFlutterLogo == Colors.blue)
colorOfFlutterLogo = Colors.green;
else if (colorOfFlutterLogo == Colors.green)
colorOfFlutterLogo = Colors.yellow;
else if (colorOfFlutterLogo == Colors.yellow)
colorOfFlutterLogo = Colors.blue;
}),
child: FlutterLogo(
size: double.infinity,
colors: colorOfFlutterLogo,
),
),
// Image 1
GestureDetector(
onTap: () => setState(() {
imagePath2 = "assets/new-path-for-image-2";
}),
child: Image.asset(imagePath1),
),
// Image 2
GestureDetector(
onTap: () => setState(() {
imagePath1 = "assets/new-path-for-image-1";
}),
child: Image.asset(imagePath2),
)
],
));
}
}
推荐阅读
- excel - 在 Excel 数据透视表中重复第一层列标题
- python - 使用成员的平均值或中位数对列的值进行平滑处理,这些成员属于同一个 bin
- php - PHP获取文件扩展名返回.file
- hibernate - 创建孩子时也要更新父集合
- node.js - 外部图像可以显示为 WebP 格式吗?
- angular - Angular 编译器中的错误需要 TypeScript >=3.6.4 和 <3.8.0 但找到的是 3.8.3
- subscribe - 在 bsc 上验证和发布合同源代码?
- java - 通过 Spring Web 客户端发送 json 字符串作为查询参数
- amazon-web-services - AWS 实例超时问题
- powershell - 具有循环的 powershell 任务中的 Azure DevOps task.setVariable 仅设置最后一个变量值