sliver_appbar_opacity_test.dart 7.58 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/material.dart';
6 7 8 9
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
10
  testWidgets('!pinned && !floating && !bottom ==> fade opacity', (WidgetTester tester) async {
11 12 13 14 15 16 17
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(
        _TestWidget(
          pinned: false,
          floating: false,
          bottom: false,
          controller: controller,
18
        ),
19 20 21
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
22
    expect(render.text.style!.color!.opacity, 1.0);
23 24 25

    controller.jumpTo(200.0);
    await tester.pumpAndSettle();
26
    expect(render.text.style!.color!.opacity, 0.0);
27 28
  });

29
  testWidgets('!pinned && !floating && bottom ==> fade opacity', (WidgetTester tester) async {
30 31 32 33 34 35 36
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(
        _TestWidget(
          pinned: false,
          floating: false,
          bottom: true,
          controller: controller,
37
        ),
38 39 40
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
41
    expect(render.text.style!.color!.opacity, 1.0);
42 43 44

    controller.jumpTo(200.0);
    await tester.pumpAndSettle();
45
    expect(render.text.style!.color!.opacity, 0.0);
46 47
  });

48
  testWidgets('!pinned && floating && !bottom ==> fade opacity', (WidgetTester tester) async {
49 50 51 52 53 54 55
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(
        _TestWidget(
          pinned: false,
          floating: true,
          bottom: false,
          controller: controller,
56
        ),
57 58 59
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
60
    expect(render.text.style!.color!.opacity, 1.0);
61 62 63

    controller.jumpTo(200.0);
    await tester.pumpAndSettle();
64
    expect(render.text.style!.color!.opacity, 0.0);
65 66
  });

67
  testWidgets('!pinned && floating && bottom ==> fade opacity', (WidgetTester tester) async {
68 69 70 71 72 73 74
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(
        _TestWidget(
          pinned: false,
          floating: true,
          bottom: true,
          controller: controller,
75
        ),
76 77 78
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
79
    expect(render.text.style!.color!.opacity, 1.0);
80 81 82

    controller.jumpTo(200.0);
    await tester.pumpAndSettle();
83
    expect(render.text.style!.color!.opacity, 0.0);
84 85 86 87 88 89 90 91 92 93
  });

  testWidgets('pinned && !floating && !bottom ==> 1.0 opacity', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(
        _TestWidget(
          pinned: true,
          floating: false,
          bottom: false,
          controller: controller,
94
        ),
95 96 97
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
98
    expect(render.text.style!.color!.opacity, 1.0);
99 100 101

    controller.jumpTo(200.0);
    await tester.pumpAndSettle();
102
    expect(render.text.style!.color!.opacity, 1.0);
103 104 105 106 107 108 109 110 111 112
  });

  testWidgets('pinned && !floating && bottom ==> 1.0 opacity', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(
        _TestWidget(
          pinned: true,
          floating: false,
          bottom: true,
          controller: controller,
113
        ),
114 115 116
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
117
    expect(render.text.style!.color!.opacity, 1.0);
118 119 120

    controller.jumpTo(200.0);
    await tester.pumpAndSettle();
121
    expect(render.text.style!.color!.opacity, 1.0);
122 123 124 125 126 127 128 129 130 131 132 133
  });

  testWidgets('pinned && floating && !bottom ==> 1.0 opacity', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/25000.

    final ScrollController controller = ScrollController();
    await tester.pumpWidget(
        _TestWidget(
          pinned: true,
          floating: true,
          bottom: false,
          controller: controller,
134
        ),
135 136 137
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
138
    expect(render.text.style!.color!.opacity, 1.0);
139 140 141

    controller.jumpTo(200.0);
    await tester.pumpAndSettle();
142
    expect(render.text.style!.color!.opacity, 1.0);
143 144
  });

145
  testWidgets('pinned && floating && bottom && extraToolbarHeight == 0.0 ==> fade opacity', (WidgetTester tester) async {
146 147 148 149 150 151 152 153 154
    // Regression test for https://github.com/flutter/flutter/issues/25993.

    final ScrollController controller = ScrollController();
    await tester.pumpWidget(
      _TestWidget(
        pinned: true,
        floating: true,
        bottom: true,
        controller: controller,
155
      ),
156 157 158
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
159
    expect(render.text.style!.color!.opacity, 1.0);
160 161 162

    controller.jumpTo(200.0);
    await tester.pumpAndSettle();
163
    expect(render.text.style!.color!.opacity, 0.0);
164
  });
165 166 167 168 169 170 171 172 173 174 175 176 177 178

  testWidgets('pinned && floating && bottom && extraToolbarHeight != 0.0 ==> 1.0 opacity', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(
      _TestWidget(
        pinned: true,
        floating: true,
        bottom: true,
        collapsedHeight: 100.0,
        controller: controller,
      ),
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
179
    expect(render.text.style!.color!.opacity, 1.0);
180 181 182

    controller.jumpTo(200.0);
    await tester.pumpAndSettle();
183
    expect(render.text.style!.color!.opacity, 1.0);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  });

  testWidgets('!pinned && !floating && !bottom && extraToolbarHeight != 0.0 ==> fade opacity', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    const double collapsedHeight = 100.0;
    await tester.pumpWidget(
        _TestWidget(
          pinned: false,
          floating: false,
          bottom: false,
          controller: controller,
          collapsedHeight: collapsedHeight,
        ),
    );

    final RenderParagraph render = tester.renderObject(find.text('Hallo Welt!!1'));
200
    expect(render.text.style!.color!.opacity, 1.0);
201 202 203

    controller.jumpTo(collapsedHeight);
    await tester.pumpAndSettle();
204
    expect(render.text.style!.color!.opacity, 0.0);
205
  });
206 207 208 209
}

class _TestWidget extends StatelessWidget {

210
  const _TestWidget({
211 212 213
    required this.pinned,
    required this.floating,
    required this.bottom,
214 215 216
    this.controller,
    this.collapsedHeight,
  });
217 218 219 220

  final bool pinned;
  final bool floating;
  final bool bottom;
221 222
  final ScrollController? controller;
  final double? collapsedHeight;
223 224 225 226 227 228 229 230 231 232 233

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CustomScrollView(
        controller: controller,
        slivers: <Widget>[
          SliverAppBar(
            pinned: pinned,
            floating: floating,
            expandedHeight: 120.0,
234
            collapsedHeight: collapsedHeight,
235 236 237 238 239 240 241 242
            title: const Text('Hallo Welt!!1'),
            bottom: !bottom ? null :  PreferredSize(
              preferredSize: const Size.fromHeight(35.0),
              child: Container(),
            ),
          ),
          SliverList(
            delegate: SliverChildListDelegate(List<Widget>.generate(20, (int i) {
243
              return SizedBox(
244
                height: 100.0,
245
                child: Text('Tile $i'),
246 247
              );
            })),
248
          ),
249 250 251 252 253 254
        ],
      ),
    );
  }

}