list_view_relayout_test.dart 6.37 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 12 13 14
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
15 16
          shrinkWrap: true,
          children: <Widget>[
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
            new ListView(
              shrinkWrap: true,
              children: <Widget>[
                const Text('1'),
                const Text('2'),
                const Text('3'),
              ],
            ),
            new ListView(
              shrinkWrap: true,
              children: <Widget>[
                const Text('4'),
                const Text('5'),
                const Text('6'),
              ],
            ),
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 43 44 45 46 47 48 49 50 51
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          children: <Widget>[
            const SizedBox(height: 100.0, child: const Text('100')),
          ],
        ),
      ),
    );
52

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

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

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

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

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

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

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

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

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

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

137 138 139 140 141 142 143 144
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          children: <Widget>[]
        ),
      ),
    );
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 152 153 154 155 156 157 158 159 160 161
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          children: <Widget>[
            const SizedBox(height: 300.0, child: const Text('300')),
            const SizedBox(height: 400.0, child: const Text('400')),
          ],
        ),
      ),
    );
162 163 164 165

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

166 167 168 169 170 171 172 173 174 175 176 177
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          children: <Widget>[
            const SizedBox(height: 300.0, child: const Text('300')),
            const SizedBox(height: 400.0, child: const Text('400')),
            const SizedBox(height: 100.0, child: const Text('100')),
          ],
        ),
      ),
    );
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 191 192 193 194 195 196 197 198 199
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          children: <Widget>[
            const SizedBox(height: 100.0, child: const Text('100')),
          ],
        ),
      ),
    );
200

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

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

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

}