diff --git a/examples/hello_world/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/examples/hello_world/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000000..18d981003d --- /dev/null +++ b/examples/hello_world/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/examples/hello_world/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/examples/hello_world/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000000..949b678982 --- /dev/null +++ b/examples/hello_world/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + BuildSystemType + Original + + diff --git a/packages/flutter/lib/src/rendering/sliver_multi_box_adaptor.dart b/packages/flutter/lib/src/rendering/sliver_multi_box_adaptor.dart index 75e77415c7..3757425a33 100644 --- a/packages/flutter/lib/src/rendering/sliver_multi_box_adaptor.dart +++ b/packages/flutter/lib/src/rendering/sliver_multi_box_adaptor.dart @@ -121,17 +121,35 @@ abstract class RenderSliverBoxChildManager { /// true without making any assertions. bool debugAssertChildListLocked() => true; } - -/// Parent data structure used by [RenderSliverMultiBoxAdaptor]. -class SliverMultiBoxAdaptorParentData extends SliverLogicalParentData with ContainerParentDataMixin { - /// The index of this child according to the [RenderSliverBoxChildManager]. - int index; - +/// Parent data structure used by [RenderSliverWithKeepAliveMixin]. +mixin KeepAliveParentDataMixin implements ParentData { /// Whether to keep the child alive even when it is no longer visible. bool keepAlive = false; /// Whether the widget is currently being kept alive, i.e. has [keepAlive] set /// to true and is offscreen. + bool get keptAlive; +} + +/// This class exists to dissociate [KeepAlive] from [RenderSliverMultiBoxAdaptor]. +/// +/// [RenderSliverWithKeepAliveMixin.setupParentData] must be implemented to use +/// a parentData class that uses the right mixin or whatever is appropriate. +mixin RenderSliverWithKeepAliveMixin implements RenderSliver { + /// Alerts the developer that the child's parentData needs to be of type + /// [KeepAliveParentDataMixin]. + @override + void setupParentData(RenderObject child) { + assert(child.parentData is KeepAliveParentDataMixin); + } +} + +/// Parent data structure used by [RenderSliverMultiBoxAdaptor]. +class SliverMultiBoxAdaptorParentData extends SliverLogicalParentData with ContainerParentDataMixin, KeepAliveParentDataMixin { + /// The index of this child according to the [RenderSliverBoxChildManager]. + int index; + + @override bool get keptAlive => _keptAlive; bool _keptAlive = false; @@ -166,7 +184,7 @@ class SliverMultiBoxAdaptorParentData extends SliverLogicalParentData with Conta /// * [RenderSliverGrid], which places its children in arbitrary positions. abstract class RenderSliverMultiBoxAdaptor extends RenderSliver with ContainerRenderObjectMixin, - RenderSliverHelpers { + RenderSliverHelpers, RenderSliverWithKeepAliveMixin { /// Creates a sliver with multiple box children. /// @@ -585,4 +603,4 @@ abstract class RenderSliverMultiBoxAdaptor extends RenderSliver } return children; } -} +} \ No newline at end of file diff --git a/packages/flutter/lib/src/widgets/automatic_keep_alive.dart b/packages/flutter/lib/src/widgets/automatic_keep_alive.dart index 170196bea6..b03cc7b055 100644 --- a/packages/flutter/lib/src/widgets/automatic_keep_alive.dart +++ b/packages/flutter/lib/src/widgets/automatic_keep_alive.dart @@ -80,7 +80,7 @@ class _AutomaticKeepAliveState extends State { handle.addListener(_handles[handle]); if (!_keepingAlive) { _keepingAlive = true; - final ParentDataElement childElement = _getChildElement(); + final ParentDataElement childElement = _getChildElement(); if (childElement != null) { // If the child already exists, update it synchronously. _updateParentDataOfChild(childElement); @@ -92,7 +92,7 @@ class _AutomaticKeepAliveState extends State { if (!mounted) { return; } - final ParentDataElement childElement = _getChildElement(); + final ParentDataElement childElement = _getChildElement(); assert(childElement != null); _updateParentDataOfChild(childElement); }); @@ -105,7 +105,7 @@ class _AutomaticKeepAliveState extends State { /// /// While this widget is guaranteed to have a child, this may return null if /// the first build of that child has not completed yet. - ParentDataElement _getChildElement() { + ParentDataElement _getChildElement() { assert(mounted); final Element element = context; Element childElement; @@ -131,11 +131,11 @@ class _AutomaticKeepAliveState extends State { element.visitChildren((Element child) { childElement = child; }); - assert(childElement == null || childElement is ParentDataElement); + assert(childElement == null || childElement is ParentDataElement); return childElement; } - void _updateParentDataOfChild(ParentDataElement childElement) { + void _updateParentDataOfChild(ParentDataElement childElement) { childElement.applyWidgetOutOfTurn(build(context)); } @@ -396,4 +396,4 @@ mixin AutomaticKeepAliveClientMixin on State { _ensureKeepAlive(); return null; } -} +} \ No newline at end of file diff --git a/packages/flutter/lib/src/widgets/sliver.dart b/packages/flutter/lib/src/widgets/sliver.dart index f059d04608..fa800f8267 100644 --- a/packages/flutter/lib/src/widgets/sliver.dart +++ b/packages/flutter/lib/src/widgets/sliver.dart @@ -570,10 +570,21 @@ class SliverChildListDelegate extends SliverChildDelegate { } } +/// A base class for sliver that have [KeepAlive] children. +abstract class SliverWithKeepAliveWidget extends RenderObjectWidget { + /// Initializes fields for subclasses. + const SliverWithKeepAliveWidget({ + Key key, + }) : super(key : key); + + @override + RenderSliverWithKeepAliveMixin createRenderObject(BuildContext context); +} + /// A base class for sliver that have multiple box children. /// /// Helps subclasses build their children lazily using a [SliverChildDelegate]. -abstract class SliverMultiBoxAdaptorWidget extends RenderObjectWidget { +abstract class SliverMultiBoxAdaptorWidget extends SliverWithKeepAliveWidget { /// Initializes fields for subclasses. const SliverMultiBoxAdaptorWidget({ Key key, @@ -1213,7 +1224,7 @@ class SliverFillRemaining extends SingleChildRenderObjectWidget { /// Mark a child as needing to stay alive even when it's in a lazy list that /// would otherwise remove it. /// -/// This widget is for use in [SliverMultiBoxAdaptorWidget]s, such as +/// This widget is for use in [SliverWithKeepAliveWidget]s, such as /// [SliverGrid] or [SliverList]. /// /// This widget is rarely used directly. The [SliverChildBuilderDelegate] and @@ -1230,7 +1241,7 @@ class SliverFillRemaining extends SingleChildRenderObjectWidget { /// In practice, the simplest way to deal with these notifications is to mix /// [AutomaticKeepAliveClientMixin] into one's [State]. See the documentation /// for that mixin class for details. -class KeepAlive extends ParentDataWidget { +class KeepAlive extends ParentDataWidget { /// Marks a child as needing to remain alive. /// /// The [child] and [keepAlive] arguments must not be null. @@ -1249,8 +1260,8 @@ class KeepAlive extends ParentDataWidget { @override void applyParentData(RenderObject renderObject) { - assert(renderObject.parentData is SliverMultiBoxAdaptorParentData); - final SliverMultiBoxAdaptorParentData parentData = renderObject.parentData; + assert(renderObject.parentData is KeepAliveParentDataMixin); + final KeepAliveParentDataMixin parentData = renderObject.parentData; if (parentData.keepAlive != keepAlive) { parentData.keepAlive = keepAlive; final AbstractNode targetParent = renderObject.parent; diff --git a/packages/flutter/test/widgets/automatic_keep_alive_test.dart b/packages/flutter/test/widgets/automatic_keep_alive_test.dart index 06eeec060a..404e633992 100644 --- a/packages/flutter/test/widgets/automatic_keep_alive_test.dart +++ b/packages/flutter/test/widgets/automatic_keep_alive_test.dart @@ -2,6 +2,7 @@ // 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/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -543,6 +544,17 @@ void main() { expect(find.text('FooBar 3'), findsNothing); expect(find.text('FooBar 73'), findsOneWidget); }); + + testWidgets('AutomaticKeepAlive with SliverKeepAliveWidget', (WidgetTester tester) async { + // We're just doing a basic test here to make sure that the functionality of + // RenderSliverWithKeepAliveMixin doesn't get regressed or deleted. As testing + // the full functionality would be cumbersome. + final RenderSliverMultiBoxAdaptorAlt alternate = RenderSliverMultiBoxAdaptorAlt(); + final RenderBox child = RenderBoxKeepAlive(); + alternate.insert(child); + + expect(alternate.children.length, 1); + }); } class _AlwaysKeepAlive extends StatefulWidget { @@ -565,3 +577,57 @@ class _AlwaysKeepAliveState extends State<_AlwaysKeepAlive> with AutomaticKeepAl ); } } + +class RenderBoxKeepAlive extends RenderBox { + State createState() => AlwaysKeepAliveRenderBoxState(); +} + +class AlwaysKeepAliveRenderBoxState extends State<_AlwaysKeepAlive> with AutomaticKeepAliveClientMixin<_AlwaysKeepAlive> { + @override + bool get wantKeepAlive => true; + + @override + Widget build(BuildContext context) { + super.build(context); + return Container( + height: 48.0, + child: const Text('keep me alive'), + ); + } +} + +abstract class KeepAliveParentDataMixinAlt implements KeepAliveParentDataMixin { + @override + bool keptAlive = false; + + @override + bool keepAlive = false; +} + +class RenderSliverMultiBoxAdaptorAlt extends RenderSliver with + KeepAliveParentDataMixinAlt, + RenderSliverHelpers, + RenderSliverWithKeepAliveMixin { + + RenderSliverMultiBoxAdaptorAlt({ + RenderSliverBoxChildManager childManager + }) : _childManager = childManager; + + @protected + RenderSliverBoxChildManager get childManager => _childManager; + final RenderSliverBoxChildManager _childManager; + + final List children = []; + + void insert(RenderBox child, { RenderBox after }) { + children.add(child); + } + + @override + void visitChildren(RenderObjectVisitor visitor) { + children.forEach(visitor); + } + + @override + void performLayout() {} +}