程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在颤动中隐藏小部件?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在颤动中隐藏小部件??

开发过程中遇到如何在颤动中隐藏小部件?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在颤动中隐藏小部件?的解决方法建议,希望对你解决如何在颤动中隐藏小部件?有所启发或帮助;

我试图隐藏一个具有 List 数组和 List 数组获取 _images.length 的小部件。例如,如果 image.length 为 Null,就像没有图像一样,所以我想隐藏占用空间的容器。不确定我错过了什么。我尝试了下面的代码。请帮帮我,谢谢。或者如果有任何其他方法可以做到这一点,请告诉我。我只是一个初学者。

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 数组长度将始终返回 0,因此您需要将条件设置为

setState(() {
    isVisible = _images.length > 0;
});

不要将变量 isVisible 设置为 _images.length > 0 之类的

visible: _images.length > 0

并删除 isVisible 变量......它会在 _images 列表更新时更新可见性

,

还有另一种不使用可见小部件的解决方案:

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(),);
  }
}

如果有图片,小部件的高度和宽度将不为零 但如果不是,小部件将是可见的,因为宽度和高度将等于零

,

正如我在代码段中看到的,您没有在任何地方调用 changeVisibility 方法。因此,isVisible 将始终保持 true

因此,无论您在何处调用 changeVisibility 方法,都应调用 getImage

另外,逻辑本身就是错误的,

初始化isVisiblefalse,这样你就可以在有图片的时候让它为true。

大佬总结

以上是大佬教程为你收集整理的如何在颤动中隐藏小部件?全部内容,希望文章能够帮你解决如何在颤动中隐藏小部件?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。