当前位置:网站首页>Flutter Getting Started and Advanced Tour (7) GestureDetector

Flutter Getting Started and Advanced Tour (7) GestureDetector

2022-08-09 13:24:00 Xie Dong_

引言:

GestureDetector在Flutter中负责处理跟用户的简单手势交互,GestureDetectorControl without images show,Just detection of user input gesture,并作出相应的处理,包括点击、拖动和缩放.Many controls useGestureDetectorTo provide other controls callback,比如IconButton、RaisedButton和FloatingActionButton控件有onPressed回调,当用户点击控件时触发回调,当用户点击控件时触发回调.

我们来一起看下GestureDetector的构造方法:

 GestureDetector({
    
    Key key,
    this.child,
    this.onTapDown,
    this.onTapUp,
    this.onTap,
    this.onTapCancel,
    this.onDoubleTap,
    this.onLongPress,
    this.onLongPressUp,
    this.onVerticalDragDown,
    this.onVerticalDragStart,
    this.onVerticalDragUpdate,
    this.onVerticalDragEnd,
    this.onVerticalDragCancel,
    this.onHorizontalDragDown,
    this.onHorizontalDragStart,
    this.onHorizontalDragUpdate,
    this.onHorizontalDragEnd,
    this.onHorizontalDragCancel,
    this.onForcePressStart,
    this.onForcePressPeak,
    this.onForcePressUpdate,
    this.onForcePressEnd,
    this.onPanDown,
    this.onPanStart,
    this.onPanUpdate,
    this.onPanEnd,
    this.onPanCancel,
    this.onScaleStart,
    this.onScaleUpdate,
    this.onScaleEnd,
    this.behavior,
    this.excludeFromSemantics = false
  }) 


从构造方法中,我们看出GestureDetectorCallback constructor defined in a variety of events,还有一个child属性,这就意味着我们可以利用GestureDetector包裹本身不支持点击回调事件的Widget赋予它们点击回调能力,像Text、ImageWe can't useRaisedButtonAs direct toText、Image绑定onPress回调,但是我们可以借助GestureDetector完成这一操作.

As shown in figure I giveTextGiven the various events interaction:
在这里插入图片描述

import 'package:flutter/material.dart';

void main() {
    
  runApp(new MaterialApp(home: new MyApp()));
}

class MyApp extends StatelessWidget {
    
  @override
  Widget build(BuildContext context) {
    

    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Gestures"),
      ),
      body: new Center(
          child: new GestureDetector(
            child: new Text("I was given the ability to click on the touch...",style: new TextStyle(fontSize: 20.0),),
            onTap: () {
    
              print("------onTap");
            },
            onDoubleTap: () {
    
              print("------onDoubleTap");
            },
            onLongPress: () {
    
              print("-----onLongPress");
            },
            onVerticalDragStart: (details){
    
              print("In the vertical direction starting position:"+details.globalPosition.toString());
            }, onVerticalDragEnd: (details){
    
            print("At the end of the vertical position:"+details.primaryVelocity.toString());
          },
          )),
    );
  }
}

我们在logThe command line crawl to the interaction of the callback events:
在这里插入图片描述

下一篇:Flutter入门进阶之旅(八)Button Widget

原网站

版权声明
本文为[Xie Dong_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091203556947.html