美文网首页
Listener 与 GestureDetector

Listener 与 GestureDetector

作者: 乐狐 | 来源:发表于2022-07-13 09:17 被阅读0次
    • 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;
        });
      }
    }
    

    相关文章

      网友评论

          本文标题:Listener 与 GestureDetector

          本文链接:https://www.haomeiwen.com/subject/iirpbrtx.html