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

2019-07-20  本文已影响0人  天色将变

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

指针事件
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
上一篇下一篇

猜你喜欢

热点阅读