sliver_fill_remaining_test.dart 16.2 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4 5
// Copyright 2017 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';
6
import 'package:flutter/foundation.dart';
7
import 'package:flutter/material.dart';
Adam Barth's avatar
Adam Barth committed
8 9 10
import 'package:flutter/widgets.dart';

void main() {
11
  testWidgets('SliverFillRemaining - no siblings', (WidgetTester tester) async {
12
    final ScrollController controller = ScrollController();
Adam Barth's avatar
Adam Barth committed
13
    await tester.pumpWidget(
14
      Directionality(
15
        textDirection: TextDirection.ltr,
16
        child: CustomScrollView(
17 18
          controller: controller,
          slivers: <Widget>[
19
            SliverFillRemaining(child: Container()),
20 21
          ],
        ),
Adam Barth's avatar
Adam Barth committed
22 23
      ),
    );
24
    expect(tester.renderObject<RenderBox>(find.byType(Container)).size.height, equals(600.0));
Adam Barth's avatar
Adam Barth committed
25

26 27 28
    controller.jumpTo(50.0);
    await tester.pump();
    expect(tester.renderObject<RenderBox>(find.byType(Container)).size.height, equals(600.0));
Adam Barth's avatar
Adam Barth committed
29

30 31 32
    controller.jumpTo(-100.0);
    await tester.pump();
    expect(tester.renderObject<RenderBox>(find.byType(Container)).size.height, equals(600.0));
Adam Barth's avatar
Adam Barth committed
33

34
    controller.jumpTo(0.0);
Adam Barth's avatar
Adam Barth committed
35
    await tester.pump();
36 37
    expect(tester.renderObject<RenderBox>(find.byType(Container)).size.height, equals(600.0));
  });
Adam Barth's avatar
Adam Barth committed
38

39
  testWidgets('SliverFillRemaining - one sibling', (WidgetTester tester) async {
40
    final ScrollController controller = ScrollController();
41
    await tester.pumpWidget(
42
      Directionality(
43
        textDirection: TextDirection.ltr,
44
        child: CustomScrollView(
45 46
          controller: controller,
          slivers: <Widget>[
47
            const SliverToBoxAdapter(child: SizedBox(height: 100.0)),
48
            SliverFillRemaining(child: Container()),
49 50
          ],
        ),
51 52 53
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byType(Container)).size.height, equals(500.0));
Adam Barth's avatar
Adam Barth committed
54

55
    controller.jumpTo(50.0);
Adam Barth's avatar
Adam Barth committed
56
    await tester.pump();
57
    expect(tester.renderObject<RenderBox>(find.byType(Container)).size.height, equals(550.0));
Adam Barth's avatar
Adam Barth committed
58

59 60 61 62 63 64 65
    controller.jumpTo(-100.0);
    await tester.pump();
    expect(tester.renderObject<RenderBox>(find.byType(Container)).size.height, equals(400.0)); // (!)

    controller.jumpTo(0.0);
    await tester.pump();
    expect(tester.renderObject<RenderBox>(find.byType(Container)).size.height, equals(500.0));
Adam Barth's avatar
Adam Barth committed
66
  });
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  group('SliverFillRemaining - hasScrollBody', () {
    final Widget sliverBox = SliverToBoxAdapter(
      child: Container(
        color: Colors.amber,
        height: 150.0,
      ),
    );
    Widget boilerplate(List<Widget> slivers, {ScrollController controller}) {
      return MaterialApp(
        home: Scaffold(
          body: CustomScrollView(
            slivers: slivers,
            controller: controller,
          ),
        ),
      );
    }

    testWidgets('does not extend past viewport when false', (WidgetTester tester) async {
      final ScrollController controller = ScrollController();
      final List<Widget> slivers = <Widget>[
        sliverBox,
        SliverFillRemaining(
          child: Container(color: Colors.white),
          hasScrollBody: false,
        ),
      ];
      await tester.pumpWidget(boilerplate(slivers, controller: controller));
      expect(controller.offset, 0.0);
      expect(find.byType(Container), findsNWidgets(2));
      controller.jumpTo(150.0);
      await tester.pumpAndSettle();
      expect(controller.offset, 0.0);
      expect(find.byType(Container), findsNWidgets(2));
    });

    testWidgets('scrolls beyond viewport by default', (WidgetTester tester) async {
      final ScrollController controller = ScrollController();
      final List<Widget> slivers = <Widget>[
              sliverBox,
              SliverFillRemaining(
                child: Container(color: Colors.white),
              ),
            ];
      await tester.pumpWidget(boilerplate(slivers, controller: controller));
      expect(controller.offset, 0.0);
      expect(find.byType(Container), findsNWidgets(2));
      controller.jumpTo(150.0);
      await tester.pumpAndSettle();
      expect(controller.offset, 150.0);
      expect(find.byType(Container), findsOneWidget);
    });

    // SliverFillRemaining considers child size when hasScrollBody: false
    testWidgets('child without size is sized by extent when false', (WidgetTester tester) async {
      final List<Widget> slivers = <Widget>[
        sliverBox,
        SliverFillRemaining(
          hasScrollBody: false,
          child: Container(color: Colors.blue),
        ),
      ];
      await tester.pumpWidget(boilerplate(slivers));
      final RenderBox box = tester.renderObject<RenderBox>(find.byType(Container).last);
      expect(box.size.height, equals(450));
    });

    testWidgets('child with size is sized by extent when false', (WidgetTester tester) async {
      final GlobalKey key = GlobalKey();
      final List<Widget> slivers = <Widget>[
        sliverBox,
        SliverFillRemaining(
          hasScrollBody: false,
          child: Container(
            key: key,
            color: Colors.blue,
            child: Align(
              alignment: Alignment.bottomCenter,
              child: RaisedButton(
                child: const Text('bottomCenter button'),
                onPressed: () {},
149 150
              ),
            ),
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
          ),
        ),
      ];
      await tester.pumpWidget(boilerplate(slivers));
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, equals(450));

      // Also check that the button alignment is true to expectations
      final Finder button = find.byType(RaisedButton);
      expect(tester.getBottomLeft(button).dy, equals(600.0));
      expect(tester.getCenter(button).dx, equals(400.0));
    });

    testWidgets('extent is overridden by child with larger size when false', (WidgetTester tester) async {
      final List<Widget> slivers = <Widget>[
        sliverBox,
        SliverFillRemaining(
          hasScrollBody: false,
          child: Container(
            color: Colors.blue,
            height: 600,
          ),
        ),
      ];
      await tester.pumpWidget(boilerplate(slivers));
      final RenderBox box = tester.renderObject<RenderBox>(find.byType(Container).last);
      expect(box.size.height, equals(600));
    });

    testWidgets('extent is overridden by child size if precedingScrollExtent > viewportMainAxisExtent when false', (WidgetTester tester) async {
      final GlobalKey key = GlobalKey();
      final List<Widget> slivers = <Widget>[
        SliverFixedExtentList(
          itemExtent: 150,
          delegate: SliverChildBuilderDelegate(
            (BuildContext context, int index) => Container(color: Colors.amber),
            childCount: 5,
          ),
        ),
        SliverFillRemaining(
          hasScrollBody: false,
          child: Container(
            key: key,
            color: Colors.blue[300],
            child: Align(
              alignment: Alignment.center,
              child: Padding(
                padding: const EdgeInsets.all(50.0),
                child: RaisedButton(
                  child: const Text('center button'),
                  onPressed: () {},
                ),
              ),
203
            ),
204
          ),
205
        ),
206 207 208 209 210
      ];
      await tester.pumpWidget(boilerplate(slivers));
      await tester.drag(find.byType(Scrollable), const Offset(0.0, -750.0));
      await tester.pump();
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, equals(148.0));
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
      // Also check that the button alignment is true to expectations
      final Finder button = find.byType(RaisedButton);
      expect(tester.getBottomLeft(button).dy, equals(550.0));
      expect(tester.getCenter(button).dx, equals(400.0));
    });

    // iOS/Similar scroll physics when hasScrollBody: false & fillOverscroll: true behavior
    testWidgets('child without size is sized by extent and overscroll', (WidgetTester tester) async {
      debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
      final List<Widget> slivers = <Widget>[
        sliverBox,
        SliverFillRemaining(
          hasScrollBody: false,
          fillOverscroll: true,
          child: Container(color: Colors.blue),
        ),
      ];
      await tester.pumpWidget(boilerplate(slivers));
      final RenderBox box1 = tester.renderObject<RenderBox>(find.byType(Container).last);
      expect(box1.size.height, equals(450));

      await tester.drag(find.byType(Scrollable), const Offset(0.0, -50.0));
      await tester.pump();
      final RenderBox box2 = tester.renderObject<RenderBox>(find.byType(Container).last);
      expect(box2.size.height, greaterThan(450));
      debugDefaultTargetPlatformOverride = null;
    });

    testWidgets('child with size is overridden and sized by extent and overscroll', (WidgetTester tester) async {
      debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
      final GlobalKey key = GlobalKey();
      final List<Widget> slivers = <Widget>[
        sliverBox,
        SliverFillRemaining(
          hasScrollBody: false,
          fillOverscroll: true,
          child: Container(
            key: key,
            color: Colors.blue,
            child: Align(
              alignment: Alignment.bottomCenter,
              child: RaisedButton(
                child: const Text('bottomCenter button'),
                onPressed: () {},
256 257
              ),
            ),
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
          ),
        ),
      ];
      await tester.pumpWidget(boilerplate(slivers));
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, equals(450));

      await tester.drag(find.byType(Scrollable), const Offset(0.0, -50.0));
      await tester.pump();
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, greaterThan(450));

      // Also check that the button alignment is true to expectations, even with
      // child stretching to fill overscroll
      final Finder button = find.byType(RaisedButton);
      expect(tester.getBottomLeft(button).dy, equals(600.0));
      expect(tester.getCenter(button).dx, equals(400.0));
      debugDefaultTargetPlatformOverride = null;
    });

    testWidgets('extent is overridden by child size and overscroll if precedingScrollExtent > viewportMainAxisExtent', (WidgetTester tester) async {
      debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
      final GlobalKey key = GlobalKey();
      final ScrollController controller = ScrollController();
      final List<Widget> slivers = <Widget>[
        SliverFixedExtentList(
          itemExtent: 150,
          delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) => Container(color: Colors.amber),
            childCount: 5,
          ),
        ),
        SliverFillRemaining(
          hasScrollBody: false,
          fillOverscroll: true,
          child: Container(
            key: key,
            color: Colors.blue[300],
            child: Align(
              alignment: Alignment.center,
              child: Padding(
                padding: const EdgeInsets.all(50.0),
                child: RaisedButton(
                  child: const Text('center button'),
                  onPressed: () {},
                ),
              ),
303
            ),
304
          ),
305
        ),
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
      ];
      await tester.pumpWidget(boilerplate(slivers, controller: controller));
      // Scroll to the end
      controller.jumpTo(controller.position.maxScrollExtent);
      await tester.pump();
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, equals(148.0));
      // Check that the button alignment is true to expectations
      final Finder button = find.byType(RaisedButton);
      expect(tester.getBottomLeft(button).dy, equals(550.0));
      expect(tester.getCenter(button).dx, equals(400.0));
      debugDefaultTargetPlatformOverride = null;

      // Drag for overscroll
      await tester.drag(find.byType(Scrollable), const Offset(0.0, -50.0));
      await tester.pump();
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, greaterThan(148.0));

      // Check that the button alignment is still centered in stretched child
      expect(tester.getBottomLeft(button).dy, lessThan(550.0));
      expect(tester.getCenter(button).dx, equals(400.0));
      debugDefaultTargetPlatformOverride = null;
    });

    // Android/Other scroll physics when hasScrollBody: false, ignores fillOverscroll: true
    testWidgets('child without size is sized by extent, fillOverscroll is ignored', (WidgetTester tester) async {
      final List<Widget> slivers = <Widget>[
        sliverBox,
        SliverFillRemaining(
          hasScrollBody: false,
          fillOverscroll: true,
          child: Container(color: Colors.blue),
        ),
      ];
      await tester.pumpWidget(boilerplate(slivers));
      final RenderBox box1 = tester.renderObject<RenderBox>(find.byType(Container).last);
      expect(box1.size.height, equals(450));

      await tester.drag(find.byType(Scrollable), const Offset(0.0, -50.0));
      await tester.pump();
      final RenderBox box2 = tester.renderObject<RenderBox>(find.byType(Container).last);
      expect(box2.size.height, equals(450));
    });

    testWidgets('child with size is overridden and sized by extent, fillOverscroll is ignored', (WidgetTester tester) async {
      final GlobalKey key = GlobalKey();
      final List<Widget> slivers = <Widget>[
        sliverBox,
        SliverFillRemaining(
          hasScrollBody: false,
          fillOverscroll: true,
          child: Container(
            key: key,
            color: Colors.blue,
            child: Align(
              alignment: Alignment.bottomCenter,
              child: RaisedButton(
                child: const Text('bottomCenter button'),
                onPressed: () {},
              ),
            ),
          ),
        ),
      ];
      await tester.pumpWidget(boilerplate(slivers));
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, equals(450));

      await tester.drag(find.byType(Scrollable), const Offset(0.0, -50.0));
      await tester.pump();
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, equals(450));

      // Also check that the button alignment is true to expectations
      final Finder button = find.byType(RaisedButton);
      expect(tester.getBottomLeft(button).dy, equals(600.0));
      expect(tester.getCenter(button).dx, equals(400.0));
    });

    testWidgets('extent is overridden by child size if precedingScrollExtent > viewportMainAxisExtent, fillOverscroll is ignored', (WidgetTester tester) async {
      final GlobalKey key = GlobalKey();
      final ScrollController controller = ScrollController();
      final List<Widget> slivers = <Widget>[
        SliverFixedExtentList(
          itemExtent: 150,
          delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) => Container(color: Colors.amber),
            childCount: 5,
          ),
        ),
        SliverFillRemaining(
          hasScrollBody: false,
          fillOverscroll: true,
          child: Container(
            key: key,
            color: Colors.blue[300],
            child: Align(
              alignment: Alignment.center,
              child: Padding(
                padding: const EdgeInsets.all(50.0),
                child: RaisedButton(
                  child: const Text('center button'),
                  onPressed: () {},
                ),
              ),
            ),
          ),
        ),
      ];
      await tester.pumpWidget(boilerplate(slivers, controller: controller));
      // Scroll to the end
      controller.jumpTo(controller.position.maxScrollExtent);
      await tester.pump();
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, equals(148.0));
      // Check that the button alignment is true to expectations
      final Finder button = find.byType(RaisedButton);
      expect(tester.getBottomLeft(button).dy, equals(550.0));
      expect(tester.getCenter(button).dx, equals(400.0));
      debugDefaultTargetPlatformOverride = null;

      await tester.drag(find.byType(Scrollable), const Offset(0.0, -50.0));
      await tester.pump();
      expect(tester.renderObject<RenderBox>(find.byKey(key)).size.height, equals(148.0));

      // Check that the button alignment is still centered in stretched child
      expect(tester.getBottomLeft(button).dy, equals(550.0));
      expect(tester.getCenter(button).dx, equals(400.0));
    });
431
  });
Adam Barth's avatar
Adam Barth committed
432
}