如何在Flutter中使装有碎片容器失效

new9mtju  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(84)

我有一个有两个容器的列
第一个容器具有3个芯片[“全部”、“费用”、“收入”],第二个容器具有显示数据的列表视图构建器,
这里我想禁用芯片第一个容器,而长按列表视图的项目,这样用户就不能点击任何芯片。
在我下面的代码中,我放置了Container()来隐藏芯片小部件...但是在这里我想显示禁用模式的芯片...

Widget typeselection(){
    return islongpressed==true?Container():Container(
      child: Row(
        children: List<Widget>.generate(
          3,
              (int index) {
            return Row(children: [
              ChoiceChip(
                label: Container(
                  child: Text(_types[index]),
                  padding: EdgeInsets.symmetric(horizontal: 20),

                ),
                selected: _value == index,
                onSelected: (bool selected) {
                  setState(() {
                    _value = selected ? index : 0;
                  });
                },
                backgroundColor: Colors.blue.shade50,

                //padding: EdgeInsets.all(0),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
                selectedColor: Colors.blue.shade100,
                padding: EdgeInsets.all(0),
              ),
              SizedBox(width: 10,),
            ],);
          },
        ).toList(),

      ),
    );

  }
mjqavswn

mjqavswn1#

如果您想阻止用户在您的条件为真时选择筹码,您可以尝试以下操作:

Widget typeselection() {
    return Row(
      children: List<Widget>.generate(
        3,
        (int index) {
          return Row(
            children: [
              ChoiceChip(
                label: Container(
                  child: Text(_types[index]),
                  padding: EdgeInsets.symmetric(horizontal: 20),
                ),
                selected: _value == index,
                onSelected: (bool selected) {
                  if (islongpressed != true) {// <----- add this
                    setState(() {
                      _value = selected ? index : 0;
                    });
                  }
                },
                backgroundColor: Colors.blue.shade50,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
                selectedColor: Colors.blue.shade100,
                padding: EdgeInsets.all(0),
              ),
              SizedBox(
                width: 10,
              ),
            ],
          );
        },
      ).toList(),
    );
  }

相关问题