Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix widget built twice during warm up frame #39079

Merged
merged 1 commit into from Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/flutter/lib/src/widgets/binding.dart
Expand Up @@ -782,6 +782,17 @@ mixin WidgetsBinding on BindingBase, SchedulerBinding, GestureBinding, RendererB
Element get renderViewElement => _renderViewElement;
Element _renderViewElement;

/// Schedules a [Timer] for attaching the root widget.
///
/// This is called by [runApp] to configure the widget tree. Consider using
/// [attachRootWidget] if you want to build the widget tree synchronously.
@protected
void scheduleAttachRootWidget(Widget rootWidget) {
Timer.run(() {
attachRootWidget(rootWidget);
});
}

/// Takes a widget and attaches it to the [renderViewElement], creating it if
/// necessary.
///
Expand Down Expand Up @@ -843,7 +854,7 @@ mixin WidgetsBinding on BindingBase, SchedulerBinding, GestureBinding, RendererB
/// ensure the widget, element, and render trees are all built.
void runApp(Widget app) {
WidgetsFlutterBinding.ensureInitialized()
..attachRootWidget(app)
..scheduleAttachRootWidget(app)
..scheduleWarmUpFrame();
}

Expand Down
9 changes: 2 additions & 7 deletions packages/flutter/lib/src/widgets/nested_scroll_view.dart
Expand Up @@ -498,13 +498,8 @@ class _NestedScrollCoordinator implements ScrollActivityDelegate, ScrollHoldCont

bool get hasScrolledBody {
for (_NestedScrollPosition position in _innerPositions) {
// TODO(chunhtai): Replace null check with assert once
// https://github.com/flutter/flutter/issues/31195 is fixed.
if (
position.minScrollExtent != null &&
position.pixels != null &&
position.pixels > position.minScrollExtent
) {
assert(position.minScrollExtent != null && position.pixels != null);
if (position.pixels > position.minScrollExtent) {
return true;
}
}
Expand Down
72 changes: 0 additions & 72 deletions packages/flutter/test/widgets/nested_scroll_view_async_test.dart

This file was deleted.

31 changes: 31 additions & 0 deletions packages/flutter/test/widgets/run_app_async_test.dart
@@ -0,0 +1,31 @@
// 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/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:quiver/testing/async.dart';

void main() {
setUp(() {
WidgetsFlutterBinding.ensureInitialized();
WidgetsBinding.instance.resetEpoch();
});

test('WidgetBinding build rendering tree and warm up frame back to back', () {
final FakeAsync fakeAsync = FakeAsync();
fakeAsync.run((FakeAsync async) {
runApp(
const MaterialApp(
home: Material(
child: Text('test'),
),
),
);
// Rendering tree is not built synchronously.
expect(WidgetsBinding.instance.renderViewElement, isNull);
fakeAsync.flushTimers();
expect(WidgetsBinding.instance.renderViewElement, isNotNull);
});
});
}
8 changes: 8 additions & 0 deletions packages/flutter_test/lib/src/binding.dart
Expand Up @@ -988,6 +988,14 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
_currentFakeAsync.flushMicrotasks();
}

@override
void scheduleAttachRootWidget(Widget rootWidget) {
// We override the default version of this so that the application-startup widget tree
// build does not schedule timers which we might never get around to running.
attachRootWidget(rootWidget);
_currentFakeAsync.flushMicrotasks();
}

@override
Future<void> idle() {
final Future<void> result = super.idle();
Expand Down