【问题标题】:Flutter - Icon widgetFlutter - 图标小部件
【发布时间】:2020-12-05 17:54:28
【问题描述】:

我正在使用一个图标小部件,我希望当我按下图标时可以跳转到另一个地方(如通话图标),以及当我按下图标时打开另一个小部件的选项(如三个点)图标),我的问题是在颤振图标小部件中没有onlongpress ... 任何可能有助于这样做的示例代码?

这是我现在的代码:

child: ListTile(
      leading: CircleAvatar(
        radius: 25.0,
        backgroundColor: Colors.brown,
      ),
      title: Text(helpRequest.category),
      subtitle: Text(helpRequest.description),
      trailing: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          IconButton(
            icon: Icon(
              Icons.call,
              size: 20.0,
              color: Colors.brown[900],
            ),
            onPressed: () {

            },

          ),
          IconButton(
            icon: Icon(
              Icons.more_vert,
              size: 20.0,
              color: Colors.brown[900],
            ),
            onPressed: () {
              
            },
          ),
        ],
      ),
    ),

【问题讨论】:

    标签: flutter widget icons onpress


    【解决方案1】:

    不要使用 IconButton,而是使用 GestureDetector 包装图标,这将为您提供 onLongPress 和 onTap (onPressed)。请看下面的代码 -

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(title: const Text("Flutter Demo")),
            body: MyStatefulWidget(),
          ),
        );
      }
    }
    
    class MyStatefulWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        Offset _tapPosition;
    
        void _storePosition(TapDownDetails details) {
          _tapPosition = details.globalPosition;
        }
    
        return ListTile(
          leading: const CircleAvatar(
            radius: 25.0,
            backgroundColor: Colors.brown,
          ),
          title: const Text("helpRequest.category"),
          subtitle: const Text("helpRequest.description"),
          trailing: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              GestureDetector(
                onTap: () => print("Tap: call"),
                onLongPress: () => print("Long Press: Call"),
                child: Icon(
                  Icons.call,
                  size: 20.0,
                  color: Colors.brown[900],
                ),
              ),
              GestureDetector(
                onTap: () => print("Tap: more_vert"),
                onTapDown: _storePosition,
                onLongPress: () async {
                  final RenderBox overlay =
                      Overlay.of(context).context.findRenderObject();
                  final int _selected = await showMenu(
                    items: [
                      PopupMenuItem(
                        value: 1,
                        child: Row(
                          children: <Widget>[
                            const Icon(Icons.delete),
                            const Text("Delete"),
                          ],
                        ),
                      ),
                      PopupMenuItem(
                        value: 2,
                        child: Row(
                          children: <Widget>[
                            const Icon(Icons.edit),
                            const Text("Edit"),
                          ],
                        ),
                      )
                    ],
                    context: context,
                    position: RelativeRect.fromRect(
                        _tapPosition &
                            const Size(40, 40), // smaller rect, the touch area
                        Offset.zero & overlay.size // Bigger rect, the entire screen
                        ),
                  );
                  switch (_selected) {
                    case 1:
                      print("delete seleted");
                      break;
                    case 2:
                      print("edit seleted");
                      break;
                  }
                },
                child: Icon(
                  Icons.more_vert,
                  size: 20.0,
                  color: Colors.brown[900],
                ),
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • InkWell 也有 onLongpress 参数和 customBorder 参数作为 circleBorder,它也可以工作。
    • 有没有一种方法可以使用 onLongPress 中的 showmenu 小部件显示菜单?我试图实现它,但它没有用,在小部件中实现小部件的事情对我来说并不顺利,你能告诉我如何以一种简单的方式做到这一点吗?
    • 我已更新代码以在 Icons.more_vert 图标的 LongPress 上显示显示菜单。您可以看到删除和编辑菜单选项。此外,当您点击任何这些菜单选项时,还可以在控制台日志中看到打印语句。如果它帮助您解决了问题,请点赞并接受答案。
    猜你喜欢
    • 2021-03-22
    • 2021-11-28
    • 1970-01-01
    • 2021-07-31
    • 2020-01-17
    • 1970-01-01
    • 2020-05-29
    • 2011-07-04
    • 1970-01-01
    相关资源
    最近更新 更多