【问题标题】:How can I use gesture detector with icons:[]如何使用带有图标的手势检测器:[]
【发布时间】:2021-06-17 19:53:12
【问题描述】:

这是我试图将手势检测器放在图标上的代码

    child: Container(
                                      child: SimpleAccountMenu(

                                        icons: [
                                          
                                          Icon(Icons.notifications),
                                          Icon(Icons.lock),
                                          Icon(Icons.thumb_up),
                                        ],
                                        iconColor: Colors.white,
                                        onChange: (index) {
                                          print(index);
                                        }, borderRadius: BorderRadius.circular(30),
                                      )),

【问题讨论】:

  • 您是否尝试过包裹在 InkWell、InkResponse 或 GestureDetector 上?
  • 是的,我试过了,但它显示错误
  • 什么是错误...你能发布错误吗?
  • 是的,我只给你看几秒钟
  • 如果您“icons[]”指定图标小部件,那么您的代码将无法正常工作。 Icon 是一个小部件,因此,而不是 Icon,期望一个小部件列表,就是这样,然后您可以包装图标或使用 IconButton

标签: android visual-studio flutter android-studio flutter-layout


【解决方案1】:

您可以使用IconButton 小部件并给 onPressed 回调。

child: Container(
                                      child: SimpleAccountMenu(

                                        icons: [
                                          
                                          IconButton(icon: Icon(Icons.notifications), onPressed: () {}),
                                          IconButton(icon: Icon(Icons.lock), onPressed: () {}),
                                          IconButton(icon: Icon(Icons.thumb_up), onPressed: () {}),
                                        ],
                                        iconColor: Colors.white,
                                        onChange: (index) {
                                          print(index);
                                        }, borderRadius: BorderRadius.circular(30),
                                      )),

【讨论】:

  • 我尝试了这种方法,但它在 iconbutton 上显示错误。
  • 您的icons 参数可能需要Icon 列表。将其更改为IconButton列表
  • 无法将元素类型“IconButton”分配给列表类型“Icon”。
  • 正是我之前的评论将解决问题。只需将SimpleAccountMenu 构造函数的icon 参数更改为List<IconButton>
  • 好的,谢谢你帮了我很多忙
猜你喜欢
  • 1970-01-01
  • 2023-03-06
  • 2021-11-21
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 2011-08-01
相关资源
最近更新 更多