layout_builder_and_state_test.dart 2.07 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:flutter_test/flutter_test.dart';
6 7 8 9
import 'package:flutter/widgets.dart';
import 'test_widgets.dart';

class StatefulWrapper extends StatefulWidget {
10
  const StatefulWrapper({
11 12 13 14 15 16 17
    Key key,
    this.child,
  }) : super(key: key);

  final Widget child;

  @override
18
  StatefulWrapperState createState() => StatefulWrapperState();
19 20 21 22 23 24 25 26 27 28 29 30 31
}

class StatefulWrapperState extends State<StatefulWrapper> {

  void trigger() {
    setState(() { /* no-op setState */ });
  }

  bool built = false;

  @override
  Widget build(BuildContext context) {
    built = true;
32
    return widget.child;
33 34 35 36
  }
}

class Wrapper extends StatelessWidget {
37
  const Wrapper({
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    Key key,
    this.child,
  }) : super(key: key);

  final Widget child;

  @override
  Widget build(BuildContext context) {
    return child;
  }
}

void main() {
  testWidgets('Calling setState on a widget that moves into a LayoutBuilder in the same frame', (WidgetTester tester) async {
    StatefulWrapperState statefulWrapper;
53 54 55 56
    final Widget inner = Wrapper(
      child: StatefulWrapper(
        key: GlobalKey(),
        child: Container(),
57 58
      ),
    );
59 60
    await tester.pumpWidget(FlipWidget(
      left: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        return inner;
      }),
      right: inner,
    ));
    statefulWrapper = tester.state(find.byType(StatefulWrapper));
    expect(statefulWrapper.built, true);
    statefulWrapper.built = false;

    statefulWrapper.trigger();
    flipStatefulWidget(tester);
    await tester.pump();
    expect(statefulWrapper.built, true);
    statefulWrapper.built = false;

    statefulWrapper.trigger();
    flipStatefulWidget(tester);
    await tester.pump();
    expect(statefulWrapper.built, true);
    statefulWrapper.built = false;

    statefulWrapper.trigger();
    flipStatefulWidget(tester);
    await tester.pump();
    expect(statefulWrapper.built, true);
    statefulWrapper.built = false;
  });
}