sliver_persistent_header_test.dart 3.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// Copyright 2014 The Flutter 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/material.dart';
import 'package:flutter/src/rendering/sliver_persistent_header.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets(
      '_SliverScrollingPersistentHeader should update stretchConfiguration',
      (WidgetTester tester) async {
    for (final double stretchTriggerOffset in <double>[10.0, 20.0]) {
      await tester.pumpWidget(MaterialApp(
        home: CustomScrollView(
          slivers: <Widget>[
            SliverPersistentHeader(
              delegate: TestDelegate(
                stretchConfiguration: OverScrollHeaderStretchConfiguration(
                  stretchTriggerOffset: stretchTriggerOffset,
                ),
              ),
            )
          ],
        ),
      ));
    }

    expect(
        tester.allWidgets.where((Widget w) =>
            w.runtimeType.toString() == '_SliverScrollingPersistentHeader'),
        isNotEmpty);

    final RenderSliverScrollingPersistentHeader render = tester.allRenderObjects
        .whereType<RenderSliverScrollingPersistentHeader>()
        .first;
    expect(render.stretchConfiguration?.stretchTriggerOffset, 20);
  });
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 88 89 90 91 92 93 94 95 96 97 98 99 100

  testWidgets(
      '_SliverPinnedPersistentHeader should update stretchConfiguration',
      (WidgetTester tester) async {
    for (final double stretchTriggerOffset in <double>[10.0, 20.0]) {
      await tester.pumpWidget(MaterialApp(
        home: CustomScrollView(
          slivers: <Widget>[
            SliverPersistentHeader(
              pinned: true,
              delegate: TestDelegate(
                stretchConfiguration: OverScrollHeaderStretchConfiguration(
                  stretchTriggerOffset: stretchTriggerOffset,
                ),
              ),
            )
          ],
        ),
      ));
    }

    expect(
        tester.allWidgets.where((Widget w) =>
            w.runtimeType.toString() == '_SliverPinnedPersistentHeader'),
        isNotEmpty);

    final RenderSliverPinnedPersistentHeader render = tester.allRenderObjects
        .whereType<RenderSliverPinnedPersistentHeader>()
        .first;
    expect(render.stretchConfiguration?.stretchTriggerOffset, 20);
  });

  testWidgets(
      '_SliverPinnedPersistentHeader should update showOnScreenConfiguration',
      (WidgetTester tester) async {
    for (final double maxShowOnScreenExtent in <double>[1000, 2000]) {
      await tester.pumpWidget(MaterialApp(
        home: CustomScrollView(
          slivers: <Widget>[
            SliverPersistentHeader(
              pinned: true,
              delegate: TestDelegate(
                showOnScreenConfiguration:
                    PersistentHeaderShowOnScreenConfiguration(
                        maxShowOnScreenExtent: maxShowOnScreenExtent),
              ),
            )
          ],
        ),
      ));
    }

    expect(
        tester.allWidgets.where((Widget w) =>
            w.runtimeType.toString() == '_SliverPinnedPersistentHeader'),
        isNotEmpty);

    final RenderSliverPinnedPersistentHeader render = tester.allRenderObjects
        .whereType<RenderSliverPinnedPersistentHeader>()
        .first;
    expect(render.showOnScreenConfiguration?.maxShowOnScreenExtent, 2000);
  });
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
}

class TestDelegate extends SliverPersistentHeaderDelegate {
  TestDelegate({this.stretchConfiguration, this.showOnScreenConfiguration});

  @override
  double get maxExtent => 200.0;

  @override
  double get minExtent => 200.0;

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(height: maxExtent);
  }

  @override
  bool shouldRebuild(TestDelegate oldDelegate) => false;

  @override
  final OverScrollHeaderStretchConfiguration? stretchConfiguration;
  @override
  final PersistentHeaderShowOnScreenConfiguration? showOnScreenConfiguration;
}