Unverified Commit 9c8badd1 authored by LongCatIsLooong's avatar LongCatIsLooong Committed by GitHub

Add SliverLayoutBuilder (#35941)

parent 63992e4f
......@@ -11,56 +11,42 @@ import 'framework.dart';
/// The signature of the [LayoutBuilder] builder function.
typedef LayoutWidgetBuilder = Widget Function(BuildContext context, BoxConstraints constraints);
/// Builds a widget tree that can depend on the parent widget's size.
/// An abstract superclass for widgets that defer their building until layout.
///
/// Similar to the [Builder] widget except that the framework calls the [builder]
/// function at layout time and provides the parent widget's constraints. This
/// is useful when the parent constrains the child's size and doesn't depend on
/// the child's intrinsic size. The [LayoutBuilder]'s final size will match its
/// child's size.
///
/// {@youtube 560 315 https://www.youtube.com/watch?v=IYDVcriKjsw}
///
/// If the child should be smaller than the parent, consider wrapping the child
/// in an [Align] widget. If the child might want to be bigger, consider
/// wrapping it in a [SingleChildScrollView].
///
/// See also:
///
/// * [Builder], which calls a `builder` function at build time.
/// * [StatefulBuilder], which passes its `builder` function a `setState` callback.
/// * [CustomSingleChildLayout], which positions its child during layout.
class LayoutBuilder extends RenderObjectWidget {
/// function at layout time and provides the constraints that this widget should
/// adhere to. This is useful when the parent constrains the child's size and layout,
/// and doesn't depend on the child's intrinsic size.
abstract class ConstrainedLayoutBuilder<ConstraintType extends Constraints> extends RenderObjectWidget {
/// Creates a widget that defers its building until layout.
///
/// The [builder] argument must not be null.
const LayoutBuilder({
/// The [builder] argument must not be null, and the returned widget should not
/// be null.
const ConstrainedLayoutBuilder({
Key key,
@required this.builder,
}) : assert(builder != null),
super(key: key);
/// Called at layout time to construct the widget tree. The builder must not
/// return null.
final LayoutWidgetBuilder builder;
@override
_LayoutBuilderElement createElement() => _LayoutBuilderElement(this);
_LayoutBuilderElement<ConstraintType> createElement() => _LayoutBuilderElement<ConstraintType>(this);
@override
_RenderLayoutBuilder createRenderObject(BuildContext context) => _RenderLayoutBuilder();
/// Called at layout time to construct the widget tree.
///
/// The builder must not return null.
final Widget Function(BuildContext, ConstraintType) builder;
// updateRenderObject is redundant with the logic in the LayoutBuilderElement below.
}
class _LayoutBuilderElement extends RenderObjectElement {
_LayoutBuilderElement(LayoutBuilder widget) : super(widget);
class _LayoutBuilderElement<ConstraintType extends Constraints> extends RenderObjectElement {
_LayoutBuilderElement(ConstrainedLayoutBuilder<ConstraintType> widget) : super(widget);
@override
LayoutBuilder get widget => super.widget;
ConstrainedLayoutBuilder<ConstraintType> get widget => super.widget;
@override
_RenderLayoutBuilder get renderObject => super.renderObject;
RenderConstrainedLayoutBuilder<ConstraintType, RenderObject> get renderObject => super.renderObject;
Element _child;
......@@ -79,15 +65,15 @@ class _LayoutBuilderElement extends RenderObjectElement {
@override
void mount(Element parent, dynamic newSlot) {
super.mount(parent, newSlot); // Creates the renderObject.
renderObject.callback = _layout;
renderObject.updateCallback(_layout);
}
@override
void update(LayoutBuilder newWidget) {
void update(ConstrainedLayoutBuilder<ConstraintType> newWidget) {
assert(widget != newWidget);
super.update(newWidget);
assert(widget == newWidget);
renderObject.callback = _layout;
renderObject.updateCallback(_layout);
renderObject.markNeedsLayout();
}
......@@ -101,11 +87,11 @@ class _LayoutBuilderElement extends RenderObjectElement {
@override
void unmount() {
renderObject.callback = null;
renderObject.updateCallback(null);
super.unmount();
}
void _layout(BoxConstraints constraints) {
void _layout(ConstraintType constraints) {
owner.buildScope(this, () {
Widget built;
if (widget.builder != null) {
......@@ -160,41 +146,71 @@ class _LayoutBuilderElement extends RenderObjectElement {
@override
void removeChildRenderObject(RenderObject child) {
final _RenderLayoutBuilder renderObject = this.renderObject;
final RenderConstrainedLayoutBuilder<ConstraintType, RenderObject> renderObject = this.renderObject;
assert(renderObject.child == child);
renderObject.child = null;
assert(renderObject == this.renderObject);
}
}
class _RenderLayoutBuilder extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
_RenderLayoutBuilder({
LayoutCallback<BoxConstraints> callback,
}) : _callback = callback;
LayoutCallback<BoxConstraints> get callback => _callback;
LayoutCallback<BoxConstraints> _callback;
set callback(LayoutCallback<BoxConstraints> value) {
/// Generic mixin for [RenderObject]s created by [ConstrainedLayoutBuilder].
///
/// Provides a callback that should be called at layout time, typically in
/// [RenderObject.performLayout].
mixin RenderConstrainedLayoutBuilder<ConstraintType extends Constraints, ChildType extends RenderObject> on RenderObjectWithChildMixin<ChildType> {
LayoutCallback<ConstraintType> _callback;
/// Change the layout callback.
void updateCallback(LayoutCallback<ConstraintType> value) {
if (value == _callback)
return;
_callback = value;
markNeedsLayout();
}
bool _debugThrowIfNotCheckingIntrinsics() {
assert(() {
if (!RenderObject.debugCheckingIntrinsics) {
throw FlutterError(
'LayoutBuilder does not support returning intrinsic dimensions.\n'
'Calculating the intrinsic dimensions would require running the layout '
'callback speculatively, which might mutate the live render object tree.'
);
}
return true;
}());
return true;
/// Invoke the layout callback.
void layoutAndBuildChild() {
assert(_callback != null);
invokeLayoutCallback(_callback);
}
}
/// Builds a widget tree that can depend on the parent widget's size.
///
/// Similar to the [Builder] widget except that the framework calls the [builder]
/// function at layout time and provides the parent widget's constraints. This
/// is useful when the parent constrains the child's size and doesn't depend on
/// the child's intrinsic size. The [LayoutBuilder]'s final size will match its
/// child's size.
///
/// {@youtube 560 315 https://www.youtube.com/watch?v=IYDVcriKjsw}
///
/// If the child should be smaller than the parent, consider wrapping the child
/// in an [Align] widget. If the child might want to be bigger, consider
/// wrapping it in a [SingleChildScrollView].
///
/// See also:
///
/// * [SliverLayoutBuilder], the sliver counterpart of this widget.
/// * [Builder], which calls a `builder` function at build time.
/// * [StatefulBuilder], which passes its `builder` function a `setState` callback.
/// * [CustomSingleChildLayout], which positions its child during layout.
class LayoutBuilder extends ConstrainedLayoutBuilder<BoxConstraints> {
/// Creates a widget that defers its building until layout.
///
/// The [builder] argument must not be null.
const LayoutBuilder({
Key key,
LayoutWidgetBuilder builder,
}) : super(key: key, builder: builder);
@override
LayoutWidgetBuilder get builder => super.builder;
@override
_RenderLayoutBuilder createRenderObject(BuildContext context) => _RenderLayoutBuilder();
}
class _RenderLayoutBuilder extends RenderBox with RenderObjectWithChildMixin<RenderBox>, RenderConstrainedLayoutBuilder<BoxConstraints, RenderBox> {
@override
double computeMinIntrinsicWidth(double height) {
assert(_debugThrowIfNotCheckingIntrinsics());
......@@ -221,8 +237,7 @@ class _RenderLayoutBuilder extends RenderBox with RenderObjectWithChildMixin<Ren
@override
void performLayout() {
assert(callback != null);
invokeLayoutCallback(callback);
layoutAndBuildChild();
if (child != null) {
child.layout(constraints, parentUsesSize: true);
size = constraints.constrain(child.size);
......@@ -241,6 +256,21 @@ class _RenderLayoutBuilder extends RenderBox with RenderObjectWithChildMixin<Ren
if (child != null)
context.paintChild(child, offset);
}
bool _debugThrowIfNotCheckingIntrinsics() {
assert(() {
if (!RenderObject.debugCheckingIntrinsics) {
throw FlutterError(
'LayoutBuilder does not support returning intrinsic dimensions.\n'
'Calculating the intrinsic dimensions would require running the layout '
'callback speculatively, which might mutate the live render object tree.'
);
}
return true;
}());
return true;
}
}
FlutterErrorDetails _debugReportException(
......
// Copyright 2019 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/foundation.dart';
import 'package:flutter/rendering.dart';
import 'framework.dart';
import 'layout_builder.dart';
/// The signature of the [SliverLayoutBuilder] builder function.
typedef SliverLayoutWidgetBuilder = Widget Function(BuildContext context, SliverConstraints constraints);
/// Builds a sliver widget tree that can depend on its own [SliverConstraints].
///
/// Similar to the [LayoutBuilder] widget except its builder should return a sliver
/// widget, and [SliverLayoutBuilder] is itself a sliver. The framework calls the
/// [builder] function at layout time and provides the current [SliverConstraints].
/// The [SliverLayoutBuilder]'s final [SliverGeometry] will match the [SliverGeometry]
/// of its child.
///
///
/// See also:
///
/// * [LayoutBuilder], the non-sliver version of this widget.
class SliverLayoutBuilder extends ConstrainedLayoutBuilder<SliverConstraints> {
/// Creates a sliver widget that defers its building until layout.
///
/// The [builder] argument must not be null.
const SliverLayoutBuilder({
Key key,
SliverLayoutWidgetBuilder builder,
}) : super(key: key, builder: builder);
/// Called at layout time to construct the widget tree.
///
/// The builder must return a non-null sliver widget.
@override
SliverLayoutWidgetBuilder get builder => super.builder;
@override
_RenderSliverLayoutBuilder createRenderObject(BuildContext context) => _RenderSliverLayoutBuilder();
}
class _RenderSliverLayoutBuilder extends RenderSliver with RenderObjectWithChildMixin<RenderSliver>, RenderConstrainedLayoutBuilder<SliverConstraints, RenderSliver> {
@override
double childMainAxisPosition(RenderObject child) {
assert(child != null);
assert(child == this.child);
return 0;
}
@override
void performLayout() {
layoutAndBuildChild();
child?.layout(constraints, parentUsesSize: true);
geometry = child?.geometry ?? SliverGeometry.zero;
}
@override
void applyPaintTransform(RenderObject child, Matrix4 transform) {
assert(child != null);
assert(child == this.child);
// child's offset is always (0, 0), transform.translate(0, 0) does not mutate the transform.
}
@override
void paint(PaintingContext context, Offset offset) {
// This renderObject does not introduce additional offset to child's position.
if (child?.geometry?.visible == true)
context.paintChild(child, offset);
}
@override
bool hitTestChildren(SliverHitTestResult result, {double mainAxisPosition, double crossAxisPosition}) {
return child != null
&& child.geometry.hitTestExtent > 0
&& child.hitTest(result, mainAxisPosition: mainAxisPosition, crossAxisPosition: crossAxisPosition);
}
}
......@@ -93,6 +93,7 @@ export 'src/widgets/shortcuts.dart';
export 'src/widgets/single_child_scroll_view.dart';
export 'src/widgets/size_changed_layout_notifier.dart';
export 'src/widgets/sliver.dart';
export 'src/widgets/sliver_layout_builder.dart';
export 'src/widgets/sliver_persistent_header.dart';
export 'src/widgets/sliver_prototype_extent_list.dart';
export 'src/widgets/spacer.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/src/rendering/sliver.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
......@@ -55,5 +56,48 @@ void main() {
return StatefulWrapper(key: key, child: Container(height: 100.0));
}),
);
expect(tester.takeException(), null);
});
testWidgets('Moving global key inside a SliverLayoutBuilder', (WidgetTester tester) async {
final GlobalKey<StatefulWrapperState> key = GlobalKey<StatefulWrapperState>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
slivers: <Widget>[
SliverLayoutBuilder(
builder: (BuildContext context, SliverConstraints constraint) {
return SliverToBoxAdapter(
child: Wrapper(child: StatefulWrapper(key: key, child: Container(height: 100.0))),
);
},
),
],
),
),
);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
slivers: <Widget>[
SliverLayoutBuilder(
builder: (BuildContext context, SliverConstraints constraint) {
key.currentState.trigger();
return SliverToBoxAdapter(
child: StatefulWrapper(key: key, child: Container(height: 100.0)),
);
},
),
],
),
),
);
expect(tester.takeException(), null);
});
}
......@@ -2,9 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/src/rendering/sliver.dart';
import 'package:flutter/src/widgets/basic.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/layout_builder.dart';
import 'package:flutter/src/widgets/sliver_layout_builder.dart';
import 'package:flutter/src/widgets/scroll_view.dart';
import 'package:flutter_test/flutter_test.dart';
class Wrapper extends StatelessWidget {
......@@ -63,5 +66,64 @@ void main() {
),
],
));
expect(tester.takeException(), null);
});
testWidgets('Moving a global key from another SliverLayoutBuilder at layout time', (WidgetTester tester) async {
final GlobalKey victimKey1 = GlobalKey();
final GlobalKey victimKey2 = GlobalKey();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
slivers: <Widget>[
SliverLayoutBuilder(
builder: (BuildContext context, SliverConstraints constraint) {
return SliverPadding(key: victimKey1, padding: const EdgeInsets.fromLTRB(1, 2, 3, 4));
},
),
SliverLayoutBuilder(
builder: (BuildContext context, SliverConstraints constraint) {
return SliverPadding(key: victimKey2, padding: const EdgeInsets.fromLTRB(5, 7, 11, 13));
},
),
SliverLayoutBuilder(
builder: (BuildContext context, SliverConstraints constraint) {
return const SliverPadding(padding: EdgeInsets.fromLTRB(5, 7, 11, 13));
},
),
],
),
),
);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
slivers: <Widget>[
SliverLayoutBuilder(
builder: (BuildContext context, SliverConstraints constraint) {
return SliverPadding(key: victimKey2, padding: const EdgeInsets.fromLTRB(1, 2, 3, 4));
},
),
SliverLayoutBuilder(
builder: (BuildContext context, SliverConstraints constraint) {
return const SliverPadding(padding: EdgeInsets.fromLTRB(5, 7, 11, 13));
},
),
SliverLayoutBuilder(
builder: (BuildContext context, SliverConstraints constraint) {
return SliverPadding(key: victimKey1, padding: const EdgeInsets.fromLTRB(5, 7, 11, 13));
},
),
],
),
),
);
expect(tester.takeException(), null);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment