Flutter学习六:GestureDetector练习

在Android中所有View都可以设置OnClick事件,但是在Flutter中除开少数自带Press事件的widget,大部分控件都是不带事件的,如果需要添加事件,就可以用GestureDetector作为父widget包裹需要添加事件的widget

import 'package:flutter/material.dart';

void main() {
  runApp(new SampleApp());
}

class SampleApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Sample App',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new SampleAppPage(),
    );
  }
}

class SampleAppPage extends StatefulWidget {
  SampleAppPage({Key key}) : super(key: key);

  @override
  _SampleAppPageState createState() => new _SampleAppPageState();
}

class _SampleAppPageState extends State<SampleAppPage> {
  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
      //双击
      onDoubleTap: () {
        print("onDoubleTap");
      },
      //单击
      onTap: () {
        print("onTap");
      },
      //长按
      onLongPress: () {
        print("onLongPress");
      },
      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.90),
          color: Colors.lightGreen,
          
        ),
        child: new Center(child: new Text("点我"),),
      ),
    );
  }
}

输出结果为:

猜你喜欢

转载自blog.csdn.net/sinat_29256651/article/details/81368812