简体   繁体   English

如何在 Flutter 的 ListView onPressed 中使图标不可见?

[英]How do I make an icon invisible in a ListView onPressed in Flutter?

I have a listView that displays a horizontal list of elements.我有一个 listView 显示元素的水平列表。 I have positioned an icon on each box.我在每个盒子上都放置了一个图标。 When the user selects or presses that particular icon (add button), it should make the icon invisible so that the user cannot press it again.当用户选择或按下该特定图标(添加按钮)时,它应该使图标不可见,以便用户无法再次按下它。

The problem I have is that ALL the icons then become invisible, for all elements of that listView.我遇到的问题是,对于该 listView 的所有元素,所有图标都变得不可见。

How can I fix this?我怎样才能解决这个问题?

I figured out that the solution will be related to some key value or keys.. so I added a key to the Visible parent widget, but it has made no difference.我发现解决方案将与一些键值或键相关。所以我向 Visible 父小部件添加了一个键,但它没有任何区别。

Here is the code这是代码

             Container(
            margin: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
            height: MediaQuery.of(context).size.height*0.4,
            width: MediaQuery.of(context).size.width,
            child: 
            ListView.builder(
              primary: false,
              shrinkWrap: true,
              scrollDirection: Axis.horizontal,
              itemCount: mylist.length,
              itemBuilder: (context, listItemIndicator)
              {

                     // Card details here


                      Positioned(bottom: 0, right: 0,
                        child: Visibility(key: UniqueKey(),
                            visible: isVisible == true ? true : false,
                            maintainSize: true,
                            maintainAnimation: true,
                            maintainState: true,
                          child: IconButton(

                              icon: Icon(Icons.add_box), iconSize: 40, 
                              onPressed: ()
                                {
                                    isVisible = false; //now all elements disappear, not just the one I pressed.

                                }

                            }
                          ),
                        ),
                      ),

The cards display with a + icon them.卡片显示时带有 + 图标。 But as I mentioned, when I press the + icon, ALL the elements have their icons made invisible - not just the one I pressed.但正如我所提到的,当我按下 + 图标时,所有元素的图标都变得不可见 - 不仅仅是我按下的那个。

All icons disappear because isVisible is same for every item.所有图标都消失了,因为每个项目的isVisible都是相同的。

You can create map of "visibilities"您可以创建“可见性”的 map

Map<int, bool> visibilities = Map.fromIterable(
  mylist,
  key: (k) => mylist.indexOf(k),
  value: (_) => true,
);

and then接着

Visibility(
  visible: visibilities[listItemIndicator],
  maintainSize: true,
  maintainAnimation: true,
  maintainState: true,
  child: IconButton(
    icon: Icon(Icons.add_box),
    iconSize: 40,
    onPressed: () {
      visibilities[listItemIndicator] = false;
    },
  ),
)

OR you can create stateful widget for that button或者您可以为该按钮创建有状态小部件

class CustomButton extends StatefulWidget {
  @override
  _CustomButtonState createState() => _CustomButtonState();
}

class _CustomButtonState extends State<CustomButton> {
  bool visible = true;

  @override
  Widget build(BuildContext context) {
    return Visibility(
      visible: visible,
      maintainSize: true,
      maintainAnimation: true,
      maintainState: true,
      child: IconButton(
        icon: Icon(Icons.add_box),
        iconSize: 40,
        onPressed: () {
          setState(() {
            visible = false;
          });
        },
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 onPressed 为选择的最喜欢的图标选择单个颤动 ListTiles? - How do I select individual flutter ListTiles for a select favorite icon with onPressed? 如何在 Flutter 的 Listview 中对齐我的文本和图标? - How do I align my text and icon in Listview in Flutter? 如何使 Android Flutter 应用程序图标变小? - How do I make the Android Flutter app icon smaller? 如何在按下时更改凸起按钮的颜色? - How do I change the color of raisedButton onPressed? 如何在没有 onPressed 的情况下在 Flutter 中进行异步 function 调用? - How to make an async function call in Flutter without onPressed? 如何在 flutter 中进入表单按钮 OnPressed() - How to make a get inside a form button OnPressed() in flutter "我应该在 OnPressed 中添加什么来进行谷歌 OAuth 登录?" - what should I add in the OnPressed in flutter to do the google OAuth sign in? 如果我无法将 Future 函数分配给 flutter 中的 onPressed,该怎么办? - What to do If I can't assign Future functions to onPressed in flutter? 更改颜色 ListView.Builder onPressed Flutter - Change color ListView.Builder onPressed Flutter 如何在 ListView 中使用多个 ListView.builder 并使用 Flutter 进行嵌套滚动? - How do I use multiple ListView.builder inside a ListView and to make nested scrolling using Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM