import 'package:flutter/material.dart';
void main(){
runApp(new MaterialApp(
title: "flutter质感设计",
home: new MyButton(),
));
}
class MyButton extends StatelessWidget{
@override
Widget build(BuildContext context) {
// TODO: implement build
return new GestureDetector(
onTap: (){
print("MyButton被监听!");
},
onDoubleTap: (){
print("MyButton被双击了");
},
onLongPress: (){
print("MyButton长按");
},
child: new Container(
height: 36.0,
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.symmetric(horizontal: 8.0),
decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(5.0),
color: Colors.lightGreen[500]
),
child: new Center(
child: new Text("点击监听"),
),
),
);
}
}
GestureDetector控件没有图像展示,只是检测用户输入的手势。当用户点击Container时,GestureDetector会调用onTap回调,然后打印信息到控制台。你可以使用GestureDetector检测各种输入手势,包括点击、拖动和缩放。
许多控件使用GestureDetector为其他控件提供回调,比如IconButton、RaisedButton和FloatingActionButton控件有onPressed回调,当用户点击控件时触发回调。
GestureDetector:
onTap():点击事件回调;
onDoubleTap():双击事件回调;
onLongPress():长按事件回调