flutter TextBotton onPressed事件注册到render源码分析

//onPressed  传递给父类 ButtonStyleButton
const ButtonStyleButton({
  Key? key,
  required this.onPressed,
 ......省略

// build() 传递给. InkWell

Widget build(BuildContext context) {

.....省略

final Widget result = ConstrainedBox(
  constraints: effectiveConstraints,
  child: Material(
          ...省略
    type: resolvedBackgroundColor == null ? MaterialType.transparency : MaterialType.button,
    animationDuration: resolvedAnimationDuration,
    clipBehavior: widget.clipBehavior,
    child: InkWell(
      onTap: widget.onPressed,...省略

//InkWell 传递给父类 InkResponse

const InkResponse({
  Key? key,
  this.child,
  this.onTap,...省略

//build 给. _InkResponseStateWidget

Widget build(BuildContext context) {
  final _ParentInkResponseState? parentState = _ParentInkResponseProvider.of(context);
  return _InkResponseStateWidget(
    onTap: onTap, ...省略

//_InkResponseStateWidget 创建state.   build

//发现在build中还是加入了GestureDetector

@override
Widget build(BuildContext context) {
 ...省略
ParentInkResponseProvider(
  state: this,
  child: Actions(
    actions: _actionMap,
    child: Focus(
       ...省略
      child: MouseRegion(
         ...省略
        child: Semantics(
          onTap: widget.excludeFromSemantics || widget.onTap == null ? null : _simulateTap,
          onLongPress: widget.excludeFromSemantics || widget.onLongPress == null ? null : _simulateLongPress,
          child: GestureDetector(
                onTapDown: enabled ? _handleTapDown : null,
                onTap: enabled ? _handleTap : null,                                                ..省略

//GestureDetector. build

//可以看出里面构建了各种手势识别器

//最后识别器集合丢到RawGestureDetector

@override
Widget build(BuildContext context) {
  final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{};
if (onTapDown != null ||
    onTapUp != null ||
    onTap != null ||
    onTapCancel != null ||
    onSecondaryTap != null ||
    onSecondaryTapDown != null ||
    onSecondaryTapUp != null ||
    onSecondaryTapCancel != null||
    onTertiaryTapDown != null ||
    onTertiaryTapUp != null ||
    onTertiaryTapCancel != null
) {
  gestures[TapGestureRecognizer] = GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
    () => TapGestureRecognizer(debugOwner: this),
    (TapGestureRecognizer instance) {
      instance
        ..onTapDown = onTapDown
        ..onTapUp = onTapUp
        ..onTap = onTap
        ..onTapCancel = onTapCancel
        ..onSecondaryTap = onSecondaryTap
        ..onSecondaryTapDown = onSecondaryTapDown
        ..onSecondaryTapUp = onSecondaryTapUp
        ..onSecondaryTapCancel = onSecondaryTapCancel
        ..onTertiaryTapDown = onTertiaryTapDown
        ..onTertiaryTapUp = onTertiaryTapUp
        ..onTertiaryTapCancel = onTertiaryTapCancel;
    },
  );
}
gestures[DoubleTapGestureRecognizer]=GestureRecognizerFactoryWithHandlers<DoubleTapGestureRecognizer> ..省略
gestures[LongPressGestureRecognizer]=GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>...省略

...省略
return RawGestureDetector(
  gestures: gestures,
  behavior: behavior,
  excludeFromSemantics: excludeFromSemantics,
  child: child,
);

// RawGestureDetector 的State.

//创建旧的识别集合,新的给旧的赋值

@override
void initState() {
  super.initState();
  _semantics = widget.semantics ?? _DefaultSemanticsGestureDelegate(this);
  _syncAll(widget.gestures);
}
void _syncAll(Map<Type, GestureRecognizerFactory> gestures) {
  assert(_recognizers != null);
  final Map<Type, GestureRecognizer> oldRecognizers = _recognizers!;
  _recognizers = <Type, GestureRecognizer>{};
  for (final Type type in gestures.keys) {
    assert(gestures[type] != null);
    assert(gestures[type]!._debugAssertTypeMatches(type));
    assert(!_recognizers!.containsKey(type));
    _recognizers![type] = oldRecognizers[type] ?? gestures[type]!.constructor();
    assert(_recognizers![type].runtimeType == type, 'GestureRecognizerFactory of type $type created a GestureRecognizer of type ${_recognizers![type].runtimeType}. The GestureRecognizerFactory must be specialized with the type of the class that it returns from its constructor method.');
    gestures[type]!.initializer(_recognizers![type]!);
  }
  for (final Type type in oldRecognizers.keys) {
    if (!_recognizers!.containsKey(type))
      oldRecognizers[type]!.dispose();
  }
}

//来到build 

// 这是发现外层套了一个Listener 监听事件. (原始监听事件,不监听鼠标某些事件。->MouseRegion)

_handlePointerDown返回监听的事件, 与之前监听集合匹配知否指定的事件
@override
Widget build(BuildContext context) {
  Widget result = Listener(
    onPointerDown: _handlePointerDown,
    behavior: widget.behavior ?? _defaultBehavior,
    child: widget.child,
  );
  if (!widget.excludeFromSemantics) {
    result = _GestureSemantics(
      behavior: widget.behavior ?? _defaultBehavior,
      assignSemantics: _updateSemanticsForRenderObject,
      child: result,
    );
  }
  return result;
}

//进入 Listener createRenderObject.   实现了一个渲染的可监听组件

//确定了这个渲染组件和widget 组件的交互点 在渲染组件下的手势监听通过这RenderPointerListener 最终传递到. textButton 的onPressed

// GestureDetector 有一个 HitTestBehavior 属性 确定命中后的后续事件处理. 

有三种属性. deferToChild  、  opaque  、translucent

@override
RenderPointerListener createRenderObject(BuildContext context) {
  return RenderPointerListener(
    onPointerDown: onPointerDown,
    onPointerMove: onPointerMove,
    onPointerUp: onPointerUp,
    onPointerHover: onPointerHover,
    onPointerCancel: onPointerCancel,
    onPointerSignal: onPointerSignal,
    behavior: behavior,
  );
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值