flutter 当Transform.scale包裹opacity时,重复缩放动画不起作用,

nzkunb0c  于 10个月前  发布在  Flutter
关注(0)|答案(2)|浏览(90)

重现步骤

  1. 使用Flutter beta
  2. 粘贴代码
  3. 点击正方形,动画按预期工作
  4. 再次点击正方形,缩放不起作用,只有透明度起作用。

预期结果

缩放动画在后续的动画调用中应该起作用,而不仅仅是第一次。

实际结果

当缩放动画被多次调用时,它不起作用,而透明度动画可以。经过热重载后,只渲染缩放动画的最后一帧。有趣的是,如果我们用Transform.scale小部件替换Opacity小部件,一切都按预期工作(当Opacity包裹Transform.scale时,而不是Transform.scale包裹Opacity)。

代码示例

代码示例

  1. class TestAnimation extends StatefulWidget {
  2. const TestAnimation({super.key});
  3. @override
  4. State<TestAnimation> createState() => _TestAnimationState();
  5. }
  6. class _TestAnimationState extends State<TestAnimation>
  7. with SingleTickerProviderStateMixin {
  8. late final AnimationController _controller;
  9. Animation<double> _scale = const AlwaysStoppedAnimation(0);
  10. Animation<double> _opacity = const AlwaysStoppedAnimation(0);
  11. @override
  12. void initState() {
  13. super.initState();
  14. _controller = AnimationController(
  15. vsync: this,
  16. duration: const Duration(seconds: 2),
  17. );
  18. _controller.addListener(() => setState(() {}));
  19. }
  20. @override
  21. void dispose() {
  22. _controller.dispose();
  23. super.dispose();
  24. }
  25. TickerFuture _animate() {
  26. _scale = _controller.drive(
  27. Tween<double>(
  28. begin: 0,
  29. end: 1.5,
  30. ),
  31. );
  32. _opacity = _controller.drive(
  33. Tween<double>(
  34. begin: 0,
  35. end: 1,
  36. ),
  37. );
  38. return _controller.forward(from: 0);
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. return GestureDetector(
  43. behavior: HitTestBehavior.translucent,
  44. onTap: _animate,
  45. child: DecoratedBox(
  46. decoration: BoxDecoration(
  47. border: Border.all(
  48. width: 2,
  49. color: Colors.green,
  50. ),
  51. ),
  52. child: Transform.scale(
  53. scale: _scale.value,
  54. child: Opacity(
  55. opacity: _opacity.value,
  56. child: Container(
  57. width: 50,
  58. height: 50,
  59. color: Colors.red,
  60. ),
  61. ),
  62. ),
  63. ),
  64. );
  65. }
  66. }

截图或视频

截图/视频演示Screen.Recording.2024-07-14.at.4.27.32.PM.mov

日志

日志

  1. [Paste your logs here]

Flutter Doctor输出

Doctor输出

  1. Doctor summary (to see all details, run flutter doctor -v):
  2. [✓] Flutter (Channel beta, 3.23.0-0.1.pre, on macOS 14.5 23F79 darwin-arm64,
  3. locale en-US)
  4. [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
  5. [✓] Xcode - develop for iOS and macOS (Xcode 15.4)
  6. [✓] Chrome - develop for the web
  7. [✓] Android Studio (version 2022.3)
  8. [✓] VS Code (version 1.90.2)
  9. [✓] Connected device (5 available)
  10. [✓] Network resources
  11. No issues found!
ego6inou

ego6inou1#

可复现性:使用上述提供的代码示例
完整代码示例

  1. import 'package:flutter/material.dart';
  2. /// Flutter code sample for [Autocomplete].
  3. void main() => runApp(const AutocompleteExampleApp());
  4. class AutocompleteExampleApp extends StatelessWidget {
  5. const AutocompleteExampleApp({super.key});
  6. @override
  7. Widget build(BuildContext context) {
  8. return const MaterialApp(
  9. home: TestAnimation(),
  10. );
  11. }
  12. }
  13. class TestAnimation extends StatefulWidget {
  14. const TestAnimation({super.key});
  15. @override
  16. State<TestAnimation> createState() => _TestAnimationState();
  17. }
  18. class _TestAnimationState extends State<TestAnimation>
  19. with SingleTickerProviderStateMixin {
  20. late final AnimationController _controller;
  21. Animation<double> _scale = const AlwaysStoppedAnimation(0);
  22. Animation<double> _opacity = const AlwaysStoppedAnimation(0);
  23. @override
  24. void initState() {
  25. super.initState();
  26. _controller = AnimationController(
  27. vsync: this,
  28. duration: const Duration(seconds: 2),
  29. );
  30. _controller.addListener(() => setState(() {}));
  31. }
  32. @override
  33. void dispose() {
  34. _controller.dispose();
  35. super.dispose();
  36. }
  37. TickerFuture _animate() {
  38. _scale = _controller.drive(
  39. Tween<double>(
  40. begin: 0,
  41. end: 1.5,
  42. ),
  43. );
  44. _opacity = _controller.drive(
  45. Tween<double>(
  46. begin: 0,
  47. end: 1,
  48. ),
  49. );
  50. return _controller.forward(from: 0);
  51. }
  52. @override
  53. Widget build(BuildContext context) {
  54. return GestureDetector(
  55. behavior: HitTestBehavior.translucent,
  56. onTap: _animate,
  57. child: Center(
  58. child: SizedBox(
  59. height: 100,
  60. width: 100,
  61. child: DecoratedBox(
  62. decoration: BoxDecoration(
  63. border: Border.all(
  64. width: 2,
  65. color: Colors.green,
  66. ),
  67. ),
  68. child: Transform.scale(
  69. scale: _scale.value,
  70. child: Opacity(
  71. opacity: _opacity.value,
  72. child: Container(
  73. width: 50,
  74. height: 50,
  75. color: Colors.red,
  76. ),
  77. ),
  78. ),
  79. ),
  80. ),
  81. ),
  82. );
  83. }
  84. }

flutter doctor -v

  1. [✓] Flutter (Channel stable, 3.22.2, on macOS 14.4.1 23E224 darwin-arm64, locale en-GB)
  2. Flutter version 3.22.2 on channel stable at /Users/nexus/dev/sdks/flutter
  3. Upstream repository https://github.com/flutter/flutter.git
  4. Framework revision 761747bfc5 (6 weeks ago), 2024-06-05 22:15:13 +0200
  5. Engine revision edd8546116
  6. Dart version 3.4.3
  7. DevTools version 2.34.3
  8. [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
  9. Android SDK at /Users/nexus/Library/Android/sdk
  10. Platform android-34, build-tools 34.0.0
  11. Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
  12. Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
  13. All Android licenses accepted.
  14. [✓] Xcode - develop for iOS and macOS (Xcode 15.3)
  15. Xcode at /Applications/Xcode-15.3.0.app/Contents/Developer
  16. Build 15E204a
  17. CocoaPods version 1.15.2
  18. [✓] Chrome - develop for the web
  19. Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
  20. [✓] Android Studio (version 2024.1)
  21. Android Studio at /Users/nexus/Applications/Android Studio.app/Contents
  22. Flutter plugin can be installed from:
  23. 🔨 https://plugins.jetbrains.com/plugin/9212-flutter
  24. Dart plugin can be installed from:
  25. 🔨 https://plugins.jetbrains.com/plugin/6351-dart
  26. Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
  27. [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5)
  28. IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app
  29. Flutter plugin version 77.2.2
  30. Dart plugin version 232.10286
  31. [✓] VS Code (version 1.91.1)
  32. VS Code at /Applications/Visual Studio Code.app/Contents
  33. Flutter extension version 3.92.0
  34. [✓] Connected device (5 available)
  35. Nexus (mobile) 00008020-001875E83A38002E ios iOS 17.5.1 21F90
  36. Deans iPad (mobile) 00008103-000825C811E3401E ios iOS 17.5.1 21F90
  37. macOS (desktop) macos darwin-arm64 macOS 14.4.1 23E224 darwin-arm64
  38. Mac Designed for iPad (desktop) mac-designed-for-ipad darwin macOS 14.4.1 23E224 darwin-arm64
  39. Chrome (web) chrome web-javascript Google Chrome 126.0.6478.127
  40. [✓] Network resources
  41. All expected network resources are available.
  42. No issues found!
  1. [!] Flutter (Channel master, 3.24.0-1.0.pre.114, on macOS 14.4.1 23E224 darwin-arm64, locale en-GB)
  2. Flutter version 3.24.0-1.0.pre.114 on channel master at /Users/nexus/dev/sdks/flutters
  3. ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path.
  4. ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path.
  5. Upstream repository https://github.com/flutter/flutter.git
  6. Framework revision 82b63ff27d (26 hours ago), 2024-07-14 05:07:36 -0400
  7. Engine revision a921637f16
  8. Dart version 3.6.0 (build 3.6.0-36.0.dev)
  9. DevTools version 2.37.1
  10. If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
  11. [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
  12. Android SDK at /Users/nexus/Library/Android/sdk
  13. Platform android-34, build-tools 34.0.0
  14. Java binary at: /Users/nexus/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
  15. Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
  16. All Android licenses accepted.
  17. [✓] Xcode - develop for iOS and macOS (Xcode 15.3)
  18. Xcode at /Applications/Xcode-15.3.0.app/Contents/Developer
  19. Build 15E204a
  20. CocoaPods version 1.15.2
  21. [✓] Chrome - develop for the web
  22. Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
  23. [✓] Android Studio (version 2024.1)
  24. Android Studio at /Users/nexus/Applications/Android Studio.app/Contents
  25. Flutter plugin can be installed from:
  26. 🔨 https://plugins.jetbrains.com/plugin/9212-flutter
  27. Dart plugin can be installed from:
  28. 🔨 https://plugins.jetbrains.com/plugin/6351-dart
  29. Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
  30. [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5)
  31. IntelliJ at /Users/nexus/Applications/IntelliJ IDEA Ultimate.app
  32. Flutter plugin version 77.2.2
  33. Dart plugin version 232.10286
  34. [✓] VS Code (version 1.91.1)
  35. VS Code at /Applications/Visual Studio Code.app/Contents
  36. Flutter extension version 3.92.0
  37. [✓] Connected device (5 available)
  38. Nexus (mobile) 00008020-001875E83A38002E ios iOS 17.5.1 21F90
  39. Deans iPad (mobile) 00008103-000825C811E3401E ios iOS 17.5.1 21F90
  40. macOS (desktop) macos darwin-arm64 macOS 14.4.1 23E224 darwin-arm64
  41. Mac Designed for iPad (desktop) mac-designed-for-ipad darwin macOS 14.4.1 23E224 darwin-arm64
  42. Chrome (web) chrome web-javascript Google Chrome 126.0.6478.127
  43. [✓] Network resources
  44. All expected network resources are available.
  45. ! Doctor found issues in 1 category.
展开查看全部
hmtdttj4

hmtdttj42#

似乎在RenderOpacity中存在一个涉及重绘边界逻辑的问题。
当alpha值在零和非零之间转换时,RenderOpacity.isRepaintBoundary会发生变化。当这种情况发生时,会调用markNeedsCompositingBitsUpdate,但出于某种原因,小部件仍然渲染为被裁剪到原始大小的样子。
如果我将_opacity动画更改为从0.01开始,这样alpha值永远不会为零,那么这个渲染就会正确。
@jonahwilliams

相关问题