reparent_state_with_layout_builder_test.dart 4.51 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8
import 'dart:ui' as ui show window;

9
import 'package:flutter/material.dart';
10
import 'package:flutter_test/flutter_test.dart';
11 12 13 14

// This is a regression test for https://github.com/flutter/flutter/issues/5840.

class Bar extends StatefulWidget {
15
  const Bar({ Key key }) : super(key: key);
16
  @override
17
  BarState createState() => BarState();
18 19 20
}

class BarState extends State<Bar> {
21
  final GlobalKey _fooKey = GlobalKey();
22 23 24 25 26 27 28 29 30 31 32 33

  bool _mode = false;

  void trigger() {
    setState(() {
      _mode = !_mode;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_mode) {
34 35
      return SizedBox(
        child: LayoutBuilder(
36
          builder: (BuildContext context, BoxConstraints constraints) {
37
            return StatefulCreationCounter(key: _fooKey);
38
          },
39 40 41
        ),
      );
    } else {
42
      return LayoutBuilder(
43
        builder: (BuildContext context, BoxConstraints constraints) {
44
          return StatefulCreationCounter(key: _fooKey);
45
        },
46 47 48 49 50 51
      );
    }
  }
}

class StatefulCreationCounter extends StatefulWidget {
52
  const StatefulCreationCounter({ Key key }) : super(key: key);
53 54

  @override
55
  StatefulCreationCounterState createState() => StatefulCreationCounterState();
56 57 58 59 60 61 62 63 64 65 66 67
}

class StatefulCreationCounterState extends State<StatefulCreationCounter> {
  static int creationCount = 0;

  @override
  void initState() {
    super.initState();
    creationCount += 1;
  }

  @override
68
  Widget build(BuildContext context) => Container();
69 70 71
}

void main() {
72
  testWidgets('reparent state with layout builder', (WidgetTester tester) async {
73
    expect(StatefulCreationCounterState.creationCount, 0);
74
    await tester.pumpWidget(const Bar());
75
    expect(StatefulCreationCounterState.creationCount, 1);
76
    final BarState s = tester.state<BarState>(find.byType(Bar));
77 78 79 80
    s.trigger();
    await tester.pump();
    expect(StatefulCreationCounterState.creationCount, 1);
  });
81

82
  testWidgets('Clean then reparent with dependencies', (WidgetTester tester) async {
83
    int layoutBuilderBuildCount = 0;
84 85

    StateSetter keyedSetState;
86 87
    StateSetter layoutBuilderSetState;
    StateSetter childSetState;
88

89 90
    final GlobalKey key = GlobalKey();
    final Widget keyedWidget = StatefulBuilder(
91 92 93 94
      key: key,
      builder: (BuildContext context, StateSetter setState) {
        keyedSetState = setState;
        MediaQuery.of(context);
95
        return Container();
96 97 98 99
      },
    );

    Widget layoutBuilderChild = keyedWidget;
100
    Widget deepChild = Container();
101

102 103 104
    await tester.pumpWidget(MediaQuery(
      data: MediaQueryData.fromWindow(ui.window),
      child: Column(
105
        children: <Widget>[
106
          StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
107
            layoutBuilderSetState = setState;
108
            return LayoutBuilder(
109
              builder: (BuildContext context, BoxConstraints constraints) {
110 111
                layoutBuilderBuildCount += 1;
                return layoutBuilderChild; // initially keyedWidget above, but then a new Container
112 113 114
              },
            );
          }),
115 116 117 118 119 120
          Container(
            child: Container(
              child: Container(
                child: Container(
                  child: Container(
                    child: Container(
121 122 123 124 125 126
                      child: StatefulBuilder(
                        builder: (BuildContext context, StateSetter setState) {
                          childSetState = setState;
                          return deepChild; // initially a Container, but then the keyedWidget above
                        },
                      ),
127 128 129 130 131 132 133 134 135 136 137
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    ));
    expect(layoutBuilderBuildCount, 1);

138
    keyedSetState(() { /* Change nothing but add the element to the dirty list. */ });
139 140

    childSetState(() {
141 142
      // The deep child builds in the initial build phase. It takes the child
      // from the LayoutBuilder before the LayoutBuilder has a chance to build.
143 144 145 146
      deepChild = keyedWidget;
    });

    layoutBuilderSetState(() {
147 148
      // The layout builder will build in a separate build scope. This delays
      // the removal of the keyed child until this build scope.
149
      layoutBuilderChild = Container();
150 151 152 153 154 155
    });

    // The essential part of this test is that this call to pump doesn't throw.
    await tester.pump();
    expect(layoutBuilderBuildCount, 2);
  });
156
}