可见性小部件中的子图像颤动
child image in visiblity widget flutter
我的目标是在我的变量 bool 为真时显示图像,否则我不会显示图像,因为我使用了小部件可见性。但它不起作用如何解决它?
Visibility(
visible: widget.isThereIcon,
child: const Padding(
padding: EdgeInsets.only(left: 8.0),
child: Image.asset(
'images/OK.png',
height: MediaQuery.of(context).size.width * .27,
width: MediaQuery.of(context).size.width * .27,
),
),
),
它是无状态的还是有状态的?展示你如何改变能见度状态?
可能你忘记了 setState
setState((){
widget.isThereIcon = true;
});
或
setState((){
widget.isThereIcon = false;
});
确保widget.isThereIcon
是bool变量
如果要显示图像,请将值设置为 true,如果要隐藏图像,请将值设置为 false
使用 statefullWidget 和您的 UI 代码:
Scaffold(
body: Column(
children: [
Visibility(
visible: widget.isThereIcon,
child: const Padding(
padding: EdgeInsets.only(left: 8.0),
child: Image.asset(
'images/OK.png',
height: MediaQuery.of(context).size.width * .27,
width: MediaQuery.of(context).size.width * .27,
),
),
),
TextButton(
onPressed: () {
setState(() {
widget.isThereIcon = !widget.isThereIcon;
//if widget.isThereIcon == true => widget.isThereIcon = false
//if widget.isThereIcon == false => widget.isThereIcon = true
});
},
child: Text("show image"),
),
],
),
),
我的目标是在我的变量 bool 为真时显示图像,否则我不会显示图像,因为我使用了小部件可见性。但它不起作用如何解决它?
Visibility(
visible: widget.isThereIcon,
child: const Padding(
padding: EdgeInsets.only(left: 8.0),
child: Image.asset(
'images/OK.png',
height: MediaQuery.of(context).size.width * .27,
width: MediaQuery.of(context).size.width * .27,
),
),
),
它是无状态的还是有状态的?展示你如何改变能见度状态? 可能你忘记了 setState
setState((){
widget.isThereIcon = true;
});
或
setState((){
widget.isThereIcon = false;
});
确保widget.isThereIcon
是bool变量
如果要显示图像,请将值设置为 true,如果要隐藏图像,请将值设置为 false
使用 statefullWidget 和您的 UI 代码:
Scaffold(
body: Column(
children: [
Visibility(
visible: widget.isThereIcon,
child: const Padding(
padding: EdgeInsets.only(left: 8.0),
child: Image.asset(
'images/OK.png',
height: MediaQuery.of(context).size.width * .27,
width: MediaQuery.of(context).size.width * .27,
),
),
),
TextButton(
onPressed: () {
setState(() {
widget.isThereIcon = !widget.isThereIcon;
//if widget.isThereIcon == true => widget.isThereIcon = false
//if widget.isThereIcon == false => widget.isThereIcon = true
});
},
child: Text("show image"),
),
],
),
),