Flutter 如何更改图标按钮中的图标
我正试图建立一个whatsapp克隆,当我在工作的时候,从前面和后面改变相机。我试图更改图标按钮中的图标,但它没有更改 我将在下面附上我的代码文件Flutter 如何更改图标按钮中的图标,flutter,dart,icons,Flutter,Dart,Icons,我正试图建立一个whatsapp克隆,当我在工作的时候,从前面和后面改变相机。我试图更改图标按钮中的图标,但它没有更改 我将在下面附上我的代码文件 Widget bottomIcon({Icon icon,double size,Function onpress}){ return IconButton( icon: icon, iconSize: size, color: Colors.white,
Widget bottomIcon({Icon icon,double size,Function onpress}){
return IconButton(
icon: icon,
iconSize: size,
color: Colors.white,
onPressed: onpress,
);
}
Icon iconForcam=Icon(Icons.camera_rear);
@override
Widget build(BuildContext context) {
if (!controller.value.isInitialized) {
return Container();
}
return MaterialApp(
home: Padding(
padding: const EdgeInsets.all(1.0),
child: Stack(
fit: StackFit.expand,
children: [
CameraPreview(controller),
Positioned(
bottom: 0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
SizedBox(width: 20.0,),
bottomIcon(icon: Icon(Icons.flash_on_rounded),size: 50.0),
SizedBox(width: 20.0,),
bottomIcon(icon: Icon(Icons.fiber_manual_record_outlined),size: 100.0),
SizedBox(width: 30.0,),
bottomIcon(icon: iconForcam,size: 50.0,onpress: (){
setState(() {
if(iconForcam == Icon(Icons.camera_front)){
iconForcam = Icon(Icons.camera_rear);
}else if(iconForcam == Icon(Icons.camera_rear)){
print('rearcam');
iconForcam = Icon(Icons.camera_front);
}
});
//toggleCamera();
}),
],
),
),
],
),
),
);
}
}
我怀疑在if中,我能否在if语句中使用两个图标。您可以定义一个布尔变量 //定义
bool _isFront = true;
//用法
bottomIcon(
icon: _isFront ?
Icons.camera_front : Icons.camera_rear,
size: 50.0, onpress: (){
setState(() {
_isFront = !_isFront;
});
//toggleCamera();
})
我试着这样做,但没有弄错 //定义
int _oldIndex=0;
Icon iconForcam=Icon(Icons.camera_rear);
//内码
bottomIcon(icon: iconForcam,size: 50.0,onpress: (){
setState(() {
if(_oldIndex == 0){
iconForcam = Icon(Icons.camera_rear);
_oldIndex = 1;
}else if(_oldIndex == 1){
//print('rearcam');
iconForcam = Icon(Icons.camera_front);
_oldIndex = 0;
}
});
toggleCamera(_oldIndex);
}),
你使用StatefulWidget吗?设置状态((){iconForcam=Icon(Icons.camera_后方);});是的,但每次点击我都需要来回更改。谢谢你的回答。兄弟,我试过这个,效果很好。兄弟,欢迎你