gpt4 book ai didi

flutter 如何在不占用任何空间的情况下从 ui 中隐藏小部件

转载 作者:行者123 更新时间:2023-12-04 01:03:03 27 4
gpt4 key购买 nike

我需要连续隐藏一个小部件。我知道它看起来重复。我搜索并尝试了很多方法来隐藏这个小部件,但它仍然有空间。所以这就是我用来解决它的
1-我使用了可见性
2-我使用了偏移
3-我使用了If case
但是小部件仍然在我的行中占有一席之地,我需要将其删除并且不要在我的 Ui 中占用任何空间。
这是我的下面的代码。非常感谢任何帮助

class _AppState extends State<App> {
bool _hasImage ;

@override
Widget build(BuildContext context) {
_hasImage = false ;
return Material(
child: Container(
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Wrap(
direction: Axis.vertical,
alignment: WrapAlignment.start,
children: <Widget>[
IconButton(
icon: Icon(Icons.camera_alt),
iconSize: 40.0,
color: Color.fromRGBO(88, 60, 26, 1),
onPressed: () {
},
),
Text(
'Camera',
style: TextStyle(color: Colors.white, fontSize: 17.0),
)
],
),
Wrap(
direction: Axis.vertical,
alignment: WrapAlignment.start,
children: <Widget>[
IconButton(
icon: Icon(Icons.image),
iconSize: 40.0,
color: Color.fromRGBO(88, 60, 26, 1),
onPressed: () {
},
),
Text(
'Gallery',
style: TextStyle(color: Colors.white, fontSize: 17.0),
)
],
),
Visibility(
visible: _hasImage,
maintainSize: false,
child: Wrap(
direction: Axis.vertical,
alignment: WrapAlignment.start,
children: <Widget>[
IconButton(
icon: Icon(Icons.delete),
iconSize: 40.0,
color: Color.fromRGBO(88, 60, 26, 1),
onPressed: () {

},
),
Text(
'Remove',
style: TextStyle(color: Colors.white, fontSize: 17.0),
)
],
),
)
],
),
),
),
);
}
}

最佳答案

而不是使用 Visibility ,您可以使用 if健康)状况

Row(
children: [
OtherWidgets(),
if (hasImage) YourWrapWidget(...), // only takes up space if hasImage is true
]
)

完整解决方案:
class _AppState extends State<App> {
bool _hasImage;

@override
Widget build(BuildContext context) {
_hasImage = false;
return Material(
child: Container(
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Wrap(
direction: Axis.vertical,
alignment: WrapAlignment.start,
children: <Widget>[
IconButton(
icon: Icon(Icons.camera_alt),
iconSize: 40.0,
color: Color.fromRGBO(88, 60, 26, 1),
onPressed: () {},
),
Text(
'Camera',
style: TextStyle(color: Colors.white, fontSize: 17.0),
)
],
),
Wrap(
direction: Axis.vertical,
alignment: WrapAlignment.start,
children: <Widget>[
IconButton(
icon: Icon(Icons.image),
iconSize: 40.0,
color: Color.fromRGBO(88, 60, 26, 1),
onPressed: () {},
),
Text(
'Gallery',
style: TextStyle(color: Colors.white, fontSize: 17.0),
)
],
),
if (_hasImage) // this is what you need
Wrap(
direction: Axis.vertical,
alignment: WrapAlignment.start,
children: <Widget>[
IconButton(
icon: Icon(Icons.delete),
iconSize: 40.0,
color: Color.fromRGBO(88, 60, 26, 1),
onPressed: () {},
),
Text(
'Remove',
style: TextStyle(color: Colors.white, fontSize: 17.0),
)
],
)
],
),
),
),
);
}
}

关于flutter 如何在不占用任何空间的情况下从 ui 中隐藏小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57794584/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com