flutter3.0学习笔记

GestureRecognizer

Preview

GestureRecognizer是一个抽象类,一种手势的识别器对应一个GestureRecognizer的子类。GestureDetector内部是使用一个或多个GestureRecognizer来识别各种手势的,而GestureRecognizer的作用就是通过Listener来将原始指针事件转换为语义手势。 例子: 点击时给文本变色

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class GestureRecognizerPage extends StatefulWidget {
  const GestureRecognizerPage({Key? key}) : super(key: key);

  @override
  State<GestureRecognizerPage> createState() => _GestureRecognizerPageState();
}

class _GestureRecognizerPageState extends State<GestureRecognizerPage> {
  TapGestureRecognizer _tapGestureRecognizer = TapGestureRecognizer();
  bool _toggle = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('GestureRecognizer'),),
      body: Center(
        child: Text.rich(
          TextSpan(
            children: [
              const TextSpan(text: '学习flutter'),
              TextSpan(
                text: '点我变色哦',
                style: TextStyle(
                  fontSize: 30,
                  color:_toggle?Colors.blue:Colors.red
                ),
                recognizer: _tapGestureRecognizer..onTap = (){
                  setState(() {
                    _toggle = !_toggle;
                  });
                }
              ),
              const TextSpan(text: '你好flutter'),
            ]
          )
        ),
      ),
    );
  }
}

image.png