1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/// @docImport 'scroll_view.dart';
6library;
7
8import 'dart:async';
9
10import 'package:flutter/foundation.dart';
11import 'package:flutter/rendering.dart';
12import 'package:flutter/scheduler.dart';
13
14import 'framework.dart';
15import 'notification_listener.dart';
16import 'sliver.dart';
17
18/// Allows subtrees to request to be kept alive in lazy lists.
19///
20/// This widget is like [KeepAlive] but instead of being explicitly configured,
21/// it listens to [KeepAliveNotification] messages from the [child] and other
22/// descendants.
23///
24/// The subtree is kept alive whenever there is one or more descendant that has
25/// sent a [KeepAliveNotification] and not yet triggered its
26/// [KeepAliveNotification.handle].
27///
28/// To send these notifications, consider using [AutomaticKeepAliveClientMixin].
29class AutomaticKeepAlive extends StatefulWidget {
30 /// Creates a widget that listens to [KeepAliveNotification]s and maintains a
31 /// [KeepAlive] widget appropriately.
32 const AutomaticKeepAlive({super.key, required this.child});
33
34 /// The widget below this widget in the tree.
35 ///
36 /// {@macro flutter.widgets.ProxyWidget.child}
37 final Widget child;
38
39 @override
40 State<AutomaticKeepAlive> createState() => _AutomaticKeepAliveState();
41}
42
43class _AutomaticKeepAliveState extends State<AutomaticKeepAlive> {
44 Map<Listenable, VoidCallback>? _handles;
45 // In order to apply parent data out of turn, the child of the KeepAlive
46 // widget must be the same across frames.
47 late Widget _child;
48 bool _keepingAlive = false;
49
50 @override
51 void initState() {
52 super.initState();
53 _updateChild();
54 }
55
56 @override
57 void didUpdateWidget(AutomaticKeepAlive oldWidget) {
58 super.didUpdateWidget(oldWidget);
59 _updateChild();
60 }
61
62 void _updateChild() {
63 _child = NotificationListener<KeepAliveNotification>(
64 onNotification: _addClient,
65 child: widget.child,
66 );
67 }
68
69 @override
70 void dispose() {
71 if (_handles != null) {
72 for (final Listenable handle in _handles!.keys) {
73 handle.removeListener(_handles![handle]!);
74 }
75 }
76 super.dispose();
77 }
78
79 bool _addClient(KeepAliveNotification notification) {
80 final Listenable handle = notification.handle;
81 _handles ??= <Listenable, VoidCallback>{};
82 assert(!_handles!.containsKey(handle));
83 _handles![handle] = _createCallback(handle);
84 handle.addListener(_handles![handle]!);
85 if (!_keepingAlive) {
86 _keepingAlive = true;
87 final ParentDataElement<KeepAliveParentDataMixin>? childElement = _getChildElement();
88 if (childElement != null) {
89 // If the child already exists, update it synchronously.
90 _updateParentDataOfChild(childElement);
91 } else {
92 // If the child doesn't exist yet, we got called during the very first
93 // build of this subtree. Wait until the end of the frame to update
94 // the child when the child is guaranteed to be present.
95 SchedulerBinding.instance.addPostFrameCallback((Duration timeStamp) {
96 if (!mounted) {
97 return;
98 }
99 final ParentDataElement<KeepAliveParentDataMixin>? childElement = _getChildElement();
100 assert(childElement != null);
101 _updateParentDataOfChild(childElement!);
102 }, debugLabel: 'AutomaticKeepAlive.updateParentData');
103 }
104 }
105 return false;
106 }
107
108 /// Get the [Element] for the only [KeepAlive] child.
109 ///
110 /// While this widget is guaranteed to have a child, this may return null if
111 /// the first build of that child has not completed yet.
112 ParentDataElement<KeepAliveParentDataMixin>? _getChildElement() {
113 assert(mounted);
114 final Element element = context as Element;
115 Element? childElement;
116 // We use Element.visitChildren rather than context.visitChildElements
117 // because we might be called during build, and context.visitChildElements
118 // verifies that it is not called during build. Element.visitChildren does
119 // not, instead it assumes that the caller will be careful. (See the
120 // documentation for these methods for more details.)
121 //
122 // Here we know it's safe (with the exception outlined below) because we
123 // just received a notification, which we wouldn't be able to do if we
124 // hadn't built our child and its child -- our build method always builds
125 // the same subtree and it always includes the node we're looking for
126 // (KeepAlive) as the parent of the node that reports the notifications
127 // (NotificationListener).
128 //
129 // If we are called during the first build of this subtree the links to the
130 // children will not be hooked up yet. In that case this method returns
131 // null despite the fact that we will have a child after the build
132 // completes. It's the caller's responsibility to deal with this case.
133 //
134 // (We're only going down one level, to get our direct child.)
135 element.visitChildren((Element child) {
136 childElement = child;
137 });
138 assert(childElement == null || childElement is ParentDataElement<KeepAliveParentDataMixin>);
139 return childElement as ParentDataElement<KeepAliveParentDataMixin>?;
140 }
141
142 void _updateParentDataOfChild(ParentDataElement<KeepAliveParentDataMixin> childElement) {
143 childElement.applyWidgetOutOfTurn(build(context) as ParentDataWidget<KeepAliveParentDataMixin>);
144 }
145
146 VoidCallback _createCallback(Listenable handle) {
147 late final VoidCallback callback;
148 return callback = () {
149 assert(() {
150 if (!mounted) {
151 throw FlutterError(
152 'AutomaticKeepAlive handle triggered after AutomaticKeepAlive was disposed.\n'
153 'Widgets should always trigger their KeepAliveNotification handle when they are '
154 'deactivated, so that they (or their handle) do not send spurious events later '
155 'when they are no longer in the tree.',
156 );
157 }
158 return true;
159 }());
160 _handles!.remove(handle);
161 handle.removeListener(callback);
162 if (_handles!.isEmpty) {
163 if (SchedulerBinding.instance.schedulerPhase.index <
164 SchedulerPhase.persistentCallbacks.index) {
165 // Build/layout haven't started yet so let's just schedule this for
166 // the next frame.
167 setState(() {
168 _keepingAlive = false;
169 });
170 } else {
171 // We were probably notified by a descendant when they were yanked out
172 // of our subtree somehow. We're probably in the middle of build or
173 // layout, so there's really nothing we can do to clean up this mess
174 // short of just scheduling another build to do the cleanup. This is
175 // very unfortunate, and means (for instance) that garbage collection
176 // of these resources won't happen for another 16ms.
177 //
178 // The problem is there's really no way for us to distinguish these
179 // cases:
180 //
181 // * We haven't built yet (or missed out chance to build), but
182 // someone above us notified our descendant and our descendant is
183 // disconnecting from us. If we could mark ourselves dirty we would
184 // be able to clean everything this frame. (This is a pretty
185 // unlikely scenario in practice. Usually things change before
186 // build/layout, not during build/layout.)
187 //
188 // * Our child changed, and as our old child went away, it notified
189 // us. We can't setState, since we _just_ built. We can't apply the
190 // parent data information to our child because we don't _have_ a
191 // child at this instant. We really want to be able to change our
192 // mind about how we built, so we can give the KeepAlive widget a
193 // new value, but it's too late.
194 //
195 // * A deep descendant in another build scope just got yanked, and in
196 // the process notified us. We could apply new parent data
197 // information, but it may or may not get applied this frame,
198 // depending on whether said child is in the same layout scope.
199 //
200 // * A descendant is being moved from one position under us to
201 // another position under us. They just notified us of the removal,
202 // at some point in the future they will notify us of the addition.
203 // We don't want to do anything. (This is why we check that
204 // _handles is still empty below.)
205 //
206 // * We're being notified in the paint phase, or even in a post-frame
207 // callback. Either way it is far too late for us to make our
208 // parent lay out again this frame, so the garbage won't get
209 // collected this frame.
210 //
211 // * We are being torn out of the tree ourselves, as is our
212 // descendant, and it notified us while it was being deactivated.
213 // We don't need to do anything, but we don't know yet because we
214 // haven't been deactivated yet. (This is why we check mounted
215 // below before calling setState.)
216 //
217 // Long story short, we have to schedule a new frame and request a
218 // frame there, but this is generally a bad practice, and you should
219 // avoid it if possible.
220 _keepingAlive = false;
221 scheduleMicrotask(() {
222 if (mounted && _handles!.isEmpty) {
223 // If mounted is false, we went away as well, so there's nothing to do.
224 // If _handles is no longer empty, then another client (or the same
225 // client in a new place) registered itself before we had a chance to
226 // turn off keepalive, so again there's nothing to do.
227 setState(() {
228 assert(!_keepingAlive);
229 });
230 }
231 });
232 }
233 }
234 };
235 }
236
237 @override
238 Widget build(BuildContext context) {
239 return KeepAlive(keepAlive: _keepingAlive, child: _child);
240 }
241
242 @override
243 void debugFillProperties(DiagnosticPropertiesBuilder description) {
244 super.debugFillProperties(description);
245 description.add(
246 FlagProperty('_keepingAlive', value: _keepingAlive, ifTrue: 'keeping subtree alive'),
247 );
248 description.add(
249 DiagnosticsProperty<Map<Listenable, VoidCallback>>(
250 'handles',
251 _handles,
252 description:
253 _handles != null
254 ? '${_handles!.length} active client${_handles!.length == 1 ? "" : "s"}'
255 : null,
256 ifNull: 'no notifications ever received',
257 ),
258 );
259 }
260}
261
262/// Indicates that the subtree through which this notification bubbles must be
263/// kept alive even if it would normally be discarded as an optimization.
264///
265/// For example, a focused text field might fire this notification to indicate
266/// that it should not be disposed even if the user scrolls the field off
267/// screen.
268///
269/// Each [KeepAliveNotification] is configured with a [handle] that consists of
270/// a [Listenable] that is triggered when the subtree no longer needs to be kept
271/// alive.
272///
273/// The [handle] should be triggered any time the sending widget is removed from
274/// the tree (in [State.deactivate]). If the widget is then rebuilt and still
275/// needs to be kept alive, it should immediately send a new notification
276/// (possible with the very same [Listenable]) during build.
277///
278/// This notification is listened to by the [AutomaticKeepAlive] widget, which
279/// is added to the tree automatically by [SliverList] (and [ListView]) and
280/// [SliverGrid] (and [GridView]) widgets.
281///
282/// Failure to trigger the [handle] in the manner described above will likely
283/// cause the [AutomaticKeepAlive] to lose track of whether the widget should be
284/// kept alive or not, leading to memory leaks or lost data. For example, if the
285/// widget that requested keepalive is removed from the subtree but doesn't
286/// trigger its [Listenable] on the way out, then the subtree will continue to
287/// be kept alive until the list itself is disposed. Similarly, if the
288/// [Listenable] is triggered while the widget needs to be kept alive, but a new
289/// [KeepAliveNotification] is not immediately sent, then the widget risks being
290/// garbage collected while it wants to be kept alive.
291///
292/// It is an error to use the same [handle] in two [KeepAliveNotification]s
293/// within the same [AutomaticKeepAlive] without triggering that [handle] before
294/// the second notification is sent.
295///
296/// For a more convenient way to interact with [AutomaticKeepAlive] widgets,
297/// consider using [AutomaticKeepAliveClientMixin], which uses
298/// [KeepAliveNotification] internally.
299class KeepAliveNotification extends Notification {
300 /// Creates a notification to indicate that a subtree must be kept alive.
301 const KeepAliveNotification(this.handle);
302
303 /// A [Listenable] that will inform its clients when the widget that fired the
304 /// notification no longer needs to be kept alive.
305 ///
306 /// The [Listenable] should be triggered any time the sending widget is
307 /// removed from the tree (in [State.deactivate]). If the widget is then
308 /// rebuilt and still needs to be kept alive, it should immediately send a new
309 /// notification (possible with the very same [Listenable]) during build.
310 ///
311 /// See also:
312 ///
313 /// * [KeepAliveHandle], a convenience class for use with this property.
314 final Listenable handle;
315}
316
317/// A [Listenable] which can be manually triggered.
318///
319/// Used with [KeepAliveNotification] objects as their
320/// [KeepAliveNotification.handle].
321///
322/// For a more convenient way to interact with [AutomaticKeepAlive] widgets,
323/// consider using [AutomaticKeepAliveClientMixin], which uses a
324/// [KeepAliveHandle] internally.
325class KeepAliveHandle extends ChangeNotifier {
326 @override
327 void dispose() {
328 notifyListeners();
329 super.dispose();
330 }
331}
332
333/// A mixin with convenience methods for clients of [AutomaticKeepAlive]. Used
334/// with [State] subclasses.
335///
336/// Subclasses must implement [wantKeepAlive], and their [build] methods must
337/// call `super.build` (though the return value should be ignored).
338///
339/// Then, whenever [wantKeepAlive]'s value changes (or might change), the
340/// subclass should call [updateKeepAlive].
341///
342/// The type argument `T` is the type of the [StatefulWidget] subclass of the
343/// [State] into which this class is being mixed.
344///
345/// See also:
346///
347/// * [AutomaticKeepAlive], which listens to messages from this mixin.
348/// * [KeepAliveNotification], the notifications sent by this mixin.
349@optionalTypeArgs
350mixin AutomaticKeepAliveClientMixin<T extends StatefulWidget> on State<T> {
351 KeepAliveHandle? _keepAliveHandle;
352
353 void _ensureKeepAlive() {
354 assert(_keepAliveHandle == null);
355 _keepAliveHandle = KeepAliveHandle();
356 KeepAliveNotification(_keepAliveHandle!).dispatch(context);
357 }
358
359 void _releaseKeepAlive() {
360 _keepAliveHandle!.dispose();
361 _keepAliveHandle = null;
362 }
363
364 /// Whether the current instance should be kept alive.
365 ///
366 /// Call [updateKeepAlive] whenever this getter's value changes.
367 @protected
368 bool get wantKeepAlive;
369
370 /// Ensures that any [AutomaticKeepAlive] ancestors are in a good state, by
371 /// firing a [KeepAliveNotification] or triggering the [KeepAliveHandle] as
372 /// appropriate.
373 @protected
374 void updateKeepAlive() {
375 if (wantKeepAlive) {
376 if (_keepAliveHandle == null) {
377 _ensureKeepAlive();
378 }
379 } else {
380 if (_keepAliveHandle != null) {
381 _releaseKeepAlive();
382 }
383 }
384 }
385
386 @override
387 void initState() {
388 super.initState();
389 if (wantKeepAlive) {
390 _ensureKeepAlive();
391 }
392 }
393
394 @override
395 void deactivate() {
396 if (_keepAliveHandle != null) {
397 _releaseKeepAlive();
398 }
399 super.deactivate();
400 }
401
402 @mustCallSuper
403 @override
404 Widget build(BuildContext context) {
405 if (wantKeepAlive && _keepAliveHandle == null) {
406 _ensureKeepAlive();
407 // Whenever wantKeepAlive's value changes (or might change), the
408 // subclass should call [updateKeepAlive].
409 // That will ensure that the keepalive is disabled (or enabled)
410 // without requiring a rebuild.
411 }
412 return const _NullWidget();
413 }
414}
415
416class _NullWidget extends StatelessWidget {
417 const _NullWidget();
418
419 @override
420 Widget build(BuildContext context) {
421 throw FlutterError(
422 'Widgets that mix AutomaticKeepAliveClientMixin into their State must '
423 'call super.build() but must ignore the return value of the superclass.',
424 );
425 }
426}
427

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com