Flutter 点击事件处理的组件是 GestureDetector 组件 ;
GestureDetector 组件中可设置的选项 , 在构造函数中的可选参数中, 大部分是回调方法设置字段 ;
class GestureDetector extends StatelessWidget {
GestureDetector({
Key key,
this.child,
this.onTapDown, // 按下
this.onTapUp, // 抬起
this.onTap, // 单击
this.onTapCancel, // 单击取消
this.onSecondaryTapDown,
this.onSecondaryTapUp,
this.onSecondaryTapCancel,
this.onDoubleTap, // 双击
this.onLongPress, // 长按
this.onLongPressStart,
this.onLongPressMoveUpdate,
this.onLongPressUp,
this.onLongPressEnd,
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,
this.dragStartBehavior = DragStartBehavior.start,
})
}
GestureDetector 组件用法 :
// 手势检测组件
GestureDetector(
// 点击事件
onTap: (){
print("双击");
},
// 双击事件
onDoubleTap: (){
print("双击");
},
// 长按事件 , ()=>方法名(参数列表) 即可回调一个现有方法
onLongPress: () => _longPress(),
// 点击取消
onTapCancel: (){
print("点击取消");
},
// 点击按下
onTapDown: (e){
print("点击按下");
},
// 点击抬起
onTapUp: (e){
print("点击抬起");
},
// 手势检测的作用组件 , 监听该组件上的各种手势
child: Container(
// 子组件居中
alignment: Alignment.center,
// 内边距
padding: EdgeInsets.all(100),
// 背景装饰
decoration: BoxDecoration(
color: Colors.green,
),
child: Text(
"手势检测",
style: TextStyle(
fontSize: 50,
color: Colors.red,
),
),
),
)
GestureDetector 常用事件说明 :
完整代码示例 :
import 'package:flutter/material.dart';
class GesturePage extends StatefulWidget {
@override
_GesturePageState createState() => _GesturePageState();
}
class _GesturePageState extends State<GesturePage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
// 设置主题
theme: ThemeData(
primarySwatch: Colors.amber,
),
// 设置主体组件
home: Scaffold(
// 设置标题栏
appBar: AppBar(
title: Text("手势检测"),
// 返回按钮设置
leading: GestureDetector(
// 点击事件回调函数
onTap: (){
// 退出当前界面
Navigator.pop(context);
},
// 回退按钮图标
child: Icon(Icons.arrow_back),
),
),
// 水平/垂直方向平铺组件
body: FractionallySizedBox(
// 水平方向平铺
widthFactor: 1,
// 帧布局
child: Stack(
children: <Widget>[
// 垂直方向线性布局
Column(
children: <Widget>[
// 手势检测组件
GestureDetector(
// 点击事件
onTap: (){
print("双击");
},
// 双击事件
onDoubleTap: (){
print("双击");
},
// 长按事件 , ()=>方法名(参数列表) 即可回调一个现有方法
onLongPress: () => _longPress(),
// 点击取消
onTapCancel: (){
print("点击取消");
},
// 点击按下
onTapDown: (e){
print("点击按下");
},
// 点击抬起
onTapUp: (e){
print("点击抬起");
},
// 手势检测的作用组件 , 监听该组件上的各种手势
child: Container(
// 子组件居中
alignment: Alignment.center,
// 内边距
padding: EdgeInsets.all(100),
// 背景装饰
decoration: BoxDecoration(
color: Colors.green,
),
child: Text(
"手势检测",
style: TextStyle(
fontSize: 50,
color: Colors.red,
),
),
),
)
],
)
],
),
),
),
);
}
/// 长按事件
void _longPress(){
print("长按");
}
}
运行效果展示 :
打印结果 :
2021-03-02 20:26:54.072 15660-15678/com.example.flutter_cmd I/flutter: 点击按下
2021-03-02 20:26:54.073 15660-15678/com.example.flutter_cmd I/flutter: 点击抬起
2021-03-02 20:26:54.073 15660-15678/com.example.flutter_cmd I/flutter: 双击
2021-03-02 20:26:58.229 15660-15678/com.example.flutter_cmd I/flutter: 点击按下
2021-03-02 20:26:58.229 15660-15678/com.example.flutter_cmd I/flutter: 点击抬起
2021-03-02 20:26:58.229 15660-15678/com.example.flutter_cmd I/flutter: 双击
2021-03-02 20:26:59.279 15660-15678/com.example.flutter_cmd I/flutter: 点击按下
2021-03-02 20:26:59.682 15660-15678/com.example.flutter_cmd I/flutter: 点击取消
2021-03-02 20:26:59.682 15660-15678/com.example.flutter_cmd I/flutter: 长按
2021-03-02 20:27:02.233 15660-15678/com.example.flutter_cmd I/flutter: 点击按下
2021-03-02 20:27:02.284 15660-15678/com.example.flutter_cmd I/flutter: 点击取消
2021-03-02 20:27:02.634 15660-15678/com.example.flutter_cmd I/flutter: 长按
2021-03-02 20:27:04.465 15660-15678/com.example.flutter_cmd I/flutter: 点击按下
2021-03-02 20:27:04.465 15660-15678/com.example.flutter_cmd I/flutter: 点击抬起
2021-03-02 20:27:04.465 15660-15678/com.example.flutter_cmd I/flutter: 双击
参考资料 :
博客源码下载 :