搜索插件、代码仓与 APP 分类

animated_to 封面
插件详情v0.9.1

animated_to

AnimateTo 将其子项动画移动到新位置,无需计算。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2930
喜欢数
211
Pub 分数
160

安装插件

flutter pub add animated_to

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

AnimatedTo 是一个 Flutter 包,可简化将小部件动画移动到新位置的过程,无需手动计算动画值。它通过利用 Flutter 的动画系统自动处理过渡,非常适合实现 UI 元素的流畅、声明式移动。该包轻量且可无缝集成到现有的 Flutter 项目中。支持所有主要平台,包括移动设备、网页和桌面端,确保在不同设备上行为一致。非常适合需要动态重新定位元素的交互式界面。

适合场景

  • 平滑的UI元素重新定位
  • 带有动态移动的交互式布局
  • 表单中的自动动画过渡
  • 响应式设计调整
  • 屏幕元素高亮
  • 拖放界面增强功能

主要特点

  • 无需手动计算动画
  • 自动位置插值
  • 跨平台兼容性
  • 简单的声明式 API
  • 内置动画控制器管理
  • 支持所有 Flutter 平台

适合对象

  • 寻求简单动画的开发者
  • UI/UX 设计师实现动态效果
  • 动态布局更改的应用程序
  • 带有视觉反馈的教育类应用程序
  • 快速原型交互

使用建议

  • 对动画曲线的控制有限
  • 不适合复杂的多步骤动画
  • 需要子部件在布局上下文中定位
  • 如果在大型列表中过度使用,可能会影响性能
  • 最适合用于单一、专注的运动,而不是复杂的动作序列

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

animated_to

animated_to provides a widget named AnimatedTo which enables its child widget to change its position with animation when its position is updated for any reason, typically for rebuilding.

AnimatedTo Preview

No calculation is required. Because every calculation is RenderObject's business in the Flutter framework, AnimatedTo just hires the calculated position and starts animation there.

Usage

First, place whatever widget you want to animate when its position changes.

Container(
  width: 60,
  height: 60,
  color: Colors.blue,
)

Then, wrap the widget with AnimatedTo.curve or AnimatedTo.spring passing GlobalKey.

AnimatedTo.curve(
  globalKey: _globalKey,
  child: Container(
    width: 60,
    height: 60,
    color: Colors.blue,
  ),
)

Note that GlobalKey is necessary to keep its RenderObject alive even when the widget's depth or position changes.

And, that's it!

All what you need to do is causing rebuilds using whatever state management packages or just setState and change its position. AnimatedTo will automatically leads the child to the new position with animation.

Spring animation

animated_to provides AnimatedTo.spring which animates its child using SpringSimulation.

https://api.flutter.dev/flutter/physics/SpringSimulation-class.html

This simulates its position transition based on the physical simulation, which make the animations smooth and natural.

What you have to do is just switch AnimatedTo.curve into AnimatedTo.spring to activate spring simulation. You can also configure your own SpringDescription using description argument, or you can use pre-defined configuration using @timcreatedit's motor package.

https://pub.dev/packages/motor

AnimatedTo.spring(
  globalKey: _globalKey,
  // you can retrieve SpringDescription from CupertinoMotion like below
  description: CupertinoMotion.smooth().description,
),

As motor is also used inside animated_to package(thanks @timcreatedit!), make sure you may have potential risk of dependency conflicts when directly depending on the package on your app side.

spring demo

Some more features

appearingFrom lets you specify the start position of the animation in the first frame. By providing an absolute position in the global coordinate system, the widget will appear there and then animate to the original position.

appearingFrom demo

slidingFrom lets you specify the start position of the animation in the first frame as well. By providing a relative position to the child's intrinsic position, the widget will slide from the specified position and then animate to the original position.

slidingFrom demo

Hit Testing and Coordinate System

By default, AnimatedTo widgets can only receive gestures (taps, drags, etc.) at their layout position, not at their animated position. This is because Flutter's hit testing system checks widgets at their natural layout position, even though AnimatedTo visually paints them at a different location during animation.

AnimatedToBoundary

AnimatedToBoundary serves two important purposes:

1. Enable Hit Testing During Animation

To enable gesture detection at the animated position, wrap your widget tree with AnimatedToBoundary. This boundary intercepts hit tests and properly forwards them to animating widgets at their current animated positions.

void main() => runApp(
  AnimatedToBoundary( // Place near root
    child: MaterialApp(
      home: MyHomePage(),
    ),
  ),
);

AnimatedToBoundary is optional. If you don't need to detect gestures on your AnimatedTo widgets during animation, you can omit it completely. The animations will work perfectly fine without it.

2. Establish Stable Coordinate Origin

AnimatedToBoundary can be used to prevent AnimatedTo from unexpected behavior by clarifying the "origin" of the coordinate system. By wrapping a page widget (typically Scaffold) with AnimatedToBoundary, AnimatedTo is not affected by animations of the whole screen, such as Navigator.push().

Without this isolation, when you navigate to a new page, the entire screen slides (the navigation transition), and AnimatedTo would incorrectly interpret this screen movement as a position change. This causes unexpected animations when you want to start an animation during the navigation transition.

By establishing a coordinate boundary, AnimatedToBoundary ensures that AnimatedTo only responds to position changes within that boundary, ignoring ancestor animations like page transitions.

@override
Widget build(BuildContext context) {
  return AnimatedToBoundary(
    child: Scaffold(
      body: AnimatedTo.spring(
        globalKey: _key,
        slidingFrom: Offset(100, 100),
        child: _YourWidget(),
      ),
    ),
  )
}
Placement and Nesting

AnimatedToBoundary should be placed:

  • Near the root of your widget tree for global hit testing coverage
  • Around individual page widgets to isolate navigation transition effects

Note that AnimatedToBoundary can be nested, so you don't have to remove other AnimatedToBoundary widgets you placed at the root when adding a new one around a page.

Limitations

  • AnimatedTo doesn't work when it is on Sliver-related widgets, such as ListView, and the animation happens across slivers. It is because RenderSliver's layout system is totally different from RenderBox's and there is no way to detect the exact from/to. If the animation happened inside a single sliver, it works.

animation inside sliver

All arguments

Argument Type Description
globalKey GlobalKey A key to keep the widget alive even when its depth or position changes.
child Widget The widget you want to animate when its position changes.
duration Duration (curve only) The duration of the animation.
curve Curve (curve only) The curve of the animation.
description SpringDescription (spring only) The configuration of the spring simulation.
velocityBuilder Offset Function()? (spring only) A function to provide initial velocity to start spring animation.
verticalController AnimationController? Required if AnimatedTo is on the subtree of vertical SingleChildScrollView. Share the controller with the SingleChildScrollView to properly animate the widget. Don't provide one when AnimatedTo is on ListView.
horizontalController AnimationController? Required if AnimatedTo is on the subtree of horizontal SingleChildScrollView. Share the controller with the SingleChildScrollView to properly animate the widget. Don't provide one when AnimatedTo is on ListView.
appearingFrom` Offset? The start position of the animation in the first frame. This offset is an absolute position in the global coordinate system.
slidingFrom Offset? The start position of the animation in the first frame. This offset is a relative position to the child's intrinsic position.
enabled bool Whether the animation is enabled.
onEnd void Function(AnimationEndCause cause)? The callback when the animation is completed. cause shows the reason why the animation is completed.
sizeWidget Widget A widget for calculating desired size and position regardless of animations.

See example for more details.

Author's X account @tsuyoshi_chujo also posts some example screenshots.

Contact

If you have anything you want to inform me (@chooyan-eng), such as suggestions to enhance this package or functionalities you want etc, feel free to make issues on GitHub or send messages on X @tsuyoshi_chujo (Japanese @chooyan_i18n).

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。