Commit cc9d602b authored by Adam Barth's avatar Adam Barth

Block should work inside LazyBlock (#3546)

Previously we were locking down the state even when calling layout in
LazyBlock. Now we lock only when building children. Making this work well
involved moving the catch out of lockState and into the few callers who
actually wanted it.

Fixes #3534
parent 00f10da1
...@@ -285,7 +285,7 @@ class RenderObjectToWidgetAdapter<T extends RenderObject> extends RenderObjectWi ...@@ -285,7 +285,7 @@ class RenderObjectToWidgetAdapter<T extends RenderObject> extends RenderObjectWi
} else { } else {
element.update(this); element.update(this);
} }
}, building: true, context: 'while attaching root widget to rendering tree'); }, building: true);
return element; return element;
} }
......
...@@ -710,7 +710,7 @@ class BuildOwner { ...@@ -710,7 +710,7 @@ class BuildOwner {
/// ///
/// The context argument is used to describe the scope in case an exception is /// The context argument is used to describe the scope in case an exception is
/// caught while invoking the callback. /// caught while invoking the callback.
void lockState(void callback(), { bool building: false, String context }) { void lockState(void callback(), { bool building: false }) {
bool debugPreviouslyBuilding; bool debugPreviouslyBuilding;
assert(_debugStateLockLevel >= 0); assert(_debugStateLockLevel >= 0);
assert(() { assert(() {
...@@ -723,8 +723,6 @@ class BuildOwner { ...@@ -723,8 +723,6 @@ class BuildOwner {
}); });
try { try {
callback(); callback();
} catch (e, stack) {
_debugReportException(context, e, stack);
} finally { } finally {
assert(() { assert(() {
_debugStateLockLevel -= 1; _debugStateLockLevel -= 1;
...@@ -758,23 +756,25 @@ class BuildOwner { ...@@ -758,23 +756,25 @@ class BuildOwner {
if (_dirtyElements.isEmpty) if (_dirtyElements.isEmpty)
return; return;
Timeline.startSync('Build'); Timeline.startSync('Build');
lockState(() { try {
_dirtyElements.sort(_elementSort); lockState(() {
int dirtyCount = _dirtyElements.length; _dirtyElements.sort(_elementSort);
int index = 0; int dirtyCount = _dirtyElements.length;
while (index < dirtyCount) { int index = 0;
_dirtyElements[index].rebuild(); while (index < dirtyCount) {
index += 1; _dirtyElements[index].rebuild();
if (dirtyCount < _dirtyElements.length) { index += 1;
_dirtyElements.sort(_elementSort); if (dirtyCount < _dirtyElements.length) {
dirtyCount = _dirtyElements.length; _dirtyElements.sort(_elementSort);
dirtyCount = _dirtyElements.length;
}
} }
} assert(!_dirtyElements.any((BuildableElement element) => element.dirty));
assert(!_dirtyElements.any((BuildableElement element) => element.dirty)); }, building: true);
} finally {
_dirtyElements.clear(); _dirtyElements.clear();
}, building: true, context: 'while rebuilding dirty elements'); Timeline.finishSync();
assert(_dirtyElements.isEmpty); }
Timeline.finishSync();
} }
/// Complete the element build pass by unmounting any elements that are no /// Complete the element build pass by unmounting any elements that are no
...@@ -789,12 +789,17 @@ class BuildOwner { ...@@ -789,12 +789,17 @@ class BuildOwner {
/// about changes to global keys will run. /// about changes to global keys will run.
void finalizeTree() { void finalizeTree() {
Timeline.startSync('Finalize tree'); Timeline.startSync('Finalize tree');
lockState(() { try {
_inactiveElements._unmountAll(); lockState(() {
}, context: 'while finalizing the widget tree'); _inactiveElements._unmountAll();
assert(GlobalKey._debugCheckForDuplicates); });
scheduleMicrotask(GlobalKey._notifyListeners); assert(GlobalKey._debugCheckForDuplicates);
Timeline.finishSync(); scheduleMicrotask(GlobalKey._notifyListeners);
} catch (e, stack) {
_debugReportException('while finalizing the widget tree', e, stack);
} finally {
Timeline.finishSync();
}
} }
/// Cause the entire subtree rooted at the given [Element] to /// Cause the entire subtree rooted at the given [Element] to
......
...@@ -164,7 +164,7 @@ abstract class VirtualViewportElement extends RenderObjectElement { ...@@ -164,7 +164,7 @@ abstract class VirtualViewportElement extends RenderObjectElement {
assert(startOffsetBase != null); assert(startOffsetBase != null);
assert(startOffsetLimit != null); assert(startOffsetLimit != null);
_updatePaintOffset(); _updatePaintOffset();
owner.lockState(_materializeChildren, building: true, context: 'during $runtimeType layout'); owner.lockState(_materializeChildren, building: true);
} }
void _materializeChildren() { void _materializeChildren() {
......
// Copyright 2016 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_test/flutter_test.dart';
import 'package:flutter/material.dart';
import 'package:test/test.dart';
void main() {
test('Block inside LazyBlock', () {
testWidgets((WidgetTester tester) {
tester.pumpWidget(new LazyBlock(
delegate: new LazyBlockChildren(
children: <Widget>[
new Block(
children: <Widget>[
new Text('1'),
new Text('2'),
new Text('3'),
]
),
new Block(
children: <Widget>[
new Text('4'),
new Text('5'),
new Text('6'),
]
),
]
)
));
});
});
}
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