- Listener:
- GestureDetector:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
home: ListenerWidget(),
));
}
class ListenerWidget extends StatefulWidget {
const ListenerWidget({Key? key}) : super(key: key);
@override
State createState() => _ListenerWidgetSate();
}
class _ListenerWidgetSate extends State<ListenerWidget> {
late String _operation = "No Gesture detected!";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Listener(
child: ConstrainedBox(
constraints: BoxConstraints.tight(const Size(100.0, 100.0)),
child: const DecoratedBox(
decoration: BoxDecoration(color: Colors.deepOrange)),
),
onPointerDown: (event) => print("点击事件"),
),
GestureDetector(
child: Container(
alignment: Alignment.center,
color: Colors.blue,
width: 200.0,
height: 100.0,
child: Text(
_operation,
style: const TextStyle(color: Colors.white),
),
),
onTap: () => updateText("Tap"),
onDoubleTap: () => updateText("DoubleTap"),
onLongPress: () => updateText("LongPress"),
),
],
),
);
}
void updateText(String text) {
//更新显示的事件名
setState(() {
_operation = text;
});
}
}
网友评论