mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
WIP Commits separated as follows: - Update lints in analysis_options files - Run `dart fix --apply` - Clean up leftover analysis issues - Run `dart format .` in the right places. Local analysis and testing passes. Checking CI now. Part of https://github.com/flutter/flutter/issues/178827 - Adoption of flutter_lints in examples/api coming in a separate change (cc @loic-sharma) ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
233 lines
7.8 KiB
Dart
233 lines
7.8 KiB
Dart
// 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/widgets.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
const String kApiDocsLink =
|
|
'See "Types with special considerations" at https://api.flutter.dev/flutter/animation/Tween-class.html for more information.';
|
|
|
|
void main() {
|
|
test(
|
|
'throws flutter error when tweening types that do not fully satisfy tween requirements - Object',
|
|
() {
|
|
final objectTween = Tween<Object>(begin: Object(), end: Object());
|
|
|
|
expect(
|
|
() => objectTween.transform(0.1),
|
|
throwsA(
|
|
isA<FlutterError>().having(
|
|
(FlutterError error) =>
|
|
error.diagnostics.map((DiagnosticsNode node) => node.toString()),
|
|
'diagnostics',
|
|
<String>[
|
|
'Cannot lerp between "Instance of \'Object\'" and "Instance of \'Object\'".',
|
|
'The type Object might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
|
|
'There may be a dedicated "ObjectTween" for this type, or you may need to create one.',
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'throws flutter error when tweening types that do not fully satisfy tween requirements - Color',
|
|
() {
|
|
final colorTween = Tween<Color>(begin: const Color(0xFF000000), end: const Color(0xFFFFFFFF));
|
|
|
|
expect(
|
|
() => colorTween.transform(0.1),
|
|
throwsA(
|
|
isA<FlutterError>().having(
|
|
(FlutterError error) =>
|
|
error.diagnostics.map((DiagnosticsNode node) => node.toString()),
|
|
'diagnostics',
|
|
<String>[
|
|
'Cannot lerp between "${const Color(0xff000000)}" and "${const Color(0xffffffff)}".',
|
|
'The type Color might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
|
|
'To lerp colors, consider ColorTween instead.',
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'throws flutter error when tweening types that do not fully satisfy tween requirements - Rect',
|
|
() {
|
|
final rectTween = Tween<Rect>(
|
|
begin: const Rect.fromLTWH(0, 0, 10, 10),
|
|
end: const Rect.fromLTWH(2, 2, 2, 2),
|
|
);
|
|
|
|
expect(
|
|
() => rectTween.transform(0.1),
|
|
throwsA(
|
|
isA<FlutterError>().having(
|
|
(FlutterError error) =>
|
|
error.diagnostics.map((DiagnosticsNode node) => node.toString()),
|
|
'diagnostics',
|
|
<String>[
|
|
'Cannot lerp between "Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)" and "Rect.fromLTRB(2.0, 2.0, 4.0, 4.0)".',
|
|
'The type Rect might not fully implement `+`, `-`, and/or `*`. $kApiDocsLink',
|
|
'To lerp rects, consider RectTween instead.',
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'throws flutter error when tweening types that do not fully satisfy tween requirements - int',
|
|
() {
|
|
final colorTween = Tween<int>(begin: 0, end: 1);
|
|
|
|
expect(
|
|
() => colorTween.transform(0.1),
|
|
throwsA(
|
|
isA<FlutterError>().having(
|
|
(FlutterError error) =>
|
|
error.diagnostics.map((DiagnosticsNode node) => node.toString()),
|
|
'diagnostics',
|
|
<String>[
|
|
'Cannot lerp between "0" and "1".',
|
|
'The type int returned a double after multiplication with a double value. $kApiDocsLink',
|
|
'To lerp int values, consider IntTween or StepTween instead.',
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
test('Can chain tweens', () {
|
|
final tween = Tween<double>(begin: 0.30, end: 0.50);
|
|
expect(tween, hasOneLineDescription);
|
|
final Animatable<double> chain = tween.chain(Tween<double>(begin: 0.50, end: 1.0));
|
|
final controller = AnimationController(vsync: const TestVSync());
|
|
expect(chain.evaluate(controller), 0.40);
|
|
expect(chain, hasOneLineDescription);
|
|
});
|
|
|
|
test('Can animate tweens', () {
|
|
final tween = Tween<double>(begin: 0.30, end: 0.50);
|
|
final controller = AnimationController(vsync: const TestVSync());
|
|
final Animation<double> animation = tween.animate(controller);
|
|
controller.value = 0.50;
|
|
expect(animation.value, 0.40);
|
|
expect(animation, hasOneLineDescription);
|
|
expect(animation.toStringDetails(), hasOneLineDescription);
|
|
});
|
|
|
|
test('Can drive tweens', () {
|
|
final tween = Tween<double>(begin: 0.30, end: 0.50);
|
|
final controller = AnimationController(vsync: const TestVSync());
|
|
final Animation<double> animation = controller.drive(tween);
|
|
controller.value = 0.50;
|
|
expect(animation.value, 0.40);
|
|
expect(animation, hasOneLineDescription);
|
|
expect(animation.toStringDetails(), hasOneLineDescription);
|
|
});
|
|
|
|
test('BorderTween nullable test', () {
|
|
var tween = BorderTween();
|
|
expect(tween.lerp(0.0), null);
|
|
expect(tween.lerp(1.0), null);
|
|
|
|
tween = BorderTween(end: const Border(top: BorderSide()));
|
|
expect(tween.lerp(0.0), const Border());
|
|
expect(tween.lerp(0.5), const Border(top: BorderSide(width: 0.5)));
|
|
expect(tween.lerp(1.0), const Border(top: BorderSide()));
|
|
});
|
|
|
|
test('SizeTween', () {
|
|
final tween = SizeTween(begin: Size.zero, end: const Size(20.0, 30.0));
|
|
expect(tween.lerp(0.5), equals(const Size(10.0, 15.0)));
|
|
expect(tween, hasOneLineDescription);
|
|
});
|
|
|
|
test('IntTween', () {
|
|
final tween = IntTween(begin: 5, end: 9);
|
|
expect(tween.lerp(0.5), 7);
|
|
expect(tween.lerp(0.7), 8);
|
|
});
|
|
|
|
test('RectTween', () {
|
|
const a = Rect.fromLTWH(5.0, 3.0, 7.0, 11.0);
|
|
const b = Rect.fromLTWH(8.0, 12.0, 14.0, 18.0);
|
|
final tween = RectTween(begin: a, end: b);
|
|
expect(tween.lerp(0.5), equals(Rect.lerp(a, b, 0.5)));
|
|
expect(tween, hasOneLineDescription);
|
|
});
|
|
|
|
test('Matrix4Tween', () {
|
|
final a = Matrix4.identity();
|
|
final Matrix4 b = a.clone()
|
|
..translate(6.0, -8.0)
|
|
..scale(0.5, 1.0, 5.0);
|
|
final tween = Matrix4Tween(begin: a, end: b);
|
|
expect(tween.lerp(0.0), equals(a));
|
|
expect(tween.lerp(1.0), equals(b));
|
|
expect(
|
|
tween.lerp(0.5),
|
|
equals(
|
|
a.clone()
|
|
..translate(3.0, -4.0)
|
|
..scale(0.75, 1.0, 3.0),
|
|
),
|
|
);
|
|
final Matrix4 c = a.clone()..rotateZ(1.0);
|
|
final rotationTween = Matrix4Tween(begin: a, end: c);
|
|
expect(rotationTween.lerp(0.0), equals(a));
|
|
expect(rotationTween.lerp(1.0), equals(c));
|
|
expect(rotationTween.lerp(0.5).absoluteError(a.clone()..rotateZ(0.5)), moreOrLessEquals(0.0));
|
|
});
|
|
|
|
test('ConstantTween', () {
|
|
final tween = ConstantTween<double>(100.0);
|
|
expect(tween.begin, 100.0);
|
|
expect(tween.end, 100.0);
|
|
expect(tween.lerp(0.0), 100.0);
|
|
expect(tween.lerp(0.5), 100.0);
|
|
expect(tween.lerp(1.0), 100.0);
|
|
});
|
|
|
|
test('ReverseTween', () {
|
|
final tween = ReverseTween<int>(IntTween(begin: 5, end: 9));
|
|
expect(tween.lerp(0.5), 7);
|
|
expect(tween.lerp(0.7), 6);
|
|
});
|
|
|
|
test('ColorTween', () {
|
|
final tween = ColorTween(begin: const Color(0xff000000), end: const Color(0xffffffff));
|
|
expect(tween.lerp(0.0), isSameColorAs(const Color(0xff000000)));
|
|
expect(tween.lerp(0.5), isSameColorAs(const Color(0xff7f7f7f)));
|
|
expect(tween.lerp(0.7), isSameColorAs(const Color(0xffb2b2b2)));
|
|
expect(tween.lerp(1.0), isSameColorAs(const Color(0xffffffff)));
|
|
});
|
|
|
|
test('StepTween', () {
|
|
final tween = StepTween(begin: 5, end: 9);
|
|
expect(tween.lerp(0.5), 7);
|
|
expect(tween.lerp(0.7), 7);
|
|
});
|
|
|
|
test('CurveTween', () {
|
|
final tween = CurveTween(curve: Curves.easeIn);
|
|
expect(tween.transform(0.0), 0.0);
|
|
expect(tween.transform(0.5), 0.31640625);
|
|
expect(tween.transform(1.0), 1.0);
|
|
});
|
|
|
|
test('BorderRadiusTween nullable test', () {
|
|
final tween = BorderRadiusTween();
|
|
expect(tween.transform(0.0), null);
|
|
expect(tween.transform(1.0), null);
|
|
expect(tween.lerp(0.0), null);
|
|
});
|
|
}
|