Listener 与 GestureDetector

  • 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;
    });
  }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容