list_view_relayout_test.dart 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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:flutter/rendering.dart';

void main() {
  testWidgets('Nested ListView with shrinkWrap', (WidgetTester tester) async {
11
    await tester.pumpWidget(
12
      Directionality(
13
        textDirection: TextDirection.ltr,
14
        child: ListView(
15 16
          shrinkWrap: true,
          children: <Widget>[
17
            ListView(
18
              shrinkWrap: true,
19
              children: const <Widget>[
20 21 22
                Text('1'),
                Text('2'),
                Text('3'),
23 24
              ],
            ),
25
            ListView(
26
              shrinkWrap: true,
27
              children: const <Widget>[
28 29 30
                Text('4'),
                Text('5'),
                Text('6'),
31 32
              ],
            ),
33 34
          ],
        ),
35 36
      ),
    );
37 38 39 40 41
  });

  testWidgets('Underflowing ListView should relayout for additional children', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/5950

42
    await tester.pumpWidget(
43
      Directionality(
44
        textDirection: TextDirection.ltr,
45
        child: ListView(
46
          children: const <Widget>[
47
            SizedBox(height: 100.0, child: Text('100')),
48 49 50 51
          ],
        ),
      ),
    );
52

53
    await tester.pumpWidget(
54
      Directionality(
55
        textDirection: TextDirection.ltr,
56
        child: ListView(
57
          children: const <Widget>[
58 59
            SizedBox(height: 100.0, child: Text('100')),
            SizedBox(height: 200.0, child: Text('200')),
60 61 62 63
          ],
        ),
      ),
    );
64 65 66 67 68

    expect(find.text('200'), findsOneWidget);
  });

  testWidgets('Underflowing ListView contentExtent should track additional children', (WidgetTester tester) async {
69
    await tester.pumpWidget(
70
      Directionality(
71
        textDirection: TextDirection.ltr,
72
        child: ListView(
73
          children: const <Widget>[
74
            SizedBox(height: 100.0, child: Text('100')),
75 76 77 78
          ],
        ),
      ),
    );
79

80
    final RenderSliverList list = tester.renderObject(find.byType(SliverList));
81 82
    expect(list.geometry.scrollExtent, equals(100.0));

83
    await tester.pumpWidget(
84
      Directionality(
85
        textDirection: TextDirection.ltr,
86
        child: ListView(
87
          children: const <Widget>[
88 89
            SizedBox(height: 100.0, child: Text('100')),
            SizedBox(height: 200.0, child: Text('200')),
90 91 92 93
          ],
        ),
      ),
    );
94 95
    expect(list.geometry.scrollExtent, equals(300.0));

96
    await tester.pumpWidget(
97
      Directionality(
98
        textDirection: TextDirection.ltr,
99
        child: ListView(
100
          children: const <Widget>[],
101 102 103
        ),
      ),
    );
104 105 106 107
    expect(list.geometry.scrollExtent, equals(0.0));
  });

  testWidgets('Overflowing ListView should relayout for missing children', (WidgetTester tester) async {
108
    await tester.pumpWidget(
109
      Directionality(
110
        textDirection: TextDirection.ltr,
111
        child: ListView(
112
          children: const <Widget>[
113 114
            SizedBox(height: 300.0, child: Text('300')),
            SizedBox(height: 400.0, child: Text('400')),
115 116 117 118
          ],
        ),
      ),
    );
119 120 121 122

    expect(find.text('300'), findsOneWidget);
    expect(find.text('400'), findsOneWidget);

123
    await tester.pumpWidget(
124
      Directionality(
125
        textDirection: TextDirection.ltr,
126
        child: ListView(
127
          children: const <Widget>[
128
            SizedBox(height: 300.0, child: Text('300')),
129 130 131 132
          ],
        ),
      ),
    );
133 134 135 136

    expect(find.text('300'), findsOneWidget);
    expect(find.text('400'), findsNothing);

137
    await tester.pumpWidget(
138
      Directionality(
139
        textDirection: TextDirection.ltr,
140
        child: ListView(
141
          children: const <Widget>[],
142 143 144
        ),
      ),
    );
145 146 147 148 149 150

    expect(find.text('300'), findsNothing);
    expect(find.text('400'), findsNothing);
  });

  testWidgets('Overflowing ListView should not relayout for additional children', (WidgetTester tester) async {
151
    await tester.pumpWidget(
152
      Directionality(
153
        textDirection: TextDirection.ltr,
154
        child: ListView(
155
          children: const <Widget>[
156 157
            SizedBox(height: 300.0, child: Text('300')),
            SizedBox(height: 400.0, child: Text('400')),
158 159 160 161
          ],
        ),
      ),
    );
162 163 164 165

    expect(find.text('300'), findsOneWidget);
    expect(find.text('400'), findsOneWidget);

166
    await tester.pumpWidget(
167
      Directionality(
168
        textDirection: TextDirection.ltr,
169
        child: ListView(
170
          children: const <Widget>[
171 172 173
            SizedBox(height: 300.0, child: Text('300')),
            SizedBox(height: 400.0, child: Text('400')),
            SizedBox(height: 100.0, child: Text('100')),
174 175 176 177
          ],
        ),
      ),
    );
178 179 180 181 182 183 184 185 186 187 188 189

    expect(find.text('300'), findsOneWidget);
    expect(find.text('400'), findsOneWidget);
    expect(find.text('100'), findsNothing);
  });

  testWidgets('Overflowing ListView should become scrollable', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/5920
    // When a ListView's viewport hasn't overflowed, scrolling is disabled.
    // When children are added that cause it to overflow, scrolling should
    // be enabled.

190
    await tester.pumpWidget(
191
      Directionality(
192
        textDirection: TextDirection.ltr,
193
        child: ListView(
194
          children: const <Widget>[
195
            SizedBox(height: 100.0, child: Text('100')),
196 197 198 199
          ],
        ),
      ),
    );
200

201
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
202 203
    expect(scrollable.position.maxScrollExtent, 0.0);

204
    await tester.pumpWidget(
205
      Directionality(
206
        textDirection: TextDirection.ltr,
207
        child: ListView(
208
          children: const <Widget>[
209 210 211
            SizedBox(height: 100.0, child: Text('100')),
            SizedBox(height: 200.0, child: Text('200')),
            SizedBox(height: 400.0, child: Text('400')),
212 213 214 215
          ],
        ),
      ),
    );
216 217 218 219 220

    expect(scrollable.position.maxScrollExtent, 100.0);
  });

}