flexible_space_bar_collapse_mode_test.dart 8.05 KB
Newer Older
1 2 3 4 5 6 7
// 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/material.dart';
import 'package:flutter_test/flutter_test.dart';

8
final Key blockKey = UniqueKey();
9
const double expandedAppbarHeight = 250.0;
10
final Key appbarContainerKey = UniqueKey();
11 12 13 14

void main() {
  testWidgets('FlexibleSpaceBar collapse mode none on Android', (WidgetTester tester) async {
    await tester.pumpWidget(
15 16 17 18
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          body: CustomScrollView(
19 20
            key: blockKey,
            slivers: <Widget>[
21
              SliverAppBar(
22 23
                expandedHeight: expandedAppbarHeight,
                pinned: true,
24 25
                flexibleSpace: FlexibleSpaceBar(
                  background: Container(
26 27 28 29 30
                    key: appbarContainerKey,
                  ),
                  collapseMode: CollapseMode.none,
                ),
              ),
31 32
              SliverToBoxAdapter(
                child: Container(
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
                  height: 10000.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    final Finder appbarContainer = find.byKey(appbarContainerKey);
    final Offset topBeforeScroll = tester.getTopLeft(appbarContainer);
    await slowDrag(tester, blockKey, const Offset(0.0, -100.0));
    final Offset topAfterScroll = tester.getTopLeft(appbarContainer);

    expect(topBeforeScroll.dy, equals(0.0));
    expect(topAfterScroll.dy, equals(0.0));
  });

  testWidgets('FlexibleSpaceBar collapse mode none on IOS', (WidgetTester tester) async {
    await tester.pumpWidget(
53 54 55 56
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.iOS),
        home: Scaffold(
          body: CustomScrollView(
57 58
            key: blockKey,
            slivers: <Widget>[
59
              SliverAppBar(
60 61
                expandedHeight: expandedAppbarHeight,
                pinned: true,
62 63
                flexibleSpace: FlexibleSpaceBar(
                  background: Container(
64 65 66 67 68
                    key: appbarContainerKey,
                  ),
                  collapseMode: CollapseMode.none,
                ),
              ),
69 70
              SliverToBoxAdapter(
                child: Container(
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
                  height: 10000.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    final Finder appbarContainer = find.byKey(appbarContainerKey);
    final Offset topBeforeScroll = tester.getTopLeft(appbarContainer);
    await slowDrag(tester, blockKey, const Offset(0.0, -100.0));
    final Offset topAfterScroll = tester.getTopLeft(appbarContainer);

    expect(topBeforeScroll.dy, equals(0.0));
    expect(topAfterScroll.dy, equals(0.0));
  });

  testWidgets('FlexibleSpaceBar collapse mode pin on Android', (WidgetTester tester) async {
    await tester.pumpWidget(
91 92 93 94
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          body: CustomScrollView(
95 96
            key: blockKey,
            slivers: <Widget>[
97
              SliverAppBar(
98 99
                expandedHeight: expandedAppbarHeight,
                pinned: true,
100 101
                flexibleSpace: FlexibleSpaceBar(
                  background: Container(
102 103 104 105 106
                    key: appbarContainerKey,
                  ),
                  collapseMode: CollapseMode.pin,
                ),
              ),
107 108
              SliverToBoxAdapter(
                child: Container(
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                  height: 10000.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    final Finder appbarContainer = find.byKey(appbarContainerKey);
    final Offset topBeforeScroll = tester.getTopLeft(appbarContainer);
    await slowDrag(tester, blockKey, const Offset(0.0, -100.0));
    final Offset topAfterScroll = tester.getTopLeft(appbarContainer);

    expect(topBeforeScroll.dy, equals(0.0));
    expect(topAfterScroll.dy, equals(-100.0));
  });

  testWidgets('FlexibleSpaceBar collapse mode pin on IOS', (WidgetTester tester) async {
    await tester.pumpWidget(
129 130 131 132
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.iOS),
        home: Scaffold(
          body: CustomScrollView(
133 134
            key: blockKey,
            slivers: <Widget>[
135
              SliverAppBar(
136 137
                expandedHeight: expandedAppbarHeight,
                pinned: true,
138 139
                flexibleSpace: FlexibleSpaceBar(
                  background: Container(
140 141 142 143 144
                    key: appbarContainerKey,
                  ),
                  collapseMode: CollapseMode.pin,
                ),
              ),
145 146
              SliverToBoxAdapter(
                child: Container(
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                  height: 10000.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    final Finder appbarContainer = find.byKey(appbarContainerKey);
    final Offset topBeforeScroll = tester.getTopLeft(appbarContainer);
    await slowDrag(tester, blockKey, const Offset(0.0, -100.0));
    final Offset topAfterScroll = tester.getTopLeft(appbarContainer);

    expect(topBeforeScroll.dy, equals(0.0));
    expect(topAfterScroll.dy, equals(-100.0));
  });



  testWidgets('FlexibleSpaceBar collapse mode parallax on Android', (WidgetTester tester) async {
    await tester.pumpWidget(
169 170 171 172
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          body: CustomScrollView(
173 174
            key: blockKey,
            slivers: <Widget>[
175
              SliverAppBar(
176 177
                expandedHeight: expandedAppbarHeight,
                pinned: true,
178 179
                flexibleSpace: FlexibleSpaceBar(
                  background: Container(
180 181 182 183 184
                    key: appbarContainerKey,
                  ),
                  collapseMode: CollapseMode.parallax,
                ),
              ),
185 186
              SliverToBoxAdapter(
                child: Container(
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
                  height: 10000.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    final Finder appbarContainer = find.byKey(appbarContainerKey);
    final Offset topBeforeScroll = tester.getTopLeft(appbarContainer);
    await slowDrag(tester, blockKey, const Offset(0.0, -100.0));
    final Offset topAfterScroll = tester.getTopLeft(appbarContainer);

    expect(topBeforeScroll.dy, equals(0.0));
    expect(topAfterScroll.dy, lessThan(10.0));
    expect(topAfterScroll.dy, greaterThan(-50.0));
  });

  testWidgets('FlexibleSpaceBar collapse mode parallax on IOS', (WidgetTester tester) async {
    await tester.pumpWidget(
208 209 210 211
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.iOS),
        home: Scaffold(
          body: CustomScrollView(
212 213
            key: blockKey,
            slivers: <Widget>[
214
              SliverAppBar(
215 216
                expandedHeight: expandedAppbarHeight,
                pinned: true,
217 218
                flexibleSpace: FlexibleSpaceBar(
                  background: Container(
219 220 221 222 223
                    key: appbarContainerKey,
                  ),
                  collapseMode: CollapseMode.parallax,
                ),
              ),
224 225
              SliverToBoxAdapter(
                child: Container(
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
                  height: 10000.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    final Finder appbarContainer = find.byKey(appbarContainerKey);
    final Offset topBeforeScroll = tester.getTopLeft(appbarContainer);
    await slowDrag(tester, blockKey, const Offset(0.0, -100.0));
    final Offset topAfterScroll = tester.getTopLeft(appbarContainer);

    expect(topBeforeScroll.dy, equals(0.0));
    expect(topAfterScroll.dy, lessThan(10.0));
    expect(topAfterScroll.dy, greaterThan(-50.0));
  });
}

Future<Null> slowDrag(WidgetTester tester, Key widget, Offset offset) async {
  final Offset target = tester.getCenter(find.byKey(widget));
  final TestGesture gesture = await tester.startGesture(target);
  await gesture.moveBy(offset);
  await tester.pump(const Duration(milliseconds: 10));
  await gesture.up();
}