create gesture recognizers on attach and dispose on detach to avoid leaks (#42076)
This commit is contained in:
@@ -229,11 +229,6 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
||||
assert(_showCursor != null);
|
||||
assert(!_showCursor.value || cursorColor != null);
|
||||
this.hasFocus = hasFocus ?? false;
|
||||
_tap = TapGestureRecognizer(debugOwner: this)
|
||||
..onTapDown = _handleTapDown
|
||||
..onTap = _handleTap;
|
||||
_longPress = LongPressGestureRecognizer(debugOwner: this)
|
||||
..onLongPress = _handleLongPress;
|
||||
}
|
||||
|
||||
/// Character used to obscure text if [obscureText] is true.
|
||||
@@ -1173,12 +1168,18 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
||||
@override
|
||||
void attach(PipelineOwner owner) {
|
||||
super.attach(owner);
|
||||
_tap = TapGestureRecognizer(debugOwner: this)
|
||||
..onTapDown = _handleTapDown
|
||||
..onTap = _handleTap;
|
||||
_longPress = LongPressGestureRecognizer(debugOwner: this)..onLongPress = _handleLongPress;
|
||||
_offset.addListener(markNeedsPaint);
|
||||
_showCursor.addListener(markNeedsPaint);
|
||||
}
|
||||
|
||||
@override
|
||||
void detach() {
|
||||
_tap.dispose();
|
||||
_longPress.dispose();
|
||||
_offset.removeListener(markNeedsPaint);
|
||||
_showCursor.removeListener(markNeedsPaint);
|
||||
if (_listenerAttached)
|
||||
|
||||
72
packages/flutter/test/rendering/editable_gesture_test.dart
Normal file
72
packages/flutter/test/rendering/editable_gesture_test.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
void main() {
|
||||
setUp(() => _GestureBindingSpy());
|
||||
|
||||
test('attach and detach correctly handle gesture', () {
|
||||
final TextSelectionDelegate delegate = FakeEditableTextState();
|
||||
final RenderEditable editable = RenderEditable(
|
||||
backgroundCursorColor: Colors.grey,
|
||||
selectionColor: Colors.black,
|
||||
textDirection: TextDirection.ltr,
|
||||
cursorColor: Colors.red,
|
||||
offset: ViewportOffset.zero(),
|
||||
textSelectionDelegate: delegate,
|
||||
text: const TextSpan(
|
||||
text: 'test',
|
||||
style: TextStyle(
|
||||
height: 1.0, fontSize: 10.0, fontFamily: 'Ahem',
|
||||
),
|
||||
),
|
||||
startHandleLayerLink: LayerLink(),
|
||||
endHandleLayerLink: LayerLink(),
|
||||
selection: const TextSelection(
|
||||
baseOffset: 0,
|
||||
extentOffset: 3,
|
||||
affinity: TextAffinity.upstream,
|
||||
),
|
||||
onSelectionChanged: (_, __, ___) { },
|
||||
);
|
||||
final PipelineOwner owner = PipelineOwner(onNeedVisualUpdate: () { });
|
||||
final _PointerRouterSpy spy = GestureBinding.instance.pointerRouter;
|
||||
editable.attach(owner);
|
||||
// This should register pointer into GestureBinding.instance.pointerRouter.
|
||||
editable.handleEvent(const PointerDownEvent(), BoxHitTestEntry(editable, const Offset(10,10)));
|
||||
expect(spy.routeCount, greaterThan(0));
|
||||
editable.detach();
|
||||
expect(spy.routeCount, 0);
|
||||
});
|
||||
}
|
||||
|
||||
class _GestureBindingSpy extends AutomatedTestWidgetsFlutterBinding {
|
||||
final PointerRouter _testPointerRouter = _PointerRouterSpy();
|
||||
|
||||
@override
|
||||
PointerRouter get pointerRouter => _testPointerRouter;
|
||||
}
|
||||
|
||||
class FakeEditableTextState extends TextSelectionDelegate with Mock { }
|
||||
|
||||
class _PointerRouterSpy extends PointerRouter {
|
||||
int routeCount = 0;
|
||||
@override
|
||||
void addRoute(int pointer, PointerRoute route, [Matrix4 transform]) {
|
||||
super.addRoute(pointer, route, transform);
|
||||
routeCount++;
|
||||
}
|
||||
|
||||
@override
|
||||
void removeRoute(int pointer, PointerRoute route) {
|
||||
super.removeRoute(pointer, route);
|
||||
routeCount--;
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,11 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import '../rendering/mock_canvas.dart';
|
||||
import '../rendering/recording_canvas.dart';
|
||||
|
||||
Reference in New Issue
Block a user