From e427c2dd8673213565e615f645d7b03a20ff565c Mon Sep 17 00:00:00 2001 From: liyuqian Date: Mon, 29 Apr 2019 11:25:41 -0700 Subject: [PATCH] Fix text field selection toolbar under Opacity (#31097) ## Description As we've introduced offset to the Opacity layer, we have to override `applyTransform` to make Leader/FollowerLayer work correctly. ## Related Issues Fixes https://github.com/flutter/flutter/issues/30587 Together with https://github.com/flutter/engine/pull/8585, this test will also exercise test against https://github.com/flutter/flutter/issues/30586. ## Tests I added the following tests: * text field selection toolbar renders correctly inside opacity --- packages/flutter/lib/src/rendering/layer.dart | 7 +++ .../test/material/text_field_test.dart | 53 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/packages/flutter/lib/src/rendering/layer.dart b/packages/flutter/lib/src/rendering/layer.dart index 1893848336..be6d8db69b 100644 --- a/packages/flutter/lib/src/rendering/layer.dart +++ b/packages/flutter/lib/src/rendering/layer.dart @@ -1282,6 +1282,13 @@ class OpacityLayer extends ContainerLayer { } } + @override + void applyTransform(Layer child, Matrix4 transform) { + assert(child != null); + assert(transform != null); + transform.translate(offset.dx, offset.dy); + } + @override ui.EngineLayer addToScene(ui.SceneBuilder builder, [ Offset layerOffset = Offset.zero ]) { bool enabled = firstChild != null; // don't add this layer if there's no child diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 746449219d..4ea45e6009 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:io'; import 'dart:math' as math; import 'dart:ui' as ui show window; @@ -380,6 +381,58 @@ void main() { expect(textField.cursorRadius, const Radius.circular(3.0)); }); + testWidgets('text field selection toolbar renders correctly inside opacity', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Center( + child: Container( + width: 100, + height: 100, + child: const Opacity( + opacity: 0.5, + child: TextField( + decoration: InputDecoration(hintText: 'Placeholder'), + ), + ), + ), + ), + ), + ), + ); + + await tester.showKeyboard(find.byType(TextField)); + + const String testValue = 'A B C'; + tester.testTextInput.updateEditingValue( + const TextEditingValue( + text: testValue + ) + ); + await tester.pump(); + + // The selectWordsInRange with SelectionChangedCause.tap seems to be needed to show the toolbar. + // (This is true even if we provide selection parameter to the TextEditingValue above.) + final EditableTextState state = tester.state(find.byType(EditableText)); + state.renderEditable.selectWordsInRange(from: const Offset(0, 0), cause: SelectionChangedCause.tap); + + expect(state.showToolbar(), true); + + // This is needed for the AnimatedOpacity to turn from 0 to 1 so the toolbar is visible. + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + + // Sanity check that the toolbar widget exists. + expect(find.text('PASTE'), findsOneWidget); + + await expectLater( + // The toolbar exists in the Overlay above the MaterialApp. + find.byType(Overlay), + matchesGoldenFile('text_field_opacity_test.0.0.png'), + skip: !Platform.isLinux, + ); + }); + // TODO(hansmuller): restore these tests after the fix for #24876 has landed. /* testWidgets('cursor layout has correct width', (WidgetTester tester) async {