[英]how to hide a widget in flutter?
I am trying to hide a widget which has List array and List array gets _images.length.我正在尝试隐藏一个具有列表数组且列表数组获取 _images.length 的小部件。 For Example, if image.length is Null, like if there are no images so I want to hide the container which takes space.Not sure what I am missing.例如,如果 image.length 是 Null,就像没有图像一样,所以我想隐藏占用空间的容器。不确定我缺少什么。 I tried the code below.我尝试了下面的代码。 Help me out pls, Thanks.请帮帮我,谢谢。 or if there is any other way to do it pls let me know.或者如果有任何其他方法可以做到这一点,请告诉我。 I am just a beginner.我只是一个初学者。
class PortfolioGallarySubPage extends StatefulWidget{
PortfolioGallarySubPage({Key key,@required this.urls,@required this.currentIndex})
:super(key:key);
@override
_PortfolioGallarySubPage createState() => _PortfolioGallarySubPage();
}
class _PortfolioGallarySubPage extends State<PortfolioGallarySubPage>
with SingleTickerProviderStateMixin{
final GlobalKey<FormState> formKey = new GlobalKey<FormState>();
final GlobalKey<ScaffoldState> key = new GlobalKey<ScaffoldState>();
List<File> _images = [];
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
if (pickedFile != null) {
_images.add(File(pickedFile.path));
}
else {
print('No image selected.');
}
});
}
@override
void initState() {
super.initState();
}
@override void dispose()
{
super.dispose();
}
bool isVisible = true;
void changeVisibility(){
setState(() {
if(_images.length ==null ){
isVisible = !isVisible;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: key,
extendBodyBehindAppBar: true,
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
actions: [
ElevatedButton(
child: Text("DONE",style: TextStyle(fontSize: 15)),
onPressed: (){
_uploadImages();
},
style: ElevatedButton.styleFrom(padding: EdgeInsets.fromLTRB(25.0, 15.0, 25.0, 10.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0))),
),
],
),
body: Container(
width: _maxScreenWidth,
child: SafeArea(
child:Form(
key: formKey,
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Visibility(
visible: isVisible,
child: Container(
height: 150.0,
padding: EdgeInsets.symmetric(vertical: 15.0,horizontal: 15),
child: ListView(
scrollDirection: Axis.horizontal,
children: [
SizedBox(width: 15.0),
ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: _images.length,
itemBuilder: (BuildContext context,int index){
//return Padding(padding: const EdgeInsets.only(top: 0.0,bottom: 0.0),
return InkWell(
onTap: () => print('tapped'),
child: Card(
elevation: 10,
child: SizedBox(height:150, width: 150,child: Image.file(_images[index], fit: BoxFit.cover,)) ,
),
);
},
),
],
),
),
),
],
),
),
],
),
),
),
),
),
);
}
}
_images
array length will always return 0 if the list is empty, so you need to set the condition as如果列表为空, _images
数组长度将始终返回 0,因此您需要将条件设置为
setState(() {
isVisible = _images.length > 0;
});
Instead of taking variable isVisible
set the _images.length > 0
like而不是采用变量isVisible
设置_images.length > 0
像
visible: _images.length > 0
And remove the isVisible variable.... it will update the visibility when _images
list is updated并删除 isVisible 变量....它会在_images
列表更新时更新可见性
there is another solution of this without using visible widget:在不使用可见小部件的情况下,还有另一种解决方案:
class Mywidget extends StatefulWidget {
@override
_MywidgetState createState() => _MywidgetState();
}
class _MywidgetState extends State<Mywidget> {
double width;
double heigth;
void changeVisibility() {
setState(() {
if (_images.length == null) {
width=any width you want ;
heigth = any height you want ;
}else{
setState(() {
width=0;
heigth=0;
});
}
});
}
@override
Widget build(BuildContext context) {
// the contanier that you want to be visble
return Container(
height: heigth,
width: width,
// the list view that has the images
child: ListView(),
);
}
}
if there is an image the height and the width of the widget will be not zero but if not the widget will be visible because the width and the height will be equal to zero如果有图像,小部件的高度和宽度将不为零,但如果没有,小部件将可见,因为宽度和高度将等于零
As I can see in the snippet, you are not calling the changeVisibility
method anywhere.正如我在片段中看到的,您没有在任何地方调用changeVisibility
方法。 Hence, isVisible
will always remain true
因此, isVisible
将始终保持为true
So make a call to changeVisibility
wherever you are calling getImage
method.因此,无论您在哪里调用getImage
方法,都要调用changeVisibility
。
Also, the logic is inherently wrong,而且,逻辑本身就是错误的,
initialize isVisible
to false initially, this way you can make it true when there is an image.最初将isVisible
初始化为 false,这样当有图像时,您可以使其为 true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.