经过上篇文章的介绍,已经将跨页面之间跳转的时候传参的方式介绍给大家了,本篇文章将给大家介绍如何在 Flutter 中使用手势交互。
Flutter中的手势系统是一个强大且灵活的方式,允许开发者捕获并响应触摸屏上的各种用户交互。
onTap
: 当用户轻触屏幕时触发。onTapUp
: 当用户完成点击并抬起手指时触发。onTapDown
: 当用户触摸屏幕并开始点击时触发。onTapCancel
: 当用户取消点击时触发。onDoubleTap
: 当用户在短时间内连续点击屏幕两次时触发。onLongPress
: 当用户在屏幕上按住一段时间后触发。onLongPressStart
: 长按开始时触发。onLongPressMoveUpdate
: 在长按期间,如果手指移动,则触发。onLongPressEnd
: 长按结束时触发。onPanUpdate
: 当用户在屏幕上拖动时连续触发。onPanStart
: 当滑动开始时触发。onPanEnd
: 当滑动结束时触发。onScaleStart
: 当缩放开始时触发。onScaleUpdate
: 在缩放过程中连续触发。onScaleEnd
: 缩放结束时触发。import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Gesture Demo',
home: GestureDemoPage(),
);
}
}
class GestureDemoPage extends StatelessWidget {
const GestureDemoPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Gesture Demo'),
),
body: Center(
child: GestureDetector(
onTap: () {
print('Box Clicked!');
},
onDoubleTap: () {
print('Box DoubleClicked!');
},
onLongPress: () {
print('Box Long Pressed!');
},
onVerticalDragDown: (details) {
print("onVerticalDragDown");
},
onVerticalDragStart: (details) {
print("onVerticalDragStart");
},
onVerticalDragUpdate: (details) {
print("onVerticalDragUpdate ${details.delta.dy}");
},
onVerticalDragEnd: (details) {
print("onVerticalDragEnd");
},
onVerticalDragCancel: () {
print("onVerticalDragCancel");
},
child: Container(
// 这里存放需要监听事件的组件
width: 200,
height: 200,
color: Colors.blue,
alignment: Alignment.center,
child: const Text(
'Click or Long Press',
style: TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
),
),
),
);
}
}
flutter/material.dart
库。main
函数调用runApp
,将MyApp作为根widget。build
方法中返回一个MaterialApp。title
属性设置为"Flutter Gesture Demo",home
属性设置为GestureDemoPage。build
方法中返回一个Scaffold。appBar
属性设置为一个AppBar,其中包含一个Text,显示"Gesture Demo"。body
属性设置为一个Center,它包含一个GestureDetector。onTap
、onDoubleTap
、onLongPress
等。这些函数分别在用户点击、双击或长按容器时触发,并在控制台中打印相应的消息。onVerticalDragDown
、onVerticalDragStart
、onVerticalDragUpdate
这些函数在用户在容器上执行垂直拖动操作时触发,并在控制台中打印相应的消息。如果
details.delta.dy
输出的值是负数,说明是向上滑动,反之则是向下滑动。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。