美文网首页
Flutter(三十三)屏幕指针事件

Flutter(三十三)屏幕指针事件

作者: 天色将变 | 来源:发表于2019-07-20 10:38 被阅读0次

    对于屏幕手指的触摸,使用Listener进行监听,Listener是一个功能Widget,内部具有屏幕事件的响应。

    指针事件
    • onPointerDown,// 手指按下时调用

    • onPointerMove,// 手指移动时调用

    • onPointerUp,// 手指抬起时调用

    • onPointerEnter,// 待确定

    • onPointerExit,// 待确定

    • onPointerHover,// 待确定

    • onPointerCancel,// 待确定

    • onPointerSignal,// 待确定

    • behavior = HitTestBehavior.deferToChild,// 待确定

    image.png
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Listener(
                onPointerEnter: (PointerEnterEvent event) => setState(() {
                  print("onPointerEnter");
                }),
                onPointerDown: (PointerDownEvent event) => setState(() {
                  print("onPointerDown");
                }),
                onPointerMove: (PointerMoveEvent event) => setState(() {
                  print("onPointerMove");
                }),
                onPointerUp: (PointerUpEvent event) => setState(() {
                  print("onPointerUp");
                }),
                onPointerExit: (PointerExitEvent event) => setState(() {
                  print("onPointerExit");
                }),
                onPointerCancel: (PointerCancelEvent event) => setState(() {
                  print("onPointerCancel");
                }),
                onPointerHover: (PointerHoverEvent event) => setState(() {
                  print("onPointerHover");
                }),
                onPointerSignal: (PointerSignalEvent event) => setState(() {
                  print("onPointerSignal");
                }),
                child: Center(
                  child: Container(
                    width: 200,
                    height: 150,
                    color: Colors.red[200],
                    alignment: Alignment.center,
                    child: Text(
                      "Pointer event,behavior=deferToChild",
                      style: TextStyle(fontSize: 18),
                    ),
                  ),
                ),
                behavior: HitTestBehavior.deferToChild,
              ),
              Listener(
                onPointerEnter: (PointerEnterEvent event) => setState(() {
                  print("onPointerEnter");
                }),
                onPointerDown: (PointerDownEvent event) => setState(() {
                  print("onPointerDown");
                }),
                onPointerMove: (PointerMoveEvent event) => setState(() {
                  print("onPointerMove");
                }),
                onPointerUp: (PointerUpEvent event) => setState(() {
                  print("onPointerUp");
                }),
                onPointerExit: (PointerExitEvent event) => setState(() {
                  print("onPointerExit");
                }),
                onPointerCancel: (PointerCancelEvent event) => setState(() {
                  print("onPointerCancel");
                }),
                onPointerHover: (PointerHoverEvent event) => setState(() {
                  print("onPointerHover");
                }),
                onPointerSignal: (PointerSignalEvent event) => setState(() {
                  print("onPointerSignal");
                }),
                child: Center(
                  child: Container(
                    width: 200,
                    height: 150,
                    color: Colors.blue[200],
                    alignment: Alignment.center,
                    child: Text(
                      "Pointer event,behavior=opaque",
                      style: TextStyle(fontSize: 18),
                    ),
                  ),
                ),
                behavior: HitTestBehavior.opaque,
              ),
              Listener(
                onPointerEnter: (PointerEnterEvent event) => setState(() {
                  print("onPointerEnter");
                }),
                onPointerDown: (PointerDownEvent event) => setState(() {
                  print("onPointerDown");
                }),
                onPointerMove: (PointerMoveEvent event) => setState(() {
                  print("onPointerMove");
                }),
                onPointerUp: (PointerUpEvent event) => setState(() {
                  print("onPointerUp");
                }),
                onPointerExit: (PointerExitEvent event) => setState(() {
                  print("onPointerExit");
                }),
                onPointerCancel: (PointerCancelEvent event) => setState(() {
                  print("onPointerCancel");
                }),
                onPointerHover: (PointerHoverEvent event) => setState(() {
                  print("onPointerHover");
                }),
                onPointerSignal: (PointerSignalEvent event) => setState(() {
                  print("onPointerSignal");
                }),
                child: Center(
                  child: Container(
                    width: 200,
                    height: 150,
                    color: Colors.green[200],
                    alignment: Alignment.center,
                    child: Text(
                      "Pointer event,behavior=translucent",
                      style: TextStyle(fontSize: 18),
                    ),
                  ),
                ),
                behavior: HitTestBehavior.translucent,
              ),
            ],
          ),
        );
      }
    }
    
    欢迎添加我的微信号:zrf540592766
    欢迎关注我的公众号:Flutter和Dart开发实践
    让我们共同学习进步,It is never too late to learn!
    image.png

    相关文章

      网友评论

          本文标题:Flutter(三十三)屏幕指针事件

          本文链接:https://www.haomeiwen.com/subject/pwyylctx.html