Skip to content

Commit c4ba26e

Browse files
authored
Revert "Let InkWell/Ink/ancestor support GlobalKey so that splash does not stop when changing position. (#71138)" (#79300)
This reverts commit e7266db.
1 parent a603714 commit c4ba26e

8 files changed

Lines changed: 144 additions & 499 deletions

File tree

‎packages/flutter/lib/src/material/ink_decoration.dart‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,9 @@ class _InkState extends State<Ink> {
251251

252252
@override
253253
void deactivate() {
254-
_ink?.visible = false;
255-
super.deactivate();
256-
}
257-
258-
@override
259-
void reactivate() {
260-
_ink?.visible = true;
261-
super.reactivate();
262-
}
263-
264-
@override
265-
void dispose() {
266254
_ink?.dispose();
267255
assert(_ink == null);
268-
super.dispose();
256+
super.deactivate();
269257
}
270258

271259
Widget _build(BuildContext context) {

‎packages/flutter/lib/src/material/ink_well.dart‎

Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,6 @@ class _InkResponseState extends State<_InkResponseStateWidget>
733733
implements _ParentInkResponseState {
734734
Set<InteractiveInkFeature>? _splashes;
735735
InteractiveInkFeature? _currentSplash;
736-
bool _active = true;
737736
bool _hovering = false;
738737
final Map<_HighlightType, InkHighlight?> _highlights = <_HighlightType, InkHighlight?>{};
739738
late final Map<Type, Action<Intent>> _actionMap = <Type, Action<Intent>>{
@@ -780,16 +779,7 @@ class _InkResponseState extends State<_InkResponseStateWidget>
780779
@override
781780
void didUpdateWidget(_InkResponseStateWidget oldWidget) {
782781
super.didUpdateWidget(oldWidget);
783-
final InkFeature? validInkFeature = _getSingleInkFeature();
784-
if (validInkFeature != null && !identical(validInkFeature.controller, Material.of(context)!)) {
785-
_removeAllFeatures();
786-
if (_hovering && enabled)
787-
updateHighlight(_HighlightType.hover, value: _hovering, callOnHover: false);
788-
_updateFocusHighlights();
789-
return;
790-
}
791-
792-
if (enabled != _isWidgetEnabled(oldWidget)) {
782+
if (_isWidgetEnabled(widget) != _isWidgetEnabled(oldWidget)) {
793783
if (enabled) {
794784
// Don't call wigdet.onHover because many wigets, including the button
795785
// widgets, apply setState to an ancestor context from onHover.
@@ -799,47 +789,12 @@ class _InkResponseState extends State<_InkResponseStateWidget>
799789
}
800790
}
801791

802-
InkFeature? _getSingleInkFeature() {
803-
final List<InkFeature?> inkFeatures = <InkFeature?>[...?_splashes, ..._highlights.values];
804-
assert(() {
805-
MaterialInkController? lastController;
806-
for (final InkFeature? inkFeature in inkFeatures) {
807-
if (inkFeature == null)
808-
continue;
809-
final MaterialInkController controller = inkFeature.controller;
810-
if (lastController != null && !identical(controller, lastController))
811-
return false;
812-
lastController = controller;
813-
}
814-
return true;
815-
}());
816-
final InkFeature? validInkFeature = inkFeatures.firstWhere((InkFeature? inkFeature) => inkFeature != null, orElse: () => null);
817-
return validInkFeature;
818-
}
819-
820792
@override
821793
void dispose() {
822-
_removeAllFeatures();
823794
FocusManager.instance.removeHighlightModeListener(_handleFocusHighlightModeChange);
824795
super.dispose();
825796
}
826797

827-
void _removeAllFeatures() {
828-
if (_splashes != null) {
829-
final Set<InteractiveInkFeature> splashes = _splashes!;
830-
_splashes = null;
831-
for (final InteractiveInkFeature splash in splashes)
832-
splash.dispose();
833-
_currentSplash = null;
834-
}
835-
assert(_currentSplash == null);
836-
for (final _HighlightType highlight in _highlights.keys) {
837-
_highlights[highlight]?.dispose();
838-
_highlights[highlight] = null;
839-
}
840-
widget.parentState?.markChildInkResponsePressed(this, false);
841-
}
842-
843798
@override
844799
bool get wantKeepAlive => highlightsExist || (_splashes != null && _splashes!.isNotEmpty);
845800

@@ -875,8 +830,7 @@ class _InkResponseState extends State<_InkResponseStateWidget>
875830
void handleInkRemoval() {
876831
assert(_highlights[type] != null);
877832
_highlights[type] = null;
878-
if (_active)
879-
updateKeepAlive();
833+
updateKeepAlive();
880834
}
881835

882836
if (type == _HighlightType.pressed) {
@@ -1059,26 +1013,24 @@ class _InkResponseState extends State<_InkResponseStateWidget>
10591013
}
10601014
}
10611015

1062-
void _setAllFeaturesVisible(bool visible) {
1063-
for (final InkFeature? splash in <InkFeature?>[...?_splashes, ..._highlights.values])
1064-
splash?.visible = visible;
1065-
}
1066-
10671016
@override
10681017
void deactivate() {
1069-
_active = !_active;
1070-
_setAllFeaturesVisible(false);
1018+
if (_splashes != null) {
1019+
final Set<InteractiveInkFeature> splashes = _splashes!;
1020+
_splashes = null;
1021+
for (final InteractiveInkFeature splash in splashes)
1022+
splash.dispose();
1023+
_currentSplash = null;
1024+
}
1025+
assert(_currentSplash == null);
1026+
for (final _HighlightType highlight in _highlights.keys) {
1027+
_highlights[highlight]?.dispose();
1028+
_highlights[highlight] = null;
1029+
}
1030+
widget.parentState?.markChildInkResponsePressed(this, false);
10711031
super.deactivate();
10721032
}
10731033

1074-
@override
1075-
void reactivate() {
1076-
_active = !_active;
1077-
_setAllFeaturesVisible(true);
1078-
updateKeepAlive();
1079-
super.reactivate();
1080-
}
1081-
10821034
bool _isWidgetEnabled(_InkResponseStateWidget widget) {
10831035
return widget.onTap != null || widget.onDoubleTap != null || widget.onLongPress != null;
10841036
}
@@ -1249,10 +1201,6 @@ class _InkResponseState extends State<_InkResponseStateWidget>
12491201
/// during animation. You should avoid using InkWells within [Material] widgets
12501202
/// that are changing size.
12511203
///
1252-
/// Animations triggered by an [InkWell] will survive their widget moving due
1253-
/// to [GlobalKey] reparenting, as long as the nearest [Material] ancestor is
1254-
/// the same before and after the move.
1255-
///
12561204
/// See also:
12571205
///
12581206
/// * [GestureDetector], for listening for gestures without ink splashes.

‎packages/flutter/lib/src/material/material.dart‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -544,20 +544,12 @@ class _RenderInkFeatures extends RenderProxyBox implements MaterialInkController
544544
canvas.save();
545545
canvas.translate(offset.dx, offset.dy);
546546
canvas.clipRect(Offset.zero & size);
547-
for (final InkFeature inkFeature in _inkFeatures!) {
548-
if (inkFeature.visible)
549-
inkFeature._paint(canvas);
550-
}
547+
for (final InkFeature inkFeature in _inkFeatures!)
548+
inkFeature._paint(canvas);
551549
canvas.restore();
552550
}
553551
super.paint(context, offset);
554552
}
555-
556-
void dispose() {
557-
// [InkFeature.dispose] will eventually call [_inkFeatures!.remove].
558-
while (_inkFeatures?.isNotEmpty == true)
559-
_inkFeatures!.first.dispose();
560-
}
561553
}
562554

563555
class _InkFeatures extends SingleChildRenderObjectWidget {
@@ -593,11 +585,6 @@ class _InkFeatures extends SingleChildRenderObjectWidget {
593585
..absorbHitTest = absorbHitTest;
594586
assert(vsync == renderObject.vsync);
595587
}
596-
597-
@override
598-
void didUnmountRenderObject(_RenderInkFeatures renderObject) {
599-
renderObject.dispose();
600-
}
601588
}
602589

603590
/// A visual reaction on a piece of [Material].
@@ -630,15 +617,6 @@ abstract class InkFeature {
630617

631618
bool _debugDisposed = false;
632619

633-
/// Whether or not visual reaction is activated.
634-
///
635-
/// Change this field will affect whether this InkFeature is render in next
636-
/// frame.
637-
///
638-
/// For this InkFeature to render properly, it should usually be change in
639-
/// [State.deactivate] and [State.reactivate].
640-
bool visible = true;
641-
642620
/// Free up the resources associated with this ink feature.
643621
@mustCallSuper
644622
void dispose() {

‎packages/flutter/lib/src/material/mergeable_material.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ class _MergeableMaterialState extends State<MergeableMaterial> with TickerProvid
584584
}
585585

586586
child = AnimatedContainer(
587+
key: _MergeableMaterialSliceKey(_children[i].key),
587588
decoration: BoxDecoration(border: border),
588589
duration: kThemeAnimationDuration,
589590
curve: Curves.fastOutSlowIn,
@@ -599,7 +600,6 @@ class _MergeableMaterialState extends State<MergeableMaterial> with TickerProvid
599600
shape: BoxShape.rectangle,
600601
),
601602
child: Material(
602-
key: _MergeableMaterialSliceKey(_children[i].key),
603603
type: MaterialType.transparency,
604604
child: child,
605605
),

‎packages/flutter/lib/src/widgets/framework.dart‎

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -930,12 +930,6 @@ abstract class State<T extends StatefulWidget> with Diagnosticable {
930930
/// It is an error to call [setState] unless [mounted] is true.
931931
bool get mounted => _element != null;
932932

933-
/// This field is used tracks [reactivate] and [deactivate], to assert that
934-
/// they are called alternatively.
935-
///
936-
/// This field is not set in release mode.
937-
bool _debugActive = true;
938-
939933
/// Called when this object is inserted into the tree.
940934
///
941935
/// The framework will call this method exactly once for each [State] object
@@ -1116,17 +1110,17 @@ abstract class State<T extends StatefulWidget> with Diagnosticable {
11161110
_element!.markNeedsBuild();
11171111
}
11181112

1119-
/// Whenever the framework removes this [State] object from the tree, the
1120-
/// framework will call this method.
1113+
/// Called when this object is removed from the tree.
11211114
///
1122-
/// In some cases, the framework will reinsert the [State] object into
1123-
/// another part of the tree (e.g., if the subtree containing this [State]
1124-
/// object is grafted from one location in the tree to another). If that
1125-
/// happens, the framework will ensure that it calls [reactivate] to give the
1126-
/// [State] object a chance to adapt to its new location in the tree. If the
1127-
/// framework does reinsert this subtree, it will do so before the end of the
1128-
/// animation frame in which the subtree was removed from the tree. For this
1129-
/// reason, [State] objects can defer releasing most resources until the
1115+
/// The framework calls this method whenever it removes this [State] object
1116+
/// from the tree. In some cases, the framework will reinsert the [State]
1117+
/// object into another part of the tree (e.g., if the subtree containing this
1118+
/// [State] object is grafted from one location in the tree to another). If
1119+
/// that happens, the framework will ensure that it calls [build] to give the
1120+
/// [State] object a chance to adapt to its new location in the tree. If
1121+
/// the framework does reinsert this subtree, it will do so before the end of
1122+
/// the animation frame in which the subtree was removed from the tree. For
1123+
/// this reason, [State] objects can defer releasing most resources until the
11301124
/// framework calls their [dispose] method.
11311125
///
11321126
/// Subclasses should override this method to clean up any links between
@@ -1142,35 +1136,7 @@ abstract class State<T extends StatefulWidget> with Diagnosticable {
11421136
/// from the tree permanently.
11431137
@protected
11441138
@mustCallSuper
1145-
void deactivate() {
1146-
assert(() {
1147-
_debugActive = !_debugActive;
1148-
return !_debugActive;
1149-
}());
1150-
}
1151-
1152-
/// Called when this object is reactivated.
1153-
///
1154-
/// If the [widget] or one of its ancestors has a [GlobalKey], the framework
1155-
/// will mark this object as inactive when it is removed and call
1156-
/// [deactivate].
1157-
///
1158-
/// If the object is reinserted to the tree in the next frame (e.g. by
1159-
/// changing position), it will be marked as active again and this method will be
1160-
/// called.
1161-
///
1162-
/// See also:
1163-
///
1164-
/// * [Element.activate] and [Element.deactivate] for more information about
1165-
/// lifecycle.
1166-
@protected
1167-
@mustCallSuper
1168-
void reactivate() {
1169-
assert(() {
1170-
_debugActive = !_debugActive;
1171-
return _debugActive;
1172-
}());
1173-
}
1139+
void deactivate() { }
11741140

11751141
/// Called when this object is removed from the tree permanently.
11761142
///
@@ -4811,7 +4777,6 @@ class StatefulElement extends ComponentElement {
48114777
@override
48124778
void activate() {
48134779
super.activate();
4814-
state.reactivate();
48154780
// Since the State could have observed the deactivate() and thus disposed of
48164781
// resources allocated in the build method, we have to rebuild the widget
48174782
// so that its State can reallocate its resources.

‎packages/flutter/test/material/ink_paint_test.dart‎

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -431,67 +431,4 @@ void main() {
431431
throw 'Expected: paint.color.alpha == 0, found: ${paint.color.alpha}';
432432
}));
433433
});
434-
435-
// Regression test for https://github.com/flutter/flutter/issues/6751
436-
testWidgets('When Ink has a GlobalKey and changes position, splash should not stop', (WidgetTester tester) async {
437-
const Color color = Colors.blue;
438-
const Color splashColor = Colors.green;
439-
440-
void expectTest(bool painted) {
441-
final PaintPattern paintPattern = paints
442-
..rect(color: Color(color.value))
443-
..circle(color: Color(splashColor.value));
444-
445-
expect(
446-
Material.of(tester.element(find.byType(InkWell)))! as RenderBox,
447-
painted ? paintPattern : isNot(paintPattern),
448-
);
449-
}
450-
451-
bool wrap = false;
452-
final Key globalKey = GlobalKey();
453-
await tester.pumpWidget(Directionality(
454-
textDirection: TextDirection.ltr,
455-
child: Material(
456-
child: Center(
457-
child: StatefulBuilder(
458-
builder: (BuildContext context, void Function(void Function()) setState) {
459-
Widget child = Ink(
460-
key: globalKey,
461-
color: color,
462-
width: 200.0,
463-
height: 200.0,
464-
child: InkWell(
465-
splashColor: splashColor,
466-
onTap: () { },
467-
onTapDown: (_) async {
468-
await Future<void>.delayed(const Duration(milliseconds: 50));
469-
470-
setState(() {
471-
wrap = !wrap;
472-
});
473-
}
474-
),
475-
);
476-
477-
if (wrap) {
478-
child = Container(
479-
margin: const EdgeInsets.only(top: 320),
480-
child: child,
481-
);
482-
}
483-
484-
return child;
485-
}
486-
),
487-
),
488-
),
489-
));
490-
final TestGesture testGesture = await tester.startGesture(tester.getRect(find.byType(InkWell)).center);
491-
await tester.pump(const Duration(milliseconds: 60));
492-
expectTest(true);
493-
await testGesture.up();
494-
await tester.pumpAndSettle();
495-
expectTest(false);
496-
});
497434
}

0 commit comments

Comments
 (0)