【问题标题】:Flutter how to show/hide container in specing listview颤动如何在指定列表视图中显示/隐藏容器
【发布时间】:2021-05-29 11:25:16
【问题描述】:

我在 ListView.builder 中显示我的数据问题是我需要在特定列表中显示/隐藏小部件,但它适用于所有列表。

我的代码

ListView.builder(
        itemCount: 3,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              print('show');
              setState(() {
                showQty = !showQty;
              });
            },
            child: Column(
              children: [
                Text('Always'),
                showQty ? Text('onClick') : Container()
              ],
            ),
          );
   })

你可以看到我有 2 个文本。当 showQty 为 true 时,我显示的第二个文本工作正常,但问题是它在所有 itemBuilder 上发生变化

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这是因为您将点击应用于列表视图的所有项目。为了实现折叠并在单击时显示,您已将 bool 应用于列表的特定索引。尝试如下

      List<bool> showQty=[];
    
    
    
    @override
      void initState() {
        super.initState();
        for(int i=0;i<data.length;i++){
          showQty.add(false);
        }
      }
    
        void showHide(int i){
         setState((){
             showQty[i]=!showQty[i];
          });
        }
    
    ListView.builder(
        itemCount: data.length,
        shrinkWrap:true,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              print('show');
             showHide( index);
            },
            child: Column(
              children: [
                Text('Always'),
                showQty[index] ? Text('onClick') : Container()
              ],
            ),
          );
      }
    )
    

    【讨论】:

    • 但是你定义了 false 3 次,如果 itemCount 的长度类似于 itemCount: data.length 那么我只给出 itemCount 的例子是 3 呢?
    【解决方案2】:

    对于显示和隐藏小部件,您可以使用Visibility 小部件。 有关Visibility 的更多信息 希望能帮到你。

    【讨论】:

      猜你喜欢
      • 2016-11-29
      • 2015-01-15
      • 1970-01-01
      • 2021-09-07
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多