패키지, 저장소 및 앱 카테고리 검색

animated_to 표지
패키지 상세v0.9.1

animated_to

AnimateTo는 자식을 새 위치로 애니메이션 시키며 계산이 필요하지 않습니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
2.9천
좋아요
211
Pub 점수
160

패키지 설치

flutter pub add animated_to

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

AnimatedTo는 수동으로 애니메이션 값을 계산할 필요 없이 위젯을 새 위치로 애니메이션하는 것을 단순화하는 Flutter 패키지입니다. Flutter의 애니메이션 시스템을 활용하여 자동으로 전환을 처리하므로 UI 요소의 부드럽고 선언적인 이동에 이상적입니다. 이 패키지는 가볍고 기존의 Flutter 프로젝트에 원활하게 통합됩니다. 모바일, 웹, 데스크톱을 포함한 모든 주요 플랫폼을 지원하며 장치 간 일관된 동작을 보장합니다. 요소가 동적으로 재배치되어야 하는 상호작용 인터페이스에 적합합니다.

사용 사례

  • 스무딩 UI 요소 재배치
  • 동적 움직임을 갖춘 인터랙티브 레이아웃
  • 양식 내 자동 애니메이션 전환
  • 반응형 디자인 조정
  • 화면 요소 강조
  • 드래그 앤 드롭 인터페이스 개선

주요 기능

  • 수동 애니메이션 계산 없음
  • 자동 위치 보간
  • 크로스 플랫폼 호환성
  • 간단한 선언형 API
  • 내장 애니메이션 컨트롤러 관리
  • 모든 플러터 플랫폼을 지원합니다

적합한 대상

  • 간단한 애니메이션을 찾는 개발자
  • 모션을 구현하는 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 개

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.