Skip to content

fix widget built twice during warm up frame #39079

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/flutter/lib/src/widgets/binding.dart
Original file line number Diff line number Diff line change
@@ -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.
///
@@ -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();
}

9 changes: 2 additions & 7 deletions packages/flutter/lib/src/widgets/nested_scroll_view.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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();