|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:ui'; |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter_api_samples/widgets/implicit_animations/sliver_animated_opacity.0.dart' |
| 9 | + as example; |
| 10 | +import 'package:flutter_test/flutter_test.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + testWidgets( |
| 14 | + 'SilverAnimatedOpacity animates on FloatingActionButton tap', |
| 15 | + (WidgetTester tester) async { |
| 16 | + await tester.pumpWidget( |
| 17 | + const example.SliverAnimatedOpacityExampleApp(), |
| 18 | + ); |
| 19 | + |
| 20 | + final Finder fadeTransitionFinder = find.descendant( |
| 21 | + of: find.byType(SliverAnimatedOpacity), |
| 22 | + matching: find.byType(SliverFadeTransition), |
| 23 | + ); |
| 24 | + |
| 25 | + const double beginOpacity = 1.0; |
| 26 | + const double endOpacity = 0.0; |
| 27 | + |
| 28 | + SliverFadeTransition fadeTransition = tester.widget(fadeTransitionFinder); |
| 29 | + expect(fadeTransition.opacity.value, beginOpacity); |
| 30 | + |
| 31 | + // Tap on the FloatingActionButton to start the forward animation. |
| 32 | + await tester.tap(find.byType(FloatingActionButton)); |
| 33 | + await tester.pump(); |
| 34 | + |
| 35 | + fadeTransition = tester.widget(fadeTransitionFinder); |
| 36 | + expect(fadeTransition.opacity.value, beginOpacity); |
| 37 | + |
| 38 | + // Advance animation to the middle. |
| 39 | + await tester.pump(example.SliverAnimatedOpacityExampleApp.duration ~/ 2); |
| 40 | + |
| 41 | + fadeTransition = tester.widget(fadeTransitionFinder); |
| 42 | + expect( |
| 43 | + fadeTransition.opacity.value, |
| 44 | + lerpDouble( |
| 45 | + beginOpacity, |
| 46 | + endOpacity, |
| 47 | + example.SliverAnimatedOpacityExampleApp.curve.transform(0.5), |
| 48 | + ), |
| 49 | + ); |
| 50 | + |
| 51 | + // Advance animation to the end. |
| 52 | + await tester.pump(example.SliverAnimatedOpacityExampleApp.duration ~/ 2); |
| 53 | + |
| 54 | + fadeTransition = tester.widget(fadeTransitionFinder); |
| 55 | + expect(fadeTransition.opacity.value, endOpacity); |
| 56 | + |
| 57 | + // Tap on the FloatingActionButton again to start the reverse animation. |
| 58 | + await tester.tap(find.byType(FloatingActionButton)); |
| 59 | + await tester.pump(); |
| 60 | + |
| 61 | + fadeTransition = tester.widget(fadeTransitionFinder); |
| 62 | + expect(fadeTransition.opacity.value, endOpacity); |
| 63 | + |
| 64 | + // Advance animation to the middle. |
| 65 | + await tester.pump(example.SliverAnimatedOpacityExampleApp.duration ~/ 2); |
| 66 | + |
| 67 | + fadeTransition = tester.widget(fadeTransitionFinder); |
| 68 | + expect( |
| 69 | + fadeTransition.opacity.value, |
| 70 | + lerpDouble( |
| 71 | + endOpacity, |
| 72 | + beginOpacity, |
| 73 | + example.SliverAnimatedOpacityExampleApp.curve.transform(0.5), |
| 74 | + ), |
| 75 | + ); |
| 76 | + |
| 77 | + // Advance animation to the end. |
| 78 | + await tester.pump(example.SliverAnimatedOpacityExampleApp.duration ~/ 2); |
| 79 | + |
| 80 | + fadeTransition = tester.widget(fadeTransitionFinder); |
| 81 | + expect(fadeTransition.opacity.value, beginOpacity); |
| 82 | + }, |
| 83 | + ); |
| 84 | +} |
0 commit comments