Flutter之用户点击行为捕获,及处理点击事件

在这里插入图片描述
在这里插入图片描述
1.实现的功能

  • 捕获用户的点击行为
  • 打印出点击行为
  • 捕获拖动行为并用于拖动小球
  • 更换主题同时清空打印 的点击行为
    2.用户手势行为主要是GestureDetector 这个widget提供api
child: GestureDetector(
                      onTap:()=> _printMsg("点击"),
                      onDoubleTap:()=> _printMsg("双击"),
                      onLongPress:()=> _printMsg("长按"),
                      onPanUpdate: (e){
                        print("拖动");
                        setState(() {
                          moveX+=e.delta.dx;
                          moveY+=e.delta.dy;
                        });
                      },

3.全部代码:

import 'package:flutter/material.dart';
class GesturePage extends StatefulWidget {
  @override
  _GesturePageState createState() => new _GesturePageState();
}

class _GesturePageState extends State<GesturePage> {
  bool itemFlag=true;

  String printMsg="";

  double moveX=0;
  double moveY=0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "11",
      theme: ThemeData(
        primarySwatch: Colors.teal,
        brightness: itemFlag?Brightness.dark:Brightness.light
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text("处理手势"),
          leading: GestureDetector(
            onTap: ()=>Navigator.pop(context),
            child: Icon(Icons.arrow_back),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          tooltip: '悬浮',
          child: Icon(Icons.brush),
          onPressed: () {
            _change();
          },
        ),
        body: Column(
          children: <Widget>[
            Container(
              height: 300,
              decoration: BoxDecoration(color: Colors.grey),
              child: Stack(
                children: <Widget>[
                  Positioned(
                    left: moveX,
                    top: moveY,
                    child: GestureDetector(
                      onTap:()=> _printMsg("点击"),
                      onDoubleTap:()=> _printMsg("双击"),
                      onLongPress:()=> _printMsg("长按"),
                      onPanUpdate: (e){
                        print("拖动");
                        setState(() {
                          moveX+=e.delta.dx;
                          moveY+=e.delta.dy;
                        });
                      },
                      child: Container(
                        width: 60,
                        height: 60,
                        decoration: BoxDecoration(borderRadius: BorderRadius.circular(30),color: Colors.orange),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Text(printMsg),
            Container(
                child: Wrap(children:listData(),)),
          ],
        ),
      
      ),
    );
  }
  var list= <String>[];
  _printMsg(String s) {
    setState(() {
      list.add(s);
    });
  }

  void _change() {
    list=<String>[];
    setState(() {
      itemFlag=!itemFlag;
    });
  }

  listData() {
    var listWidget= <Container>[];
    for (var i = 0; i < list.length; ++i) {
      listWidget.add(new Container(
          child: Chip(avatar: Icon(Icons.people), label: Text(list[i]),
              labelStyle: TextStyle(
                  color: Colors.pink, fontSize: 12, fontWeight:
              FontWeight.w100)))
      );
    }
  return listWidget;
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值